/src/main/java/uk/ac/cam/ch/ami/AmiObject.java
https://bitbucket.org/jat45/ami · Java · 84 lines · 57 code · 18 blank · 9 comment · 11 complexity · 5a56a78ffc0afea41e347c45fba19c83 MD5 · raw file
- package uk.ac.cam.ch.ami;
-
- import com.thoughtworks.xstream.annotations.XStreamOmitField;
-
- import javax.swing.tree.DefaultMutableTreeNode;
- import java.util.Comparator;
- import java.util.Map;
- import java.util.TreeMap;
-
- /**
- * Created by IntelliJ IDEA.
- * User: Matthew
- * Date: 07-Sep-2010
- * Time: 16:30:02
- * To change this template use File | Settings | File Templates.
- */
- public class AmiObject {
-
- public static AmiObject EQUIPMENT = new AmiObject("Equipment");
-
- @XStreamOmitField
- Comparator treeComparator = new CustomEquipmentTreeComparator();
-
- // we'll create cache only when needed (in register()) to avoid superfluous empty caches
- // in the serialized form - most of the objects in the tree are leaf nodes without alpha cache
- private Map<String, AmiObject> cache = null;
-
- private final String name;
-
- private AmiObject(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public String toString() {
- return name;
- }
-
- public AmiObject register(String name) throws IllegalArgumentException {
- if(cache == null) { cache = new TreeMap<String, AmiObject>(treeComparator); }
- if (cache.containsKey(name)) {
- throw new IllegalArgumentException("Duplicate name " + name);
- }
- AmiObject amiObject = new AmiObject(name);
- cache.put(name, amiObject);
- return amiObject;
- }
-
- public AmiObject valueOf(String name) {
- return cache.get(name);
- }
-
- public AmiObject[] values() {
- return cache.values().toArray(new AmiObject[cache.size()]);
- }
-
- public boolean hasChildren(){
- if(cache == null) { return false; }
- if(this.values().length != 0){
- return true;
- } else{
- return false;
- }
- }
-
- public static void clearCache(){
- if(EQUIPMENT.cache != null) { EQUIPMENT.cache.clear(); }
- }
-
- public static DefaultMutableTreeNode buildTreeFrom(AmiObject o) {
- DefaultMutableTreeNode node=new DefaultMutableTreeNode(o);
- if(o.hasChildren()) {
- for(AmiObject amiObject : o.values()) {
- node.add(buildTreeFrom(amiObject));
- }
- }
-
- return node;
- }
-
- }