001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.planning.ldm.policy;
028:
029: /**
030: *
031: **/
032:
033: /**
034: * An LongRuleParameter is a RuleParameter with specified/protected
035: * long bounds that returns an Long
036: */
037: public class LongRuleParameter implements RuleParameter,
038: java.io.Serializable {
039:
040: protected String my_name;
041: protected Long my_value;
042: protected long my_min;
043: protected long my_max;
044:
045: /**
046: * Constructor sets min/max values and establishes value as not set
047: */
048: public LongRuleParameter(String param_name, long min, long max,
049: long value) throws RuleParameterIllegalValueException {
050: this (param_name, min, max);
051: setValue(new Long(value));
052: }
053:
054: public LongRuleParameter(String param_name, long min, long max) {
055: my_min = min;
056: my_max = max;
057: my_value = null;
058: my_name = param_name;
059: }
060:
061: public LongRuleParameter(String param_name) {
062: my_value = null;
063: my_name = param_name;
064: }
065:
066: public LongRuleParameter() {
067: }
068:
069: /*
070: * Parameter type is LONG
071: */
072: public int ParameterType() {
073: return RuleParameter.LONG_PARAMETER;
074: }
075:
076: public String getName() {
077: return my_name;
078: }
079:
080: public void setName(String name) {
081: my_name = name;
082: }
083:
084: public long getMin() {
085: return my_min;
086: }
087:
088: public void setMin(long min) {
089: my_min = min;
090: }
091:
092: public long getMax() {
093: return my_max;
094: }
095:
096: public void setMax(long max) {
097: my_max = max;
098: }
099:
100: public void setBounds(long min, long max) {
101: if (min > max) {
102: throw new java.lang.IllegalArgumentException("min - "
103: + min + " - must be greater than max - " + max);
104: }
105: my_min = min;
106: my_max = max;
107: }
108:
109: public long getLowerBound() {
110: return getMin();
111: }
112:
113: public long getUpperBound() {
114: return getMax();
115: }
116:
117: /**
118: * Get parameter value (Long)
119: * @return Object parameter value (Long). Note : could be null.
120: */
121: public Object getValue() {
122: return my_value;
123: }
124:
125: public long longValue() {
126: return my_value.longValue();
127: }
128:
129: /**
130: * Set parameter value
131: * @param new_value must be Long
132: * @throws RuleParameterIllegalValueException
133: */
134: public void setValue(Object new_value)
135: throws RuleParameterIllegalValueException {
136: boolean success = false;
137: if (new_value instanceof Long) {
138: Long new_long = (Long) new_value;
139: if ((new_long.longValue() >= my_min)
140: && (new_long.longValue() <= my_max)) {
141: my_value = new_long;
142: success = true;
143: }
144: }
145: if (!success)
146: throw new RuleParameterIllegalValueException(
147: RuleParameter.LONG_PARAMETER,
148: "Long must be between " + my_min + " and " + my_max);
149: }
150:
151: /**
152: *
153: * @param test_value : must be Long
154: * @return true if test_value is within the acceptable range
155: */
156: public boolean inRange(Object test_value) {
157: if (test_value instanceof Long) {
158: Long new_long = (Long) test_value;
159: if ((new_long.longValue() >= my_min)
160: && (new_long.longValue() <= my_max))
161: return true;
162: }
163: return false;
164:
165: }
166:
167: public String toString() {
168: return "<#LONG_PARAMETER : " + my_value + " [" + my_min + " , "
169: + my_max + "]>";
170: }
171:
172: public Object clone() {
173: LongRuleParameter irp = new LongRuleParameter(my_name, my_min,
174: my_max);
175: try {
176: irp.setValue(my_value);
177: } catch (RuleParameterIllegalValueException rpive) {
178: }
179: return irp;
180: }
181:
182: public static void Test() {
183: LongRuleParameter irp = new LongRuleParameter("testIntParam",
184: 3, 10);
185:
186: if (irp.getValue() != null) {
187: System.out
188: .println("Error : Parameter not initialized to null");
189: }
190:
191: try {
192: irp.setValue(new Long(11));
193: System.out.println("Error detecting illegal set condition");
194: } catch (RuleParameterIllegalValueException rpive) {
195: }
196:
197: try {
198: irp.setValue(new Long(1));
199: System.out.println("Error detecting illegal set condition");
200: } catch (RuleParameterIllegalValueException rpive) {
201: }
202:
203: Long i4 = new Long(4);
204: try {
205: irp.setValue(i4);
206: } catch (RuleParameterIllegalValueException rpive) {
207: System.out.println("Error detecting legal set condition");
208: }
209:
210: if (irp.getValue() != i4) {
211: System.out.println("Error retrieving value of parameter");
212: }
213:
214: System.out.println("IRP = " + irp);
215: System.out.println("LongRuleParameter test complete.");
216:
217: }
218:
219: }
|