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