Skip to content

Instantly share code, notes, and snippets.

@smyykb
Last active November 17, 2017 08:22
Show Gist options
  • Save smyykb/5d8b41a0e6a8281973cda06677cfcf43 to your computer and use it in GitHub Desktop.
Save smyykb/5d8b41a0e6a8281973cda06677cfcf43 to your computer and use it in GitHub Desktop.
Enumsal Bişiler (Note : Just For Self-reference)
- Tanımlama:
public enum UserStatus {
PENDING,
ACTIVE,
INACTIVE,
DELETED;
}
- Kullanım:
UserStatus.PENDING
- Tanımlama:
public enum UserStatus {
PENDING(0,"User is pending"),
ACTIVE(1,"User is active"),
INACTIVE(2,"User is inactive"),
DELETED(3,"User is deleted");
private String message;
priavte int index;
UserStatus(String message, int index){
this.message = message;
this.index = index;
}
public String message(){
return this.message;
}
public int index(){
return this.index;
}
}
- Kullanım:
UserStatus.PENDING.message()
public enum Operation {
PLUS,
MINUS,
TIMES,
DIVIDE;
double calculate(double x, double y) {
switch (this) {
case PLUS:
return x + y;
case MINUS:
return x - y;
case TIMES:
return x * y;
case DIVIDE:
return x / y;
default:
throw new AssertionError("Unknown operations " + this);
}
}
}
- Kullanımı:
double result = Operation.PLUS.calculate(1, 2);
System.out.println(result); //3.0
public enum UserStatus {
PENDING,
ACTIVE,
INACTIVE,
DELETED;
}
for (UserStatus status : UserStatus.values()) {
System.out.println(status);
}
- OUTPUT:
PENDING
ACTIVE
INACTIVE
DELETED
UserStatus status = UserStatus.PENDING;
if(status == UserStatus.PENDING) {
System.out.println("Now : " + status.message());
}
------
switch(status){
case PENDING:
//...
break;
}
- String To Enum
Operation op = Operation.valueOf("times".toUpperCase());
System.out.println(op.calculate(10, 3));
- Enum To String
String operationStr = Operation.TIMES.name(); //TIMES
int order = Operation.TIMES.ordinal(); //2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment