I have enum like:
public enum Enum2
{
ONE,TWO,THREE;
}
I can list all values like:
public static void main(String... args)
{
for (Enum2 e : Enum2.values())
{
System.out.println(e);
}
}
Is it possible list values if I have only string name of Enum?
String enum_name="Enum2";
E.g. if in some logic like:
if (a>b)
{
enum_name="EnumA";
}
else
{
enum_name="EnumB";
}
And after I receive string name of enum - I can list all values.
Class<?> enumClazz = Class.forName("com.mycompany.Enum2");
for (Enum<?> e : ((Class<? extends Enum<?>>)enumClazz).getEnumConstants()) {
System.out.println(e.name()); // The variable "e" would be Enum2.ONE, etc
}
Thank you #Harry for helping me get this right.
Your question is not much clear to be but this is what you may want to do
Class<?> cls = Class.forName("EnumName");
if (cls.isEnum()) {
Field[] flds = cls.getDeclaredFields();
//-- your logic for fields.
}
You can use: Class.getEnumConstants(). For more see this.
yes, with
Enum2.EnumA.toString();
Related
How to get value by Java enum class name and field name?
The sample code is as follows, but I don't know how to pass the enum class as a parameter.
public enum ErrorCodes1{
OK(0),
NOT_EXIST_USER(1),
FAIL_TO_SEND_MAIL(2),
...
}
public enum ErrorCodes2{
OK(0),
NOT_EXIST_USER(1),
FAIL_TO_SEND_MESSA(2),
...
}
public void foo1()
{
foo2(ErrorCodes1.class, "NOT_EXIST_USER");
foo2(ErrorCodes2.class, "NOT_EXIST_USER");
}
public void foo2(Enum EnumClass, String EnumText)
{
int code = xxxx; //I want to get code(1) via EnumText and EnumClass, but I don't know how to do it.
}
You can use the type Class as a param of your function foo2
public static void foo2(Class<?> enumClass, String enumText) {
int code = -1; // I want to get code(1) via EnumText and EnumClass, but I don't know how to do
// it.
switch (enumClass.getCanonicalName()) {
case "ErrorCodes2": {
ErrorCodes1 errorCode = ErrorCodes1.valueOf(enumText);
code = errorCode.ordinal();
}
case "ErrorCodes1": {
ErrorCodes1 errorCode = ErrorCodes1.valueOf(enumText);
code = errorCode.ordinal();
}
}
System.out.println(code);
}
After that, you can use valueOf to instantiate your enum from a string that contains the enum value. I don't think this is the best solution but its works.
PS: The param of a function begins with a lowercase letter.
Option A, simple: check for the type explicitly.
int code;
if (enumClass instanceof ErrorCodes1) {
code = ((ErrorCodes1) enumClass).valueOf(enumText).ordinal();
} else if (enumClass instanceof ErrorCodes2)
// repeat
This implies you can pass an error object itself, not necessarily its class. Unfortunately, switch does not work with Class type.
Option B, probably overkill: use Reflection.
public void foo2(Class<?> enumClass, String enumText) {
try{
Object resultingEnum = enumClass.getMethod("valueOf", String.class).invoke(null, enumText);
int code = (Integer) resultingEnum.getClass().getMethod("ordinal").invoke(resultingEnum);
// ...
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
// ignore: enumClass is not an expected class
}
}
This will suit you in case you have an unlimited number of ErrorCode enums, though it is a barely plausible situation.
I have task to change this if:
if (userDecision.equalsIgnoreCase("D")) {
return DirectoriesActivity.DELETE;
} else if (userDecision.equalsIgnoreCase("R")) {
return DirectoriesActivity.REPLACE;
} else {
return DirectoriesActivity.ADD_NEW_CONTENTS;
}
On something what will return just enum without using if. I have to use some enum properties but I don't know which one :/ Here is my enum:
public enum DirectoriesActivity {
DELETE,
REPLACE,
ADD_NEW_CONTENTS;
}
I tried to do something like this:
public enum DirectoriesActivity {
DELETE ("D"),
REPLACE ("R"),
ADD_NEW_CONTENTS ("A");
private String directoriesActivityCode;
private DirectoriesActivity(String directoriesActivityCode) {
this.directoriesActivityCode = directoriesActivityCode;
}
public DirectoriesActivity getEnum(String x){
//no idea what to do here
}
}
Or maybe somebody have some other idea?
You can add a Map lookup.
static Map<String, DirectoriesActivity> lookup = new HashMap<>();
static {
// iterate over all the values and
// put the value we want to lookup as the key to the map.
for(DirectoriesActivity da: values())
lookup.put(da.directoriesActivitCode, da);
}
public static DirectoriesActivity lookup(String s) {
// lookup the map we built in the static block.
return s == null ? null : lookup.get(s.toUppercase());
}
This way you can add as many codes as you want without having to change the code.
How about this:
public enum DirectoriesActivity {
DELETE ("D"),
REPLACE ("R"),
ADD_NEW_CONTENTS ("A");
private String directoriesActivityCode;
private DirectoriesActivity(String directoriesActivityCode) {
this.directoriesActivityCode = directoriesActivityCode;
}
public DirectoriesActivity getEnum(String x){
for (DirectoriesActivity directoriesActivity : values()) {
if (directoriesActivity.directoriesActivityCode.equals(x)) {
return directoriesActivity;
}
}
throw new IllegalArgumentException("Unknown value " + x);
}
}
Or in case you are using Java 8
return Arrays.stream(DirectoriesActivity.values())
.filter(directoriesActivity -> directoriesActivity.directoriesActivityCode.equals(userDecision))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown value " + userDecision));
Important side note here is that this solution is performing a lot worse than the solution provided by Peter. But as long as performance is not an issue, I'd prefer a solution like this.
If you can change enum names to D, R, A then you can use built-in feature:
public enum DirectoriesActivity {D,R,A}
DirectoriesActivity activity = DirectoriesActivity.valueOf("D");
valueOf throws IllegalArgumentException if string is not match.
String activityState = "resume";
DebugLog(activityState)
void DebugLog(String obj1) {}
How to make the DebugLog to print like this:
activityState : resume
I used to write many print statement as logs at many places while debugging. I will write statements like
System.out.println("activityState : " + activityState);
I want a method to print the variable name and variable value. In C++, it can be done like the below:
#define dbg(x) cout<< #x <<" --> " << x << endl ;
Is there any way to do this?
Thanks in advance.
There's no direct solution to get the variable name.
However, in a context where you have many fields and don't want to manually print their state, you can use reflection.
Here's a quick example:
class MyPojo {
public static void main(String[] args) {
System.out.println(new MyPojo());
}
int i = 1;
String s = "foo";
#Override
public String toString() {
StringBuilder result = new StringBuilder();
for (Field f: getClass().getDeclaredFields()) {
try {
result
.append(f.getName())
.append(" : ")
.append(f.get(this))
.append(System.getProperty("line.separator"));
}
catch (IllegalStateException ise) {
result
.append(f.getName())
.append(" : ")
.append("[cannot retrieve value]")
.append(System.getProperty("line.separator"));
}
// nope
catch (IllegalAccessException iae) {}
}
return result.toString();
}
}
Output
i : 1
s : foo
You can use java Reflection to get the variable name and the value. Here's an example code;
public class Example{
String activityState = "resume";
public static void main(String[] args) {
Example example = new Example();
Class<?> c = example.getClass();
Field field = c.getDeclaredField("activityState");
System.out.println(field.getName());
System.out.println(field.get(example));
}
}
Since this is for debugging you could use instrumentation with aspectj, your code remains clean from the the debugging output statements and you can waeve the aspect as needed.
Define a set(FieldPattern) point cut to catch all field assignements (join points)
public aspect TestAssignmentAspect {
pointcut assigmentPointCut() : set(* *);
after() : assigmentPointCut() {
System.out.printf("%s = %s%n", thisJoinPoint.getSignature().getName(),
String.valueOf(Arrays.toString(thisJoinPoint.getArgs())));
}
}
Here is Test class
public class Test {
public static String activityState = "stopped";
public static void main(String[] args) {
activityState = "start";
doSomething();
activityState = "pause";
doSomeOtherthing();
activityState = "resume";
System.out.printf("the end!%n");
}
private static void doSomeOtherthing() {
System.out.printf("doing some other thing...%n");
}
private static void doSomething() {
System.out.printf("doing something...%n");
}
}
If you run this example with the aspect weaved the output will be
activityState = [stopped]
activityState = [start]
doing something...
activityState = [pause]
doing some other thing...
activityState = [resume]
the end!
Explanation
pointcut assigmentPointCut() : set(* *);
set point cut to catch assignments, the point joins, to any variable with any name, could also in the example be
pointcut assigmentPointCut() : set(String activityState);
The advice, the desired behavior when the given point cut matches
after() : assigmentPointCut() { ... }
Informations about the point join can be accessed using the special reference thisJoinPoint.
I have a class that i'm uses a generic Type that extends the interface zwave
everything is fine until i try to access a zwave variable for some reason the rm.keyword gives a "NullPointerException". if I cast it to the class scene it works, but that is not what I want
public <T extends zwave> T Find(List<T> Zwave,List<List<String>> listofinputstrings)
{
for(List<String> lst: listofinputstrings)
{
for(String str: lst)
{
for (T rm: Zwave)
{
//*** problem is here
//rm.keyword is always gives a NullPointerException unless i cast it to a class
if (rm.keyword.equals( str.toLowerCase()))
{
return rm;
}
}
}
}
return null;
}
//here is the interface
interface zwave
{
public String keyword="";
public String zwaveID="";
}
//here is a class that implements the interface
public class Scene implements zwave
{
String name;
String keyword;
String zwaveID;
public Scene(String Name,String Keyword,String ZwaveID)
{
name= Name;
zwaveID= ZwaveID;
keyword = Keyword;
}
}
edit
Working code
//search class
public <T extends searchable> T Find(List<T> searchableclasses, List<List<String>> listofinputstrings)
{
for(List<String> lst: listofinputstrings)
{
for(String str: lst)
{
for (T searchable: searchableclasses)
{
for(String key: searchable.GetKeywords())
{
if ( key.equals(str.toLowerCase()))
{
return searchable;
}
}
}
}
}
return null;
}
//abstract class
abstract class searchable
{
String[] keywords; //using array so i can use java's param ability
public List<String> GetKeywords()
{
return new ArrayList(Arrays.asList(keywords));
}
}
//actual class
public class Scene extends searchable
{
String name;
String zwaveID;
public Scene(String Name,String ZwaveID,String... Keywords)
{
name= Name;
zwaveID= ZwaveID;
keywords = Keywords;
}
}
If you don't wanna cast you can do some thing like this:
public <T extends zwave> T Find(List<T> Zwave,List<List<String>> listofinputstrings)
{
for(List<String> lst: listofinputstrings)
{
for(String str: lst)
{
for (T rm: Zwave)
{
if(rm instanceof Scene){
Method method=null;
try {
method = rm.getClass().getMethod("getKeyword");
if ( method.invoke(rm).equals( str.toLowerCase()))
{
return rm;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
NOte:define getKeyword method in Scene class:
I can customize it more with the help of java.lang.reflect. You would not even need to use instance of Scene. But I think you can do it yourself. And hope it will help.
use Reflection API to call at run time.
You need to be using a getter method. When you say rm.keyword, that's referring to a constant (zwave.keyword), which is the empty string. When you cast to Scene, the compiler sees that it's a field and looks it up instead.
Generally, you should make fields like name and keyword private unless you have a specific reason not to and use getter and setter methods to manipulate them.
The variables defined in the interface are final static public even though you didn't explicitly define. When the variable is final, once the value is assigned you cannot reassign it again.
Since you have defined as empty string ("") it will take that value. But you define the variable again in Scene class. So when you cast to Scene object will refer this variable and not the variable in the interface. Otherwise it refers to interface variable.
I have a string (which is a message) that I get as input and I need to do one of 4 possible things depending on the string
I know that there is eunm.valueOf() option, but I have 4 different enums, each with few possible messages.
looks something like:
public enum first{ONE,TWO,THREE};
public enum second{FOUR,FIVE,SIX};
public enum third{SEVEN,EIGHT,NINE};
public void work(String message){
//Here I want to compare message string to one of the 3 enums
}
is it possible to do this in one method of the enum?
or should I just try to create one, and if I get an exception try the other and so on?
As others have commented, it may be better to think through whether you really need 4 distinct enums.
But if you do, you could have them implement a common interface. Then you can map the input strings to the appropriate enum member, and call its method to accomplish what you want. Something like
public interface SomeInterface {
void doSomething();
};
public enum First implements SomeInterface {
ONE,TWO,THREE;
#Override
public void doSomething() { ... }
};
...
Map<String, SomeInterface> myMap = new HashMap<String, SomeInterface>();
for (First item : First.values()) {
myMap.put(item.toString(), item);
}
...
public void work(String message){
SomeInterface obj = myMap.get(message);
if (obj != null) {
obj.doSomething();
}
}
This assumes that the 4 possible things you want to do correspond to the 4 enums. If not, you can override the method separately for each and any enum member too, e.g.
public enum First implements SomeInterface {
ONE,
TWO {
#Override
public void doSomething() { // do something specific to TWO }
},
THREE;
#Override
public void doSomething() { // general solution for all values of First }
};
Enumerations in Java are full blown classes. Individual values can even override the behavior to meet their needs. It's pretty cool. You can use this to your advantage:
public enum Value implements Worker
{
ONE,
TWO,
THREE
{
#Override
public void doWork(String message)
{
// overrides behavior of base enum
}
},
FOUR,
/* ... */,
NINE;
private final String message;
Value() { this(""); }
Value(String message) { this.message = message; }
public void doWork(String message)
{
if (this.message.equals(message))
{
/* ... */
}
}
}
public interface Worker
{
void doWork(String message);
}
You can create a Map of them all
static final Map<String, Enum> enumMap = new LinkedHashMap<String, Enum>(){{
for(First e: First.values()) put(e.name(), e);
for(Second e: Second.values()) put(e.name(), e);
for(Third e: Third.values()) put(e.name(), e);
}};
Enum e = enumMap.get(name);
What you're really looking for is a aggregation of the other enums. The easiest way to get that is to make a new enum that puts all of those choices in a new enum. Something to this effect:
public enum Combination {
NEWONE(first.ONE), NEWTWO(first.TWO), NEWTHREE(first.THREE),
NEWFOUR(second.FOUR), NEWFIVE(second.FIVE), NEWSIX(second.SIX),
NEWSEVEN(third.SEVEN), NEWEIGHT(third.EIGHT), NEWNINE(third.NINE);
private String contents;
public Combination(first f) {
contents = f.toString();
}
public Combination(second s) {
contents = s.toString();
}
public Combination(third t) {
contents = t.toString();
}
public String toString() {
return contents;
}
}
This will more correctly aggregate the previous enums into a single data structure.
Even given your odd/even example in the comments, I don't feel multiple enums are the way to go here. I would use something like (warning, untested):
public enum Numbers {
ONE("first"), TWO("first"), THREE("first"), FOUR("second"), FIVE("second"), SIX("second"), SEVEN("third"), EIGHT("third"), NINE("third")
private String type;
Numbers(String t) { this.type = t; }
String getType { return this.type; }
}
Then you can use valueOf() to look up the enum element, and getType() to find out which of your three categories it belongs to.
It isn't entirely clear what you are asking, but perhaps you want to define a mapping between strings and constants, like this:
enum Type { FIRST, SECOND, THIRD };
Map<String, Type> mapping = new HashSet<String, Type>(){{
put("ONE", Type.FIRST);
put("TWO", Type.FIRST);
//...
put("NINE", Type.THIRD);
}};
public Type getTypeFromString(String s) {
return mapping.get(s);
}