enum understand


Description:
Enum in java is a data type that contains fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The java enum constants are static and final implicitly. It is available from JDK 1.5.

Java Enums can be thought of as classes that have fixed set of constants.
  • enum improves type safety
  • enum can be easily used in switch
  • enum can have fields, constructors and methods
  • enum can be traversed
  • enum may implement many interfaces but cannot extend any class because it internally extends Enum class
Step 1:
http://www.javatpoint.com/enum-in-java



Code 1:
?
public enum EnumMethod{
SESSION("session", "AES", "AES/CBC/PKCS5Padding"), 
TOKEN("token", "AES","AES/CBC/PKCS5Padding"), 
CONFIG("mongoservice_config", "AES","AES/CBC/PKCS5Padding"),
SUMMARY("summary", "AES","AES/CBC/PKCS5Padding"),
DOCUMENT("document", "AES","AES/CBC/PKCS5Padding");
private String transformation;
private String role;
private String algorithm;

private EnumMethod(String role, String algorithm,String transformation) {
this.transformation = transformation;
this.role = role;
this.algorithm = algorithm;
}

public String transformation() {
return transformation;
}

public String role() {
return role;
}

public String algorithm() {
return algorithm;
}
}


How to use code 1:
where you need to use just call that variable after then you can use 
Example A:
   EnumMethod enumMethodSession =  EnumMethod.SESSION;
Example B:
         EnumMethod enumMethodToken =  EnumMethod.TOKEN;

Step 2:
XMl to Java object and java object to xml creation using JAXB
So download
jaxb-api-2.2 jar

Code 2: 
?

public enum CredentialStatus {
SCRAM_SHA_1 {
@Override
public String getName() {
return "ScramSha1";
}
},
MONGO_CR {
@Override
public String getName() {
return "MongoCR";
}
},
PLAIN {
@Override
public String getName() {
return "Plain";
}
},
GSSAPI {
@Override
public String getName() {
return "GSSAPI";
}
},
MONGO_X509 {
@Override
public String getName() {
return "MongoX509";
}
},
NONE {
@Override
public String getName() {
return "NONE";
}
};
/**
* @return getName of Enum class method
*/
public abstract String getName();

}


How to use code 2:
where you need to use just call that variable after then you can use 
Example A:
   String scrm =  CredentialStatus.SCRAM_SHA_1.getName();
Example B:
         String mongoCr = CredentialStatus.MONGO_CR.getName();

Previous
Next Post »