KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > Parameterization


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  ******************************************************************************/

11
12 package org.eclipse.core.commands;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.internal.commands.util.Util;
18
19 /**
20  * <p>
21  * A parameter with a specific value. This is usually a part of a
22  * <code>ParameterizedCommand</code>, which is used to refer to a command
23  * with a collection of parameterizations.
24  * </p>
25  *
26  * @since 3.1
27  */

28 public final class Parameterization {
29
30     /**
31      * The constant integer hash code value meaning the hash code has not yet
32      * been computed.
33      */

34     private static final int HASH_CODE_NOT_COMPUTED = -1;
35
36     /**
37      * A factor for computing the hash code for all parameterized commands.
38      */

39     private static final int HASH_FACTOR = 89;
40
41     /**
42      * The seed for the hash code for all parameterized commands.
43      */

44     private static final int HASH_INITIAL = Parameterization.class.getName()
45             .hashCode();
46
47     /**
48      * The hash code for this object. This value is computed lazily, and marked
49      * as invalid when one of the values on which it is based changes.
50      */

51     private transient int hashCode = HASH_CODE_NOT_COMPUTED;
52
53     /**
54      * The parameter that is being parameterized. This value is never
55      * <code>null</code>.
56      */

57     private final IParameter parameter;
58
59     /**
60      * The value that defines the parameterization. This value may be
61      * <code>null</code>.
62      */

63     private final String JavaDoc value;
64
65     /**
66      * Constructs a new instance of <code>Parameterization</code>.
67      *
68      * @param parameter
69      * The parameter that is being parameterized; must not be
70      * <code>null</code>.
71      * @param value
72      * The value for the parameter; may be <code>null</code>.
73      */

74     public Parameterization(final IParameter parameter, final String JavaDoc value) {
75         if (parameter == null) {
76             throw new NullPointerException JavaDoc(
77                     "You cannot parameterize a null parameter"); //$NON-NLS-1$
78
}
79
80         this.parameter = parameter;
81         this.value = value;
82     }
83
84     /* (non-Javadoc)
85      * @see java.lang.Object#equals(java.lang.Object)
86      */

87     public final boolean equals(final Object JavaDoc object) {
88         if (this == object) {
89             return true;
90         }
91
92         if (!(object instanceof Parameterization)) {
93             return false;
94         }
95
96         final Parameterization parameterization = (Parameterization) object;
97         if (!(Util.equals(this.parameter.getId(), parameterization.parameter
98                 .getId()))) {
99             return false;
100         }
101
102         return Util.equals(this.value, parameterization.value);
103     }
104
105     /**
106      * Returns the parameter that is being parameterized.
107      *
108      * @return The parameter; never <code>null</code>.
109      */

110     public final IParameter getParameter() {
111         return parameter;
112     }
113
114     /**
115      * Returns the value for the parameter in this parameterization.
116      *
117      * @return The value; may be <code>null</code>.
118      */

119     public final String JavaDoc getValue() {
120         return value;
121     }
122
123     /**
124      * Returns the human-readable name for the current value, if any. If the
125      * name cannot be found, then it simply returns the value. It also ensures
126      * that any <code>null</code> values are converted into an empty string.
127      *
128      * @return The human-readable name of the value; never <code>null</code>.
129      * @throws ParameterValuesException
130      * If the parameter needed to be initialized, but couldn't be.
131      */

132     public final String JavaDoc getValueName() throws ParameterValuesException {
133         final Map JavaDoc parameterValues = parameter.getValues().getParameterValues();
134         final Iterator JavaDoc parameterValueItr = parameterValues.entrySet()
135                 .iterator();
136         String JavaDoc returnValue = null;
137         while (parameterValueItr.hasNext()) {
138             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) parameterValueItr.next();
139             final String JavaDoc currentValue = (String JavaDoc) entry.getValue();
140             if (Util.equals(value, currentValue)) {
141                 returnValue = (String JavaDoc) entry.getKey();
142                 break;
143             }
144         }
145
146         if (returnValue == null) {
147             return Util.ZERO_LENGTH_STRING;
148         }
149
150         return returnValue;
151     }
152
153     /* (non-Javadoc)
154      * @see java.lang.Object#hashCode()
155      */

156     public final int hashCode() {
157         if (hashCode == HASH_CODE_NOT_COMPUTED) {
158             hashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(parameter);
159             hashCode = hashCode * HASH_FACTOR + Util.hashCode(value);
160             if (hashCode == HASH_CODE_NOT_COMPUTED) {
161                 hashCode++;
162             }
163         }
164         return hashCode;
165
166     }
167 }
168
Popular Tags