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