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