1 11 12 package org.eclipse.ui.internal.contexts; 13 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.ui.internal.util.Util; 21 22 final class ContextDefinition implements Comparable { 23 private final static int HASH_FACTOR = 89; 24 private final static int HASH_INITIAL = 25 ContextDefinition.class.getName().hashCode(); 26 27 static Map contextDefinitionsById( 28 Collection contextDefinitions, 29 boolean allowNullIds) { 30 if (contextDefinitions == null) 31 throw new NullPointerException (); 32 33 Map map = new HashMap (); 34 Iterator iterator = contextDefinitions.iterator(); 35 36 while (iterator.hasNext()) { 37 Object object = iterator.next(); 38 Util.assertInstance(object, ContextDefinition.class); 39 ContextDefinition contextDefinition = (ContextDefinition) object; 40 String id = contextDefinition.getId(); 41 42 if (allowNullIds || id != null) 43 map.put(id, contextDefinition); 44 } 45 46 return map; 47 } 48 49 static Map contextDefinitionsByName( 50 Collection contextDefinitions, 51 boolean allowNullNames) { 52 if (contextDefinitions == null) 53 throw new NullPointerException (); 54 55 Map map = new HashMap (); 56 Iterator iterator = contextDefinitions.iterator(); 57 58 while (iterator.hasNext()) { 59 Object object = iterator.next(); 60 Util.assertInstance(object, ContextDefinition.class); 61 ContextDefinition contextDefinition = (ContextDefinition) object; 62 String name = contextDefinition.getName(); 63 64 if (allowNullNames || name != null) { 65 Collection contextDefinitions2 = (Collection ) map.get(name); 66 67 if (contextDefinitions2 == null) { 68 contextDefinitions2 = new HashSet (); 69 map.put(name, contextDefinitions2); 70 } 71 72 contextDefinitions2.add(contextDefinition); 73 } 74 } 75 76 return map; 77 } 78 79 private transient int hashCode; 80 private transient boolean hashCodeComputed; 81 private String id; 82 private String name; 83 private String parentId; 84 private String sourceId; 85 private transient String string; 86 87 ContextDefinition( 88 String id, 89 String name, 90 String parentId, 91 String sourceId) { 92 this.id = id; 93 this.name = name; 94 this.parentId = parentId; 95 this.sourceId = sourceId; 96 } 97 98 public int compareTo(Object object) { 99 ContextDefinition castedObject = (ContextDefinition) object; 100 int compareTo = Util.compare(id, castedObject.id); 101 102 if (compareTo == 0) { 103 compareTo = Util.compare(name, castedObject.name); 104 105 if (compareTo == 0) { 106 compareTo = Util.compare(parentId, castedObject.parentId); 107 108 if (compareTo == 0) 109 compareTo = Util.compare(sourceId, castedObject.sourceId); 110 } 111 } 112 113 return compareTo; 114 } 115 116 public boolean equals(Object object) { 117 if (!(object instanceof ContextDefinition)) 118 return false; 119 120 ContextDefinition castedObject = (ContextDefinition) object; 121 boolean equals = true; 122 equals &= Util.equals(id, castedObject.id); 123 equals &= Util.equals(name, castedObject.name); 124 equals &= Util.equals(parentId, castedObject.parentId); 125 equals &= Util.equals(sourceId, castedObject.sourceId); 126 return equals; 127 } 128 129 public String getId() { 130 return id; 131 } 132 133 public String getName() { 134 return name; 135 } 136 137 public String getParentId() { 138 return parentId; 139 } 140 141 public String getSourceId() { 142 return sourceId; 143 } 144 145 public int hashCode() { 146 if (!hashCodeComputed) { 147 hashCode = HASH_INITIAL; 148 hashCode = hashCode * HASH_FACTOR + Util.hashCode(id); 149 hashCode = hashCode * HASH_FACTOR + Util.hashCode(name); 150 hashCode = hashCode * HASH_FACTOR + Util.hashCode(parentId); 151 hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId); 152 hashCodeComputed = true; 153 } 154 155 return hashCode; 156 } 157 158 public String toString() { 159 if (string == null) { 160 final StringBuffer stringBuffer = new StringBuffer (); 161 stringBuffer.append('['); 162 stringBuffer.append(id); 163 stringBuffer.append(','); 164 stringBuffer.append(name); 165 stringBuffer.append(','); 166 stringBuffer.append(parentId); 167 stringBuffer.append(','); 168 stringBuffer.append(sourceId); 169 stringBuffer.append(']'); 170 string = stringBuffer.toString(); 171 } 172 173 return string; 174 } 175 } 176 | Popular Tags |