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;
}
Related
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;
}
}
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();
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
}
This question already has answers here:
Getting enum associated with int value
(8 answers)
Java getting the Enum name given the Enum Value
(7 answers)
Closed 9 years ago.
I have the following enum
public enum AppointmentSlotStatusType {
INACTIVE(0), ACTIVE(1);
private int value;
private AppointmentSlotStatusType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String getName() {
return name();
}
}
How do I get the enum name if a value is known for instance 1 ?
For this specific enum it's easy
String name = TimeUnit.values()[1].name();
You can implement a public static method inside the enum, which will give you the enum instance for that id:
public static AppointmentSlotStatusType forId(int id) {
for (AppointmentSlotStatusType type: values()) {
if (type.value == id) {
return value;
}
}
return null;
}
Probably you would also like to cache the array returned by values() in a field:
public static final AppointmentSlotStatusType[] VALUES = values();
then use VALUES instead of values().
Or you can use a Map instead.
private static final Map<Integer, AppointmentSlotStatusType> map = new HashMap<>();
static {
for (AppointmentSlotStatusType type: values()) {
map.put(type.value, type);
}
}
public static AppointmentSlotStatusType forId(int id) {
return map.get(id);
}
You can maintain a Map to hold name for Integer key.
public enum AppointmentSlotStatusType {
INACTIVE(0), ACTIVE(1);
private int value;
private static Map<Integer, AppointmentSlotStatusType> map = new HashMap<Integer, AppointmentSlotStatusType>();
static {
for (AppointmentSlotStatusType item : AppointmentSlotStatusType.values()) {
map.put(item.value, item);
}
}
private AppointmentSlotStatusType(final int value) { this.value = value; }
public static AppointmentSlotStatusType valueOf(int value) {
return map.get(value);
}
}
Take a look at this answer.
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;
}
}