How to create Enum for the given objects - java

I have a class called - SparqlResource.java and in the class I am instantiating four objects like this-
public static final SparqlResource MARK_SIMPLE_TYPE = new SparqlResource("ldmext/MarkSimpleType.rq");
public static final SparqlResource FORTRESS_HAS_ENVOY = new SparqlResource("ldmext/FortressHasEnvoy.rq");
public static final SparqlResource FORTRESS_HAS_GUARD = new SparqlResource("ldmext/FortressHasGuard.rq");
public static final SparqlResource FORTRESS_HAS_PORT = new SparqlResource("ldmext/FortressHasPort.rq");
Now from another class - JenaLanguageConstructor.java, I am referencing these objects like this-
runOneQuery(SparqlResource.MARK_SIMPLE_TYPE, true);
runOneQuery(SparqlResource.FORTRESS_HAS_ENVOY, true);
runOneQuery(SparqlResource.FORTRESS_HAS_GUARD, true);
runOneQuery(SparqlResource.FORTRESS_HAS_PORT, true);
Now my question is is there any way I can use enums to achieve this, if so then can any one please give me a sample code which I can use to create the enum?

public enum SPARQLENUM {
MARK_SIMPLE_TYPE("ldmext/MarkSimpleType.rq") ,
FORTRESS_HAS_ENVOY("ldmext/FortressHasEnvoy.rq") ,
FORTRESS_HAS_GUARD("ldmext/FortressHasGuard.rq") ,
FORTRESS_HAS_PORT("ldmext/FortressHasPort.rq");
private String value;
private SPARQLENUM(String value) {
this.value = value;
}
public String getValue(){
return value;
}
}
And you can call it this way:
SPARQLENUM.FORTRESS_HAS_ENVOY.getValue()
EDITED
If you need the SparqlResource object, you can create the enum this way:
public enum SPARQLENUM {
MARK_SIMPLE_TYPE(new SparqlResource("ldmext/MarkSimpleType.rq")) ,
FORTRESS_HAS_ENVOY(new SparqlResource("ldmext/FortressHasEnvoy.rq")) ,
FORTRESS_HAS_GUARD(new SparqlResource("ldmext/FortressHasGuard.rq")) ,
FORTRESS_HAS_PORT(new SparqlResource("ldmext/FortressHasPort.rq"));
private SparqlResource value;
private SPARQLENUM(SparqlResource value) {
this.value = value;
}
public SparqlResource getValue(){
return value;
}
}

Well, creating an enum wouldn't be that hard:
enum MyEnum {
VALUE1("name 1"),
VALUE2("name 2");
private String name;
private MyEnum(String n) {
name = n;
}
//whatever else you need
}

Related

creat an enumeration that containt Integer variables

I want to create an enumeration that containt Integer variables , the result is needed to be something like this :
#AllArgsConstructor
#Getter
public enum Test {
1("Test1"),
2("Test2");
private final String value;
}
As far as I know there is no such thing like this in Java.
If you really need enum for that, you can do it in reverse:
public enum TestEnum {
TEST_1(1),
TEST_2(2);
private final int value;
TestEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}

Use enum like a getter?

I have a util class that keeps track of important system variables:
public static final String REQUEST_ADDRESS = "http.request.address";
public static final String REQUEST_PORT = "http.request.port";
public static final String get(String property) {
return System.getProperty(property);
}
And I can retrieve these values like so:
String port = SystemPropertyHelper.get(SystemPropertyHelper.REQUEST_PORT);
Is it possible, in Java, to get these like an enum?
REQUEST_PORT {
return System.getProperty("http.request.port");
}
String port = SystemPropertyHelper.REQUEST_PORT;
I'd solve it that way.
public static final String REQUEST_PORT = System.getProperty("http.request.port");
enum SystemPropertyHelper {
REQUEST_PORT("http.request.port"), ...;
private String key;
Config(String key) {
this.key = key;
}
public String get() {
return System.getProperty(key);
}
}
and use it like SystemPropertyHelper.REQUEST_PORT.get();
Sure, you could create an enum like this, which would give you access to the property name, and the value:
public enum SystemPropertyEnum {
REQUEST_PORT("http.request.port"),
REQUEST_ADDRESS("http.request.address");
private String propertyName;
private String value;
SystemPropertyEnum(final String propertyName) {
this.propertyName = propertyName;
this.value = System.getProperty(propertyName);
}
public String getPropertyName() {
return propertyName;
}
public String getValue() {
return value;
}
}
However, you could avoid the need to call a getter by just using public static final String variables for your properties, as #halloei suggests.
Also you can do something like this:
public enum Properties {
REQUEST_PORT("http.request.port"),
REQUEST_USE_SSL("http.request.ssl");
// Add others...
private final String value;
Properties(String value) {
this.value = System.getProperty(value);
}
public String getValue() {
return this.value;
}
}
This can be use like:
String port = Properties.REQUEST_PORT.getValue();

how to convert "value" to enum?

I have this enum class:
public enum IconImageTag {
None("val1"),
USD("val2"),
EURO("val3");
}
given a string which represent a "value" (say `"val"1)
how can I convert it to the corresponding enum?
update
I have tried this. Why is this illegal to access static member from the ctor? I get an error.
private final String value;
private static final Map<String, IconImageTag> stringToEnumMap = new HashMap<>();
IconImageTag(String value) {
this.value = value;
stringToEnumMap.put(value, this);
}
Ideally, you'd build up a Map<String, IconImageTag> and add an appropriate method. For example:
public enum IconImageTag {
NONE("val1"),
USD("val2"),
EURO("val3");
private final String value;
private final Map<String, IconImageTag> valueMap = new HashMap<>();
static {
for (IconImageTag tag : values()) {
valueMap.put(tag.value, tag);
}
}
private IconImageTag(String value) {
this.value = value;
}
public static IconImageTag fromValue(String value) {
return valueMap.get(value);
}
}
(I'd probably use a different term from "value" here, to avoid confusion with valueOf() etc...)
Note the use of the static initializer block - any static variables in an enum are initialized after the enum values themselves, which means that when the enum constructor runs, valueMap will still be null.
You can also iterate over every enum.
public enum IconImageTag {
None("val1"),
USD("val2"),
EURO("val3");
private final String value;
private IconImageTag(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static IconImageTag getByValue(String value) {
for(IconImageTag iconImageTag : values()) {
if(iconImageTag.getValue().equals(value)) {
return iconImageTag;
}
}
return null;
}

How can I declare enums using java

I want to convert this sample C# code into a java code:
public enum myEnum {
ONE = "one",
TWO = "two",
};
Because I want to change this constant class into enum
public final class TestConstants {
public static String ONE = "one";
public static String TWO= "two";
}
public enum MyEnum {
ONE(1),
TWO(2);
private int value;
private MyEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
In short - you can define any number of parameters for the enum as long as you provide constructor arguments (and set the values to the respective fields)
As Scott noted - the official enum documentation gives you the answer. Always start from the official documentation of language features and constructs.
Update: For strings the only difference is that your constructor argument is String, and you declare enums with TEST("test")
enums are classes in Java. They have an implicit ordinal value, starting at 0. If you want to store an additional field, then you do it like for any other class:
public enum MyEnum {
ONE(1),
TWO(2);
private final int value;
private MyEnum(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
Quite simply as follows:
/**
* #author The Elite Gentleman
*
*/
public enum MyEnum {
ONE("one"), TWO("two")
;
private final String value;
private MyEnum(final String value) {
this.value = value;
}
public String getValue() {
return value;
}
#Override
public String toString() {
// TODO Auto-generated method stub
return getValue();
}
}
For more info, visit Enum Types from Oracle Java Tutorials. Also, bear in mind that enums have private constructor.
Update, since you've updated your post, I've changed my value from an int to a String.
Related: Java String enum.
Well, in java, you can also create a parameterized enum. Say you want to create a className enum, in which you need to store classCode as well as className, you can do that like this:
public enum ClassEnum {
ONE(1, "One"),
TWO(2, "Two"),
THREE(3, "Three"),
FOUR(4, "Four"),
FIVE(5, "Five")
;
private int code;
private String name;
private ClassEnum(int code, String name) {
this.code = code;
this.name = name;
}
public int getCode() {
return code;
}
public String getName() {
return name;
}
}
public enum MyEnum
{
ONE(1),
TWO(2);
private int value;
private MyEnum(int val){
value = val;
}
public int getValue(){
return value;
}
}
public enum NewEnum {
ONE("test"),
TWO("test");
private String s;
private NewEnum(String s) {
this.s = s);
}
public String getS() {
return this.s;
}
}

Can I set enum start value in Java?

I use the enum to make a few constants:
enum ids {OPEN, CLOSE};
the OPEN value is zero, but I want it as 100. Is it possible?
Java enums are not like C or C++ enums, which are really just labels for integers.
Java enums are implemented more like classes - and they can even have multiple attributes.
public enum Ids {
OPEN(100), CLOSE(200);
private final int id;
Ids(int id) { this.id = id; }
public int getValue() { return id; }
}
The big difference is that they are type-safe which means you don't have to worry about assigning a COLOR enum to a SIZE variable.
See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more.
Yes. You can pass the numerical values to the constructor for the enum, like so:
enum Ids {
OPEN(100),
CLOSE(200);
private int value;
private Ids(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
See the Sun Java Language Guide for more information.
whats about using this way:
public enum HL_COLORS{
YELLOW,
ORANGE;
public int getColorValue() {
switch (this) {
case YELLOW:
return 0xffffff00;
case ORANGE:
return 0xffffa500;
default://YELLOW
return 0xffffff00;
}
}
}
there is only one method ..
you can use static method and pass the Enum as parameter
like:
public enum HL_COLORS{
YELLOW,
ORANGE;
public static int getColorValue(HL_COLORS hl) {
switch (hl) {
case YELLOW:
return 0xffffff00;
case ORANGE:
return 0xffffa500;
default://YELLOW
return 0xffffff00;
}
}
Note that these two ways use less memory and more process units .. I don't say this is the best way but its just another approach.
If you use very big enum types then, following can be useful;
public enum deneme {
UPDATE, UPDATE_FAILED;
private static Map<Integer, deneme> ss = new TreeMap<Integer,deneme>();
private static final int START_VALUE = 100;
private int value;
static {
for(int i=0;i<values().length;i++)
{
values()[i].value = START_VALUE + i;
ss.put(values()[i].value, values()[i]);
}
}
public static deneme fromInt(int i) {
return ss.get(i);
}
public int value() {
return value;
}
}
If you want emulate enum of C/C++ (base num and nexts incrementals):
enum ids {
OPEN, CLOSE;
//
private static final int BASE_ORDINAL = 100;
public int getCode() {
return ordinal() + BASE_ORDINAL;
}
};
public class TestEnum {
public static void main (String... args){
for (ids i : new ids[] { ids.OPEN, ids.CLOSE }) {
System.out.println(i.toString() + " " +
i.ordinal() + " " +
i.getCode());
}
}
}
OPEN 0 100
CLOSE 1 101
The ordinal() function returns the relative position of the identifier in the enum. You can use this to obtain automatic indexing with an offset, as with a C-style enum.
Example:
public class TestEnum {
enum ids {
OPEN,
CLOSE,
OTHER;
public final int value = 100 + ordinal();
};
public static void main(String arg[]) {
System.out.println("OPEN: " + ids.OPEN.value);
System.out.println("CLOSE: " + ids.CLOSE.value);
System.out.println("OTHER: " + ids.OTHER.value);
}
};
Gives the output:
OPEN: 100
CLOSE: 101
OTHER: 102
Edit: just realized this is very similar to ggrandes' answer, but I will leave it here because it is very clean and about as close as you can get to a C style enum.
#scottf
An enum is like a Singleton. The JVM creates the instance.
If you would create it by yourself with classes it could be look like that
public static class MyEnum {
final public static MyEnum ONE;
final public static MyEnum TWO;
static {
ONE = new MyEnum("1");
TWO = new MyEnum("2");
}
final String enumValue;
private MyEnum(String value){
enumValue = value;
}
#Override
public String toString(){
return enumValue;
}
}
And could be used like that:
public class HelloWorld{
public static class MyEnum {
final public static MyEnum ONE;
final public static MyEnum TWO;
static {
ONE = new MyEnum("1");
TWO = new MyEnum("2");
}
final String enumValue;
private MyEnum(String value){
enumValue = value;
}
#Override
public String toString(){
return enumValue;
}
}
public static void main(String []args){
System.out.println(MyEnum.ONE);
System.out.println(MyEnum.TWO);
System.out.println(MyEnum.ONE == MyEnum.ONE);
System.out.println("Hello World");
}
}
public class MyClass {
public static void main(String args[]) {
Ids id1 = Ids.OPEN;
System.out.println(id1.getValue());
}
}
enum Ids {
OPEN(100), CLOSE(200);
private final int id;
Ids(int id) { this.id = id; }
public int getValue() { return id; }
}
#scottf, You probably confused because of the constructor defined in the ENUM.
Let me explain that.
When class loader loads enum class, then enum constructor also called. On what!! Yes, It's called on OPEN and close. With what values 100 for OPEN and 200 for close
Can I have different value?
Yes,
public class MyClass {
public static void main(String args[]) {
Ids id1 = Ids.OPEN;
id1.setValue(2);
System.out.println(id1.getValue());
}
}
enum Ids {
OPEN(100), CLOSE(200);
private int id;
Ids(int id) { this.id = id; }
public int getValue() { return id; }
public void setValue(int value) { id = value; }
}
But, It's bad practice. enum is used for representing constants like days of week, colors in rainbow i.e such small group of predefined constants.
I think you're confused from looking at C++ enumerators. Java enumerators are different.
This would be the code if you are used to C/C++ enums:
public class TestEnum {
enum ids {
OPEN,
CLOSE,
OTHER;
public final int value = 100 + ordinal();
};
public static void main(String arg[]) {
System.out.println("OPEN: " + ids.OPEN.value);
System.out.println("CLOSE: " + ids.CLOSE.value);
System.out.println("OTHER: " + ids.OTHER.value);
}
};

Categories

Resources