Mercurial > projects > dwt2
comparison org.eclipse.core.commands/src/org/eclipse/core/commands/Parameterization.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 ******************************************************************************/ | |
13 | |
14 module org.eclipse.core.commands.Parameterization; | |
15 | |
16 import org.eclipse.core.commands.IParameter; | |
17 import org.eclipse.core.internal.commands.util.Util; | |
18 | |
19 import java.lang.all; | |
20 import java.util.Map; | |
21 import java.util.Iterator; | |
22 | |
23 /** | |
24 * <p> | |
25 * A parameter with a specific value. This is usually a part of a | |
26 * <code>ParameterizedCommand</code>, which is used to refer to a command | |
27 * with a collection of parameterizations. | |
28 * </p> | |
29 * | |
30 * @since 3.1 | |
31 */ | |
32 public final class Parameterization { | |
33 | |
34 /** | |
35 * The constant integer hash code value meaning the hash code has not yet | |
36 * been computed. | |
37 */ | |
38 private static const int HASH_CODE_NOT_COMPUTED = -1; | |
39 | |
40 /** | |
41 * A factor for computing the hash code for all parameterized commands. | |
42 */ | |
43 private static const int HASH_FACTOR = 89; | |
44 | |
45 /** | |
46 * The seed for the hash code for all parameterized commands. | |
47 */ | |
48 private static const int HASH_INITIAL; | |
49 | |
50 static this(){ | |
51 HASH_INITIAL = java.lang.all.toHash( Parameterization.classinfo.name ); | |
52 } | |
53 /** | |
54 * The hash code for this object. This value is computed lazily, and marked | |
55 * as invalid when one of the values on which it is based changes. | |
56 */ | |
57 private /+transient+/ hash_t hashCode = HASH_CODE_NOT_COMPUTED; | |
58 | |
59 /** | |
60 * The parameter that is being parameterized. This value is never | |
61 * <code>null</code>. | |
62 */ | |
63 private const IParameter parameter; | |
64 | |
65 /** | |
66 * The value that defines the parameterization. This value may be | |
67 * <code>null</code>. | |
68 */ | |
69 private const String value; | |
70 | |
71 /** | |
72 * Constructs a new instance of <code>Parameterization</code>. | |
73 * | |
74 * @param parameter | |
75 * The parameter that is being parameterized; must not be | |
76 * <code>null</code>. | |
77 * @param value | |
78 * The value for the parameter; may be <code>null</code>. | |
79 */ | |
80 public this(IParameter parameter, String value) { | |
81 if (parameter is null) { | |
82 throw new NullPointerException( | |
83 "You cannot parameterize a null parameter"); //$NON-NLS-1$ | |
84 } | |
85 | |
86 this.parameter = parameter; | |
87 this.value = value; | |
88 } | |
89 | |
90 /* (non-Javadoc) | |
91 * @see java.lang.Object#equals(java.lang.Object) | |
92 */ | |
93 public override final int opEquals(Object object) { | |
94 if (this is object) { | |
95 return true; | |
96 } | |
97 | |
98 if (!(cast(Parameterization)object)) { | |
99 return false; | |
100 } | |
101 | |
102 Parameterization parameterization = cast(Parameterization) object; | |
103 if (!(Util.equals(this.parameter.getId(), parameterization.parameter | |
104 .getId()))) { | |
105 return false; | |
106 } | |
107 | |
108 return Util.equals(this.value, parameterization.value); | |
109 } | |
110 | |
111 /** | |
112 * Returns the parameter that is being parameterized. | |
113 * | |
114 * @return The parameter; never <code>null</code>. | |
115 */ | |
116 public final IParameter getParameter() { | |
117 return parameter; | |
118 } | |
119 | |
120 /** | |
121 * Returns the value for the parameter in this parameterization. | |
122 * | |
123 * @return The value; may be <code>null</code>. | |
124 */ | |
125 public final String getValue() { | |
126 return value; | |
127 } | |
128 | |
129 /** | |
130 * Returns the human-readable name for the current value, if any. If the | |
131 * name cannot be found, then it simply returns the value. It also ensures | |
132 * that any <code>null</code> values are converted into an empty string. | |
133 * | |
134 * @return The human-readable name of the value; never <code>null</code>. | |
135 * @throws ParameterValuesException | |
136 * If the parameter needed to be initialized, but couldn't be. | |
137 */ | |
138 public final String getValueName() { | |
139 Map parameterValues = parameter.getValues().getParameterValues(); | |
140 Iterator parameterValueItr = parameterValues.entrySet() | |
141 .iterator(); | |
142 String returnValue = null; | |
143 while (parameterValueItr.hasNext()) { | |
144 Map.Entry entry = cast(Map.Entry) parameterValueItr.next(); | |
145 String currentValue = stringcast( entry.getValue()); | |
146 if (Util.equals(value, currentValue)) { | |
147 returnValue = stringcast( entry.getKey()); | |
148 break; | |
149 } | |
150 } | |
151 | |
152 if (returnValue is null) { | |
153 return Util.ZERO_LENGTH_STRING; | |
154 } | |
155 | |
156 return returnValue; | |
157 } | |
158 | |
159 /* (non-Javadoc) | |
160 * @see java.lang.Object#hashCode() | |
161 */ | |
162 public override final hash_t toHash() { | |
163 if (hashCode is HASH_CODE_NOT_COMPUTED) { | |
164 hashCode = HASH_INITIAL * HASH_FACTOR + Util.toHash(cast(Object)parameter); | |
165 hashCode = hashCode * HASH_FACTOR + Util.toHash(value); | |
166 if (hashCode is HASH_CODE_NOT_COMPUTED) { | |
167 hashCode++; | |
168 } | |
169 } | |
170 return hashCode; | |
171 } | |
172 } |