1 11 package org.eclipse.jface.bindings; 12 13 import org.eclipse.jface.util.Util; 14 15 23 public abstract class TriggerSequence { 24 25 28 private static final int HASH_CODE_NOT_COMPUTED = -1; 29 30 33 private static final int HASH_FACTOR = 89; 34 35 38 private static final int HASH_INITIAL = TriggerSequence.class.getName() 39 .hashCode(); 40 41 47 protected transient int hashCode = HASH_CODE_NOT_COMPUTED; 48 49 53 protected final Trigger[] triggers; 54 55 63 public TriggerSequence(final Trigger[] triggers) { 64 if (triggers == null) { 65 throw new NullPointerException ("The triggers cannot be null"); } 67 68 for (int i = 0; i < triggers.length; i++) { 69 if (triggers[i] == null) { 70 throw new IllegalArgumentException ( 71 "All triggers in a trigger sequence must be an instance of Trigger"); } 73 } 74 75 final int triggerLength = triggers.length; 76 this.triggers = new Trigger[triggerLength]; 77 System.arraycopy(triggers, 0, this.triggers, 0, triggerLength); 78 } 79 80 92 public final boolean endsWith(final TriggerSequence triggerSequence, 93 final boolean equals) { 94 if (triggerSequence == null) { 95 throw new NullPointerException ( 96 "Cannot end with a null trigger sequence"); } 98 99 return Util.endsWith(triggers, triggerSequence.triggers, equals); 100 } 101 102 public final boolean equals(final Object object) { 103 if (object == this) { 105 return true; 106 } 107 108 if (!(object instanceof TriggerSequence)) { 110 return false; 111 } 112 113 final TriggerSequence triggerSequence = (TriggerSequence) object; 114 return Util.equals(triggers, triggerSequence.triggers); 115 } 116 117 123 public abstract String format(); 124 125 141 public abstract TriggerSequence[] getPrefixes(); 142 143 149 public final Trigger[] getTriggers() { 150 final int triggerLength = triggers.length; 151 final Trigger[] triggerCopy = new Trigger[triggerLength]; 152 System.arraycopy(triggers, 0, triggerCopy, 0, triggerLength); 153 return triggerCopy; 154 } 155 156 161 public final int hashCode() { 162 if (hashCode == HASH_CODE_NOT_COMPUTED) { 163 hashCode = HASH_INITIAL; 164 hashCode = hashCode * HASH_FACTOR + Util.hashCode(triggers); 165 if (hashCode == HASH_CODE_NOT_COMPUTED) { 166 hashCode++; 167 } 168 } 169 170 return hashCode; 171 } 172 173 178 public final boolean isEmpty() { 179 return (triggers.length == 0); 180 } 181 182 194 public final boolean startsWith(final TriggerSequence triggerSequence, 195 final boolean equals) { 196 if (triggerSequence == null) { 197 throw new NullPointerException ( 198 "A trigger sequence cannot start with null"); } 200 201 return Util.startsWith(triggers, triggerSequence.triggers, equals); 202 } 203 } 204 | Popular Tags |