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 IntegerRuleParameter is a RuleParameter with specified/protected
035: * integer bounds that returns an Integer
036: */
037: public class IntegerRuleParameter implements RuleParameter,
038: java.io.Serializable {
039: protected String my_name;
040: protected Integer my_value;
041: protected int my_min;
042: protected int my_max;
043:
044: /**
045: * Constructor sets min/max values and establishes value as not set
046: */
047: public IntegerRuleParameter(String param_name, int min, int max,
048: int value) throws RuleParameterIllegalValueException {
049: this (param_name, min, max);
050: setValue(new Integer(value));
051: }
052:
053: public IntegerRuleParameter(String param_name, int min, int max) {
054: my_min = min;
055: my_max = max;
056: my_value = null;
057: my_name = param_name;
058: }
059:
060: public IntegerRuleParameter(String param_name) {
061: my_value = null;
062: my_name = param_name;
063: }
064:
065: public IntegerRuleParameter() {
066: }
067:
068: /**
069: * Parameter type is INTEGER
070: */
071: public int ParameterType() {
072: return RuleParameter.INTEGER_PARAMETER;
073: }
074:
075: public String getName() {
076: return my_name;
077: }
078:
079: public void setName(String name) {
080: my_name = name;
081: }
082:
083: public int getMin() {
084: return my_min;
085: }
086:
087: public void setMin(int min) {
088: my_min = min;
089: }
090:
091: public int getMax() {
092: return my_max;
093: }
094:
095: public void setMax(int max) {
096: my_max = max;
097: }
098:
099: public void setBounds(int min, int max) {
100: if (min > max) {
101: throw new java.lang.IllegalArgumentException("min - "
102: + min + " - must be greater than max - " + max);
103: }
104: my_min = min;
105: my_max = max;
106: }
107:
108: public int getLowerBound() {
109: return getMin();
110: }
111:
112: public int getUpperBound() {
113: return getMax();
114: }
115:
116: /**
117: * Get parameter value (Integer)
118: * @return Object parameter value (Integer). Note : could be null.
119: */
120: public Object getValue() {
121: return my_value;
122: }
123:
124: public int intValue() {
125: return my_value.intValue();
126: }
127:
128: /**
129: * Set parameter value
130: * @param new_value : must be Integer
131: * @throws RuleParameterIllegalValueException
132: */
133: public void setValue(Object new_value)
134: throws RuleParameterIllegalValueException {
135: boolean success = false;
136: if (new_value instanceof Integer) {
137: Integer new_integer = (Integer) new_value;
138: if ((new_integer.intValue() >= my_min)
139: && (new_integer.intValue() <= my_max)) {
140: my_value = new_integer;
141: success = true;
142: }
143: }
144: if (!success)
145: throw new RuleParameterIllegalValueException(
146: RuleParameter.INTEGER_PARAMETER,
147: "Integer must be between " + my_min + " and "
148: + my_max);
149: }
150:
151: /**
152: *
153: * @param test_value : must be Integer
154: * @return true if test_value is within the acceptable range
155: */
156: public boolean inRange(Object test_value) {
157: if (test_value instanceof Integer) {
158: Integer new_integer = (Integer) test_value;
159: if ((new_integer.intValue() >= my_min)
160: && (new_integer.intValue() <= my_max))
161: return true;
162: }
163: return false;
164:
165: }
166:
167: public String toString() {
168: return "#<INTEGER_PARAMETER : " + my_value + " [" + my_min
169: + " , " + my_max + "] >";
170: }
171:
172: public Object clone() {
173: IntegerRuleParameter irp = new IntegerRuleParameter(my_name,
174: my_min, 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: IntegerRuleParameter irp = new IntegerRuleParameter(
184: "testIntParam", 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 Integer(11));
193: System.out.println("Error detecting illegal set condition");
194: } catch (RuleParameterIllegalValueException rpive) {
195: }
196:
197: try {
198: irp.setValue(new Integer(1));
199: System.out.println("Error detecting illegal set condition");
200: } catch (RuleParameterIllegalValueException rpive) {
201: }
202:
203: Integer i4 = new Integer(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("IntegerRuleParameter test complete.");
216:
217: }
218:
219: }
|