Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class Color {
- //
- private static final List<Color> colors = new ArrayList<>();
- public static final Color RED = new Color();
- public static final Color GREEN = new Color();
- private final int ordinal;
- private final String name;
- private Color() {
- this.ordinal = colors == null ? 0 : colors.size();
- this.name = Arrays.stream(this.getClass().getDeclaredFields()).filter(f -> f.getType()==Color.class).filter(f -> {
- try {
- return f.get(this) == this;
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- return false;
- }).map(Field::getName).findFirst().orElseThrow();
- colors.add(this);
- }
- public int getOrdinal() {
- return ordinal;
- }
- public String getName() {
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment