001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import org.cougaar.qos.ResourceStatus.data_types;
032: import org.cougaar.qos.ResourceStatus.data_value;
033: import org.cougaar.util.log.Logger;
034:
035: /**
036: * A simple struct-like class used to represent the values manipulated by the
037: * various DataScope/DataFormula objects.
038: */
039: public class DataValue implements Constants, java.io.Serializable {
040: /**
041: * Standard instance of a value without any specific credibility. This may
042: * move into the Constants interface later.
043: */
044: public static final DataValue NO_VALUE = new DataValue(0.0, 0.0);
045:
046: private Object value;
047: private final double credibility;
048: private final String units;
049: private final String provenance;
050: private final long timestamp;
051: private final long halflife;
052:
053: public DataValue(DataValue value) {
054: this (value.value, value.credibility, value.units,
055: value.provenance, value.timestamp, value.halflife);
056: }
057:
058: public DataValue(Object value, double credibility, String units,
059: String provenance, long timestamp, long halflife) {
060: this .value = value;
061: this .credibility = credibility;
062: this .units = units;
063: this .provenance = provenance;
064: this .timestamp = timestamp;
065: this .halflife = halflife;
066: }
067:
068: public DataValue(double value, double credibility, String units,
069: String provenance) {
070: this (new Double(value), credibility, units, provenance);
071: }
072:
073: public DataValue(long value, double credibility, String units,
074: String provenance) {
075: this (new Long(value), credibility, units, provenance);
076: }
077:
078: public DataValue(char value, double credibility, String units,
079: String provenance) {
080: this (new Character(value), credibility, units, provenance);
081: }
082:
083: public DataValue(boolean value, double credibility, String units,
084: String provenance) {
085: this (value ? Boolean.TRUE : Boolean.FALSE, credibility, units,
086: provenance);
087: }
088:
089: public DataValue(org.cougaar.qos.ResourceStatus.DataValue value) {
090: data_value v = value.value;
091: switch (v.discriminator().value()) {
092: case data_types._boolean_data:
093: this .value = v.b_value() ? Boolean.TRUE : Boolean.FALSE;
094: break;
095:
096: case data_types._string_data:
097: this .value = v.s_value();
098: break;
099:
100: case data_types._number_data:
101: this .value = new Double(v.d_value());
102: break;
103: }
104: this .credibility = value.credibility;
105: this .units = value.units;
106: this .provenance = value.provenance;
107: this .timestamp = value.timestamp;
108: this .halflife = value.halflife;
109: }
110:
111: public DataValue(Object value, double credibility, String units,
112: String provenance) {
113: this (value, credibility, units, provenance, System
114: .currentTimeMillis(), 0);
115: }
116:
117: /**
118: * Use value of 0.0 and default credibility (from Constants).
119: */
120: public DataValue() {
121: this (0.0);
122: }
123:
124: public DataValue(double value, double credibility) {
125: this (new Double(value), credibility, null, null);
126: }
127:
128: public DataValue(double value) {
129: this (new Double(value), DEFAULT_CREDIBILITY, null, null);
130: }
131:
132: public DataValue(long value, double credibility) {
133: this (new Long(value), credibility, null, null);
134: }
135:
136: public DataValue(long value) {
137: this (new Long(value), DEFAULT_CREDIBILITY, null, null);
138: }
139:
140: public DataValue(String value, double credibility) {
141: this (value, credibility, null, null);
142: }
143:
144: public DataValue(String value) {
145: this (value, DEFAULT_CREDIBILITY, null, null);
146: }
147:
148: public DataValue(char value, double credibility) {
149: this (new Character(value), credibility, null, null);
150: }
151:
152: public DataValue(char value) {
153: this (new Character(value), DEFAULT_CREDIBILITY, null, null);
154: }
155:
156: public DataValue(boolean value, double credibility) {
157: this (value, credibility, null, null);
158: }
159:
160: public DataValue(boolean value) {
161: this (value, DEFAULT_CREDIBILITY, null, null);
162: }
163:
164: public DataValue newDataValue(Object raw) {
165: return new DataValue(raw, credibility, units, provenance,
166: timestamp, halflife);
167: }
168:
169: public DataValue newCredibility(double new_credibility) {
170: return new DataValue(value, new_credibility, units, provenance,
171: timestamp, halflife);
172: }
173:
174: public String toString() {
175: return "<" + value + ":" + credibility + ">";
176: }
177:
178: public org.cougaar.qos.ResourceStatus.DataValue getCorbaValue() {
179: data_value val = new data_value();
180: if (value instanceof String) {
181: val.s_value((String) value);
182: } else if (value instanceof Character) {
183: val.s_value(((Character) value).toString());
184: } else if (value instanceof Double) {
185: val.d_value(((Double) value).doubleValue());
186: } else if (value instanceof Long) {
187: val.d_value(((Long) value).longValue());
188: } else if (value instanceof Boolean) {
189: val.b_value(((Boolean) value).booleanValue());
190: } else {
191: Logger logger = Logging.getLogger(DataValue.class);
192: logger.error("Value is weird " + val);
193: }
194: return new org.cougaar.qos.ResourceStatus.DataValue(timestamp,
195: halflife, credibility, (units != null ? units : ""),
196: (provenance != null ? provenance : ""), val);
197: }
198:
199: // Irrespective of timestamp
200: public boolean contentsEquals(DataValue candidate) {
201: if (candidate == null) {
202: return false;
203: }
204: if (candidate == this ) {
205: return true;
206: }
207: return (value == null && candidate.value == null || value != null
208: && value.equals(candidate.value))
209: && credibility == candidate.credibility
210: && (units == null && candidate.units == null || units != null
211: && units.equals(candidate.units))
212: && (provenance == null && candidate.provenance == null || provenance != null
213: && provenance.equals(candidate.provenance))
214: && halflife == candidate.halflife;
215: }
216:
217: public boolean equals(DataValue candidate) {
218: return contentsEquals(candidate)
219: && timestamp == candidate.timestamp;
220: }
221:
222: public Object getRawValue() {
223: return value;
224: }
225:
226: public String getStringValue() {
227: return (String) value;
228: }
229:
230: public byte getByteValue() {
231: return ((Number) value).byteValue();
232: }
233:
234: public short getShortValue() {
235: return ((Number) value).shortValue();
236: }
237:
238: public int getIntValue() {
239: return ((Number) value).intValue();
240: }
241:
242: public long getLongValue() {
243: return ((Number) value).longValue();
244: }
245:
246: public float getFloatValue() {
247: if (value instanceof Double) {
248: return ((Double) value).floatValue();
249: } else if (value instanceof Long) {
250: return ((Long) value).longValue();
251: } else {
252: // Should signal an error...
253: return (float) 0.0;
254: }
255: }
256:
257: public double getDoubleValue() {
258: if (value instanceof Double) {
259: return ((Double) value).doubleValue();
260: } else if (value instanceof Long) {
261: return ((Long) value).longValue();
262: } else {
263: // Should signal an error...
264: return 0.0;
265: }
266: }
267:
268: public char getCharValue() {
269: return ((Character) value).charValue();
270: }
271:
272: public boolean getBooleanValue() {
273: return ((Boolean) value).booleanValue();
274: }
275:
276: public double getCredibility() {
277: return credibility;
278: }
279:
280: public String getUnits() {
281: return units;
282: }
283:
284: public String getProvenance() {
285: return provenance;
286: }
287:
288: public long getTimestamp() {
289: return timestamp;
290: }
291:
292: public long getHalflife() {
293: return halflife;
294: }
295:
296: public static DataValue mostCredible(DataValue d1, DataValue d2) {
297: if (d1 == null) {
298: return d2;
299: } else if (d2 == null) {
300: return d1;
301: } else if (d1.credibility == d2.credibility) {
302: return d1.timestamp > d2.timestamp ? d1 : d2;
303: } else if (d1.credibility > d2.credibility) {
304: return d1;
305: } else {
306: return d2;
307: }
308: }
309:
310: /**
311: * Returns the value with the hightest credibility. If the max credibilitry
312: * is common to several values, returns the last one (order is significant).
313: */
314: public static DataValue maxCredibility(DataValue[] values) {
315: DataValue max = null;
316: for (DataValue element : values) {
317: max = mostCredible(max, element);
318: }
319: return max;
320: }
321:
322: }
|