Created
June 10, 2016 15:55
-
-
Save mitallast/9a6f522d30f57b4ef5fee4dbe1bc8bec to your computer and use it in GitHub Desktop.
test2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public final class Currency { | |
| public static final Currency EU = new Currency("EU"); | |
| public static final Currency USD = new Currency("USD"); | |
| private final static List<Currency> values = new ArrayList<>(); | |
| private final int ordinal; | |
| private final String name; | |
| private Currency(String name) { | |
| ordinal = values.size(); | |
| values.add(this); | |
| this.name = name; | |
| } | |
| public int getOrdinal() { | |
| return ordinal; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) return true; | |
| if (o == null || getClass() != o.getClass()) return false; | |
| Currency currency = (Currency) o; | |
| return ordinal == currency.ordinal; | |
| } | |
| @Override | |
| public int hashCode() { | |
| return ordinal; | |
| } | |
| public static Currency findByName(String value) { | |
| for (Currency currency : values) { | |
| if (currency.getName().equals(value)) { | |
| return currency; | |
| } | |
| } | |
| throw new RuntimeException("Enum not present with name " + value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment