Skip to content

Instantly share code, notes, and snippets.

@t3hk0d3
Last active December 26, 2015 06:49
Show Gist options
  • Save t3hk0d3/7110513 to your computer and use it in GitHub Desktop.
Save t3hk0d3/7110513 to your computer and use it in GitHub Desktop.
written in browser, have no IDE there
//
protected enum IntervalType {
UNKNOWN(0),
MINUTE(60, "minute", "m"),
HOUR(3600, "hour", "h"),
DAY(86400, "day", "d"),
WEEK(604800, "week", "w"),
MONTH(2592000, "month"),
YEAR(31104000, "year");
private final int value;
private final String[] labels;
private IntervalType(int seconds, String... labels) {
// save into private final properties
this.value = seconds;
this.labels = labels;
}
public int value() {
return this.value;
}
public String[] labels() {
return this.labels;
}
public static IntervalType byLabel(String label) {
if(intervalMap.containsKey(label) {
return intervalMap.get(label);
} else {
return UNKNOWN;
}
}
private final static Map<String, IntervalType> intervalMap = new HashMap<String, IntervalType>();
static {
for(IntervalType type : IntervalType.values()) {
for(String label : type.labels()) {
intervalMap.put(label, type);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment