Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package ru.ya.test;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import static java.util.Collections.unmodifiableList;
  8.  
  9. /**
  10. */
  11. public final class Currency {
  12. private static int idx = 0;
  13.  
  14. private final int ordinal;
  15. private String name;
  16.  
  17. private Currency(String name) {
  18. this.ordinal = idx++;
  19.  
  20. items.add(ordinal, this);
  21. }
  22.  
  23. private static final List<Currency> items = new ArrayList<>();
  24.  
  25. public static final Currency RUR = new Currency("RUR");
  26.  
  27. public static final Currency USD = new Currency("USD");
  28.  
  29. public static final Currency EUR = new Currency("EUR");
  30.  
  31. static {
  32. final Field[] fields = Currency.class.getDeclaredFields();
  33. for (Field field : fields) {
  34. if (field.getModifiers() != static... || field.getType() != Currency.class)
  35. continue;
  36.  
  37. String name = field.getName();
  38. }
  39. }
  40.  
  41. public static List<Currency> items() {
  42. return unmodifiableList(items);
  43. }
  44.  
  45. public static Currency item(int ordinal) {
  46. return items.get(ordinal);
  47. }
  48.  
  49. public static Currency byName(String name) {
  50. for (Currency currency : items)
  51. if (currency.name.equals(name))
  52. return currency;
  53.  
  54. return null;
  55. }
  56. }
Add Comment
Please, Sign In to add comment