1 11 12 package org.eclipse.ui.internal.activities; 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 public final class CategoryDefinition implements Comparable { 23 private final static int HASH_FACTOR = 89; 24 25 private final static int HASH_INITIAL = CategoryDefinition.class.getName() 26 .hashCode(); 27 28 static Map categoryDefinitionsById(Collection categoryDefinitions, 29 boolean allowNullIds) { 30 if (categoryDefinitions == null) { 31 throw new NullPointerException (); 32 } 33 34 Map map = new HashMap (); 35 Iterator iterator = categoryDefinitions.iterator(); 36 37 while (iterator.hasNext()) { 38 Object object = iterator.next(); 39 Util.assertInstance(object, CategoryDefinition.class); 40 CategoryDefinition categoryDefinition = (CategoryDefinition) object; 41 String id = categoryDefinition.getId(); 42 43 if (allowNullIds || id != null) { 44 map.put(id, categoryDefinition); 45 } 46 } 47 48 return map; 49 } 50 51 static Map categoryDefinitionsByName(Collection categoryDefinitions, 52 boolean allowNullNames) { 53 if (categoryDefinitions == null) { 54 throw new NullPointerException (); 55 } 56 57 Map map = new HashMap (); 58 Iterator iterator = categoryDefinitions.iterator(); 59 60 while (iterator.hasNext()) { 61 Object object = iterator.next(); 62 Util.assertInstance(object, CategoryDefinition.class); 63 CategoryDefinition categoryDefinition = (CategoryDefinition) object; 64 String name = categoryDefinition.getName(); 65 66 if (allowNullNames || name != null) { 67 Collection categoryDefinitions2 = (Collection ) map.get(name); 68 69 if (categoryDefinitions2 == null) { 70 categoryDefinitions2 = new HashSet (); 71 map.put(name, categoryDefinitions2); 72 } 73 74 categoryDefinitions2.add(categoryDefinition); 75 } 76 } 77 78 return map; 79 } 80 81 private transient int hashCode = HASH_INITIAL; 82 83 private String id; 84 85 private String name; 86 87 private String sourceId; 88 89 private transient String string; 90 91 private String description; 92 93 public CategoryDefinition(String id, String name, String sourceId, 94 String description) { 95 this.id = id; 96 this.name = name; 97 this.sourceId = sourceId; 98 this.description = description; 99 } 100 101 public int compareTo(Object object) { 102 CategoryDefinition castedObject = (CategoryDefinition) object; 103 int compareTo = Util.compare(id, castedObject.id); 104 105 if (compareTo == 0) { 106 compareTo = Util.compare(name, castedObject.name); 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 CategoryDefinition)) { 118 return false; 119 } 120 121 final CategoryDefinition castedObject = (CategoryDefinition) object; 122 if (!Util.equals(id, castedObject.id)) { 123 return false; 124 } 125 126 if (!Util.equals(name, castedObject.name)) { 127 return false; 128 } 129 130 return Util.equals(sourceId, castedObject.sourceId); 131 } 132 133 public String getId() { 134 return id; 135 } 136 137 public String getName() { 138 return name; 139 } 140 141 public String getSourceId() { 142 return sourceId; 143 } 144 145 public int hashCode() { 146 if (hashCode == HASH_INITIAL) { 147 hashCode = hashCode * HASH_FACTOR + Util.hashCode(id); 148 hashCode = hashCode * HASH_FACTOR + Util.hashCode(name); 149 hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId); 150 if (hashCode == HASH_INITIAL) { 151 hashCode++; 152 } 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(sourceId); 167 stringBuffer.append(']'); 168 string = stringBuffer.toString(); 169 } 170 171 return string; 172 } 173 174 public String getDescription() { 175 return description; 176 } 177 } 178 | Popular Tags |