1 11 12 package org.eclipse.ui.internal.commands; 13 14 import org.eclipse.core.commands.IParameter; 15 import org.eclipse.core.commands.IParameterValues; 16 import org.eclipse.core.commands.ITypedParameter; 17 import org.eclipse.core.commands.ParameterType; 18 import org.eclipse.core.commands.ParameterValuesException; 19 import org.eclipse.core.commands.common.HandleObject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.ui.internal.util.Util; 23 24 39 public final class Parameter implements IParameter, ITypedParameter { 40 41 46 private static final String ATTRIBUTE_VALUES = "values"; 48 52 private static final int HASH_CODE_NOT_COMPUTED = -1; 53 54 57 private static final int HASH_FACTOR = 89; 58 59 62 private static final int HASH_INITIAL = HandleObject.class.getName() 63 .hashCode(); 64 65 69 private transient int hashCode = HASH_CODE_NOT_COMPUTED; 70 71 76 protected final String id; 77 78 82 private final String name; 83 84 87 private final boolean optional; 88 89 93 private final ParameterType parameterType; 94 95 101 protected transient String string = null; 102 103 108 private transient IParameterValues values = null; 109 110 115 private final IConfigurationElement valuesConfigurationElement; 116 117 137 public Parameter(final String id, final String name, 138 final IConfigurationElement values, 139 final ParameterType parameterType, final boolean optional) { 140 if (id == null) { 141 throw new NullPointerException ( 142 "Cannot create a parameter with a null id"); } 144 145 if (name == null) { 146 throw new NullPointerException ( 147 "The name of a parameter cannot be null."); } 149 150 if (values == null) { 151 throw new NullPointerException ( 152 "The values for a parameter cannot be null."); } 154 155 this.id = id; 156 this.name = name; 157 this.valuesConfigurationElement = values; 158 this.parameterType = parameterType; 159 this.optional = optional; 160 } 161 162 171 public final boolean equals(final Object object) { 172 if (this == object) { 173 return true; 174 } 175 176 if (!(object instanceof Parameter)) { 177 return false; 178 } 179 180 final Parameter parameter = (Parameter) object; 181 if (!Util.equals(id, parameter.id)) { 182 return false; 183 } 184 if (!Util.equals(name, parameter.name)) { 185 return false; 186 } 187 if (!Util.equals(values, parameter.values)) { 188 return false; 189 } 190 191 return Util.equals(optional, parameter.optional); 192 } 193 194 public final String getId() { 195 return id; 196 } 197 198 public final String getName() { 199 return name; 200 } 201 202 public final ParameterType getParameterType() { 203 return parameterType; 204 } 205 206 public final IParameterValues getValues() throws ParameterValuesException { 207 if (values == null) { 208 try { 209 values = (IParameterValues) valuesConfigurationElement 210 .createExecutableExtension(ATTRIBUTE_VALUES); 211 } catch (final CoreException e) { 212 throw new ParameterValuesException( 213 "Problem creating parameter values", e); } catch (final ClassCastException e) { 215 throw new ParameterValuesException( 216 "Parameter values were not an instance of IParameterValues", e); } 218 } 219 220 return values; 221 } 222 223 public final int hashCode() { 224 if (hashCode == HASH_CODE_NOT_COMPUTED) { 225 hashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(id); 226 if (hashCode == HASH_CODE_NOT_COMPUTED) { 227 hashCode++; 228 } 229 } 230 return hashCode; 231 } 232 233 public final boolean isOptional() { 234 return optional; 235 } 236 237 public final String toString() { 238 if (string == null) { 239 final StringBuffer buffer = new StringBuffer (); 240 241 buffer.append("Parameter("); buffer.append(id); 243 buffer.append(','); 244 buffer.append(name); 245 buffer.append(','); 246 buffer.append(values); 247 buffer.append(','); 248 buffer.append(optional); 249 buffer.append(')'); 250 251 string = buffer.toString(); 252 } 253 254 return string; 255 } 256 } 257 | Popular Tags |