001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2006-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.tools.mocks;
019:
020: import java.net.URL;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Date;
024: import java.util.LinkedList;
025: import org.java.plugin.PathResolver;
026: import org.java.plugin.registry.Extension;
027: import org.java.plugin.registry.ExtensionPoint;
028: import org.java.plugin.registry.PluginDescriptor;
029: import org.java.plugin.registry.Extension.Parameter;
030: import org.java.plugin.registry.ExtensionPoint.ParameterDefinition;
031:
032: /**
033: * @version $Id$
034: */
035: public class MockParameter extends MockPluginElement<Parameter>
036: implements Parameter {
037: private Extension declaringExtension;
038: private ParameterDefinition definition;
039: private Parameter super Parameter;
040: private String rawValue;
041: private Object typedValue;
042: private LinkedList<Parameter> subParameters = new LinkedList<Parameter>();
043:
044: /**
045: * No-arguments constructor.
046: */
047: public MockParameter() {
048: // no-op
049: }
050:
051: /**
052: * @param id parameter ID
053: * @param aRawValue raw parameter value
054: * @param aTypedValue typed parameter value
055: */
056: public MockParameter(final String id, final String aRawValue,
057: final Object aTypedValue) {
058: setId(id);
059: rawValue = aRawValue;
060: typedValue = aTypedValue;
061: }
062:
063: /**
064: * @param id parameter ID
065: * @param aRawValue raw parameter value
066: * @param aTypedValue typed parameter value
067: * @param aDeclaringExtension declaring extension
068: */
069: public MockParameter(final String id, final String aRawValue,
070: final Object aTypedValue,
071: final Extension aDeclaringExtension) {
072: setId(id);
073: rawValue = aRawValue;
074: typedValue = aTypedValue;
075: declaringExtension = aDeclaringExtension;
076: }
077:
078: /**
079: * @see org.java.plugin.registry.Extension.Parameter#getDeclaringExtension()
080: */
081: public Extension getDeclaringExtension() {
082: return declaringExtension;
083: }
084:
085: /**
086: * @param value the declaring extension to set
087: * @return this instance
088: */
089: public MockParameter setDeclaringExtension(final Extension value) {
090: declaringExtension = value;
091: return this ;
092: }
093:
094: /**
095: * @see org.java.plugin.registry.Extension.Parameter#getDefinition()
096: */
097: public ParameterDefinition getDefinition() {
098: return definition;
099: }
100:
101: /**
102: * @param value the parameter definition to set
103: * @return this instance
104: */
105: public MockParameter setDefinition(final ParameterDefinition value) {
106: definition = value;
107: return this ;
108: }
109:
110: /**
111: * @see org.java.plugin.registry.Extension.Parameter#getSubParameter(
112: * java.lang.String)
113: */
114: public Parameter getSubParameter(final String id) {
115: for (Parameter param : subParameters) {
116: if (param.getId().equals(id)) {
117: return param;
118: }
119: }
120: throw new IllegalArgumentException("unknown parameter ID " + id); //$NON-NLS-1$
121: }
122:
123: /**
124: * @see org.java.plugin.registry.Extension.Parameter#getSubParameters()
125: */
126: public Collection<Parameter> getSubParameters() {
127: return Collections.unmodifiableCollection(subParameters);
128: }
129:
130: /**
131: * @see org.java.plugin.registry.Extension.Parameter#getSubParameters(
132: * java.lang.String)
133: */
134: public Collection<Parameter> getSubParameters(final String id) {
135: LinkedList<Parameter> result = new LinkedList<Parameter>();
136: for (Parameter param : subParameters) {
137: if (param.getId().equals(id)) {
138: result.add(param);
139: }
140: }
141: return result;
142: }
143:
144: /**
145: * @param parameter sub-parameter to add
146: * @return this instance
147: */
148: public MockParameter addParameter(final Parameter parameter) {
149: subParameters.add(parameter);
150: return this ;
151: }
152:
153: /**
154: * @see org.java.plugin.registry.Extension.Parameter#getSuperParameter()
155: */
156: public Parameter getSuperParameter() {
157: return super Parameter;
158: }
159:
160: /**
161: * @param value the super parameter to set
162: * @return this instance
163: */
164: public MockParameter setSuperParameter(final Parameter value) {
165: super Parameter = value;
166: return this ;
167: }
168:
169: /**
170: * @see org.java.plugin.registry.Extension.Parameter#rawValue()
171: */
172: public String rawValue() {
173: return rawValue;
174: }
175:
176: /**
177: * @param raw raw parameter value
178: * @param typed typed parameter value
179: * @return this instance
180: */
181: public MockParameter setValue(final String raw, final Object typed) {
182: rawValue = raw;
183: typedValue = typed;
184: return this ;
185: }
186:
187: /**
188: * @see org.java.plugin.registry.Extension.Parameter#valueAsBoolean()
189: */
190: public Boolean valueAsBoolean() {
191: return (Boolean) typedValue;
192: }
193:
194: /**
195: * @see org.java.plugin.registry.Extension.Parameter#valueAsDate()
196: */
197: public Date valueAsDate() {
198: return (Date) typedValue;
199: }
200:
201: /**
202: * @see org.java.plugin.registry.Extension.Parameter#valueAsExtension()
203: */
204: public Extension valueAsExtension() {
205: return (Extension) typedValue;
206: }
207:
208: /**
209: * @see org.java.plugin.registry.Extension.Parameter#valueAsExtensionPoint()
210: */
211: public ExtensionPoint valueAsExtensionPoint() {
212: return (ExtensionPoint) typedValue;
213: }
214:
215: /**
216: * @see org.java.plugin.registry.Extension.Parameter#valueAsNumber()
217: */
218: public Number valueAsNumber() {
219: return (Number) typedValue;
220: }
221:
222: /**
223: * @see org.java.plugin.registry.Extension.Parameter#valueAsPluginDescriptor()
224: */
225: public PluginDescriptor valueAsPluginDescriptor() {
226: return (PluginDescriptor) typedValue;
227: }
228:
229: /**
230: * @see org.java.plugin.registry.Extension.Parameter#valueAsString()
231: */
232: public String valueAsString() {
233: return (String) typedValue;
234: }
235:
236: /**
237: * @see org.java.plugin.registry.Extension.Parameter#valueAsUrl()
238: */
239: public URL valueAsUrl() {
240: return (URL) typedValue;
241: }
242:
243: /**
244: * @see org.java.plugin.registry.Extension.Parameter#valueAsUrl(
245: * org.java.plugin.PathResolver)
246: */
247: public URL valueAsUrl(final PathResolver pathResolver) {
248: return (URL) typedValue;
249: }
250: }
|