1 11 12 package org.eclipse.ui.internal.activities; 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.ui.activities.IIdentifier; 20 import org.eclipse.ui.activities.IIdentifierListener; 21 import org.eclipse.ui.activities.IdentifierEvent; 22 import org.eclipse.ui.internal.util.Util; 23 24 final class Identifier implements IIdentifier { 25 private final static int HASH_FACTOR = 89; 26 27 private final static int HASH_INITIAL = Identifier.class.getName() 28 .hashCode(); 29 30 private final static Set strongReferences = new HashSet (); 31 32 private Set activityIds; 33 34 private transient String [] activityIdsAsArray; 35 36 private boolean enabled; 37 38 private transient int hashCode = HASH_INITIAL; 39 40 private String id; 41 42 private List identifierListeners; 43 44 private transient String string; 45 46 Identifier(String id) { 47 if (id == null) { 48 throw new NullPointerException (); 49 } 50 51 this.id = id; 52 } 53 54 public void addIdentifierListener(IIdentifierListener identifierListener) { 55 if (identifierListener == null) { 56 throw new NullPointerException (); 57 } 58 59 if (identifierListeners == null) { 60 identifierListeners = new ArrayList (); 61 } 62 63 if (!identifierListeners.contains(identifierListener)) { 64 identifierListeners.add(identifierListener); 65 } 66 67 strongReferences.add(this); 68 } 69 70 public int compareTo(Object object) { 71 Identifier castedObject = (Identifier) object; 72 int compareTo = Util.compare(activityIdsAsArray, 73 castedObject.activityIdsAsArray); 74 75 if (compareTo == 0) { 76 compareTo = Util.compare(enabled, castedObject.enabled); 77 78 if (compareTo == 0) { 79 compareTo = Util.compare(id, castedObject.id); 80 } 81 } 82 83 return compareTo; 84 } 85 86 public boolean equals(Object object) { 87 if (!(object instanceof Identifier)) { 88 return false; 89 } 90 91 final Identifier castedObject = (Identifier) object; 92 if (!Util.equals(activityIds, castedObject.activityIds)) { 93 return false; 94 } 95 96 if (!Util.equals(enabled, castedObject.enabled)) { 97 return false; 98 } 99 100 return Util.equals(id, castedObject.id); 101 } 102 103 void fireIdentifierChanged(IdentifierEvent identifierEvent) { 104 if (identifierEvent == null) { 105 throw new NullPointerException (); 106 } 107 108 if (identifierListeners != null) { 109 for (int i = 0; i < identifierListeners.size(); i++) { 110 ((IIdentifierListener) identifierListeners.get(i)) 111 .identifierChanged(identifierEvent); 112 } 113 } 114 } 115 116 public Set getActivityIds() { 117 return activityIds; 118 } 119 120 public String getId() { 121 return id; 122 } 123 124 public int hashCode() { 125 if (hashCode == HASH_INITIAL) { 126 hashCode = hashCode * HASH_FACTOR + Util.hashCode(activityIds); 127 hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled); 128 hashCode = hashCode * HASH_FACTOR + Util.hashCode(id); 129 if (hashCode == HASH_INITIAL) { 130 hashCode++; 131 } 132 } 133 134 return hashCode; 135 } 136 137 public boolean isEnabled() { 138 return enabled; 139 } 140 141 public void removeIdentifierListener(IIdentifierListener identifierListener) { 142 if (identifierListener == null) { 143 throw new NullPointerException (); 144 } 145 146 if (identifierListeners != null) { 147 identifierListeners.remove(identifierListener); 148 } 149 150 if (identifierListeners.isEmpty()) { 151 strongReferences.remove(this); 152 } 153 } 154 155 boolean setActivityIds(Set activityIds) { 156 activityIds = Util.safeCopy(activityIds, String .class); 157 158 if (!Util.equals(activityIds, this.activityIds)) { 159 this.activityIds = activityIds; 160 this.activityIdsAsArray = (String []) this.activityIds 161 .toArray(new String [this.activityIds.size()]); 162 hashCode = HASH_INITIAL; 163 string = null; 164 return true; 165 } 166 167 return false; 168 } 169 170 boolean setEnabled(boolean enabled) { 171 if (enabled != this.enabled) { 172 this.enabled = enabled; 173 hashCode = HASH_INITIAL; 174 string = null; 175 return true; 176 } 177 178 return false; 179 } 180 181 public String toString() { 182 if (string == null) { 183 final StringBuffer stringBuffer = new StringBuffer (); 184 stringBuffer.append('['); 185 stringBuffer.append(activityIds); 186 stringBuffer.append(','); 187 stringBuffer.append(enabled); 188 stringBuffer.append(','); 189 stringBuffer.append(id); 190 stringBuffer.append(']'); 191 string = stringBuffer.toString(); 192 } 193 194 return string; 195 } 196 } 197 | Popular Tags |