001package jmri.jmrit.logixng;
002
003import java.util.*;
004import java.util.concurrent.CopyOnWriteArrayList;
005
006/**
007 * The category of expressions and actions.
008 * <P>
009 * It's used to group expressions or actions then the user creates a new
010 * expression or action.
011 * <P>
012 * This class is intended to be an Enum, but implemented as an abstract class
013 * to allow adding more categories later without needing to change this class.
014 * For example, external programs using JMRI as a lib might want to add their
015 * own categories.
016 *
017 * @author Daniel Bergqvist Copyright 2018
018 */
019public abstract class Category implements Comparable<Category> {
020
021    /**
022     * A item on the layout, for example turnout, sensor and signal mast.
023     */
024    public static final Item ITEM = new Item();
025
026    /**
027     * Common.
028     */
029    public static final Common COMMON = new Common();
030
031    /**
032     * Flow Control.
033     */
034    public static final FlowControl FLOW_CONTROL = new FlowControl();
035
036    /**
037     * Other things.
038     */
039    public static final Other OTHER = new Other();
040
041    /**
042     * Linux specific things.
043     */
044    public static final Linux LINUX = new Linux();
045
046    static {
047        // It's not often any item is added to this list so we use CopyOnWriteArrayList
048        _categories = new CopyOnWriteArrayList<>();
049        registerCategory(ITEM);
050        registerCategory(COMMON);
051        registerCategory(FLOW_CONTROL);
052        registerCategory(OTHER);
053        if (jmri.util.SystemType.isLinux()) {
054            registerCategory(LINUX);
055        }
056    }
057
058    /**
059     * Get all the registered Categories
060     * @return a list of categories
061     */
062    public static List<Category> values() {
063        return Collections.unmodifiableList(_categories);
064    }
065
066    /**
067     * Register a category
068     * @param category the category
069     */
070    public static void registerCategory(Category category) {
071        _categories.add(category);
072    }
073
074
075    private static final List<Category> _categories;
076
077    private final String _name;
078    private final String _description;
079    private final int _order;
080
081
082    protected Category(String name, String description, int order) {
083        _name = name;
084        _description = description;
085        _order = order;
086    }
087
088    public String name() {
089        return _name;
090    }
091
092    @Override
093    public final String toString() {
094        return _description;
095    }
096
097    public int order() {
098        return _order;
099    }
100
101    @Override
102    public boolean equals(Object o) {
103        if (o instanceof Category) {
104            Category c = (Category)o;
105            return _description.equals(c._description) && _name.equals(c._name);
106        }
107        return false;
108    }
109
110    @Override
111    public int hashCode() {
112        return _description.hashCode();
113    }
114
115    @Override
116    public int compareTo(Category c) {
117        if (_order < c.order()) return -1;
118        if (_order > c.order()) return 1;
119        return 0;
120    }
121
122
123    public static final class Item extends Category {
124
125        public Item() {
126            super("ITEM", Bundle.getMessage("CategoryItem"), 100);
127        }
128    }
129
130
131    public static final class Common extends Category {
132
133        public Common() {
134            super("COMMON", Bundle.getMessage("CategoryCommon"), 200);
135        }
136    }
137
138
139    public static final class FlowControl extends Category {
140
141        public FlowControl() {
142            super("FLOW_CONTROL", Bundle.getMessage("CategoryFlowControl"), 210);
143        }
144    }
145
146
147    public static final class Linux extends Category {
148
149        public Linux() {
150            super("LINUX", Bundle.getMessage("CategoryLinux"), 2000);
151        }
152    }
153
154
155    public static final class Other extends Category {
156
157        public Other() {
158            super("OTHER", Bundle.getMessage("CategoryOther"), Integer.MAX_VALUE);
159        }
160    }
161
162}