001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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: package org.cougaar.tools.csmart.core.cdata;
027:
028: import org.cougaar.tools.csmart.util.ArgValue;
029:
030: import java.io.Serializable;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: /**
036: * Holds a single set of values. These values might be
037: * <code>String</code>s, or might be name-value pairs.
038: **/
039: public class PGPropMultiVal implements Serializable {
040: private List values; // of Strings or of name/value pairs
041:
042: /**
043: * Creates a new <code>PGPropMultiVal</code> instance.
044: *
045: */
046: public PGPropMultiVal() {
047: this .values = new ArrayList();
048: }
049:
050: /**
051: * Creates a new <code>PGPropMultiVal</code> instance with the given values.
052: * Note that the values must be <code>String</code>s ar <code>ArgValue</code>s
053: *
054: * @param newValues an <code>Object[]</code> array of values
055: */
056: // public PGPropMultiVal(Object[] newValues) {
057: // this.values = new ArrayList();
058: // this.setValues(newValues);
059: // }
060: public PGPropMultiVal(Object newValues) {
061: if (newValues instanceof List)
062: this .values = (List) newValues;
063: else if (newValues.getClass().isArray()) {
064: this .values = new ArrayList();
065: this .setValues((Object[]) newValues);
066: }
067: }
068:
069: /**
070: * Sets all values for this Property
071: *
072: * @param newValues Object[] array of values
073: */
074: public void setValues(Object[] newValues) {
075: this .values.clear();
076: for (int i = 0; i < newValues.length; i++) {
077: if (!(newValues[i] instanceof String)
078: && !(newValues[i] instanceof ArgValue)) {
079: throw new RuntimeException(
080: "Value must be a String or ArgValue ["
081: + newValues[i] + "],, but is a "
082: + newValues[i].getClass().toString());
083: //newValues[i] = newValues[i].toString();
084: }
085: this .values.add(newValues[i]);
086: }
087: }
088:
089: /**
090: * Adds a value for this Property
091: *
092: * @param value for this property
093: */
094: public void addValue(String value) {
095: this .values.add(value);
096: }
097:
098: /**
099: * Adds a value for this Property
100: *
101: * @param value as an ArgValue
102: */
103: public void addValue(ArgValue value) {
104: this .values.add(value);
105: }
106:
107: /**
108: * Sets a value for this Property, replacing the previous value at this index
109: *
110: * @param index for value
111: * @param value to replace with
112: */
113: public void setValue(int index, String value)
114: throws IndexOutOfBoundsException {
115: this .values.set(index, value);
116: }
117:
118: /**
119: * Sets a value for this Property, replacing the previous value at this index
120: *
121: * @param index for value
122: * @param value as ArgValue to replace with
123: */
124: public void setValue(int index, ArgValue value)
125: throws IndexOutOfBoundsException {
126: this .values.set(index, value);
127: }
128:
129: /**
130: * Returns an array of values for this property.
131: *
132: * @return values
133: */
134: public String[] getValuesStringArray() {
135: // Do type checking!!!
136: return (String[]) values.toArray(new String[values.size()]);
137: }
138:
139: /**
140: * Returns an array of values for this property.
141: *
142: * @return values
143: */
144: public ArgValue[] getValuesArgValueArray() {
145: // Do type checking!!
146: return (ArgValue[]) values.toArray(new ArgValue[values.size()]);
147: }
148:
149: /**
150: * Returns an array of values for this property.
151: *
152: * @return values
153: */
154: public Object[] getValuesObjectArray() {
155: // Do type checking!!
156: return values.toArray();
157: }
158:
159: /**
160: * Returns an iterator of values for this property.
161: *
162: * @return iterator
163: */
164: public Iterator getValuesIterator() {
165: return values.iterator();
166: }
167:
168: /**
169: * Returns a count of all values for this property.
170: *
171: * @return count
172: */
173: public int getValueCount() {
174: return values.size();
175: }
176:
177: public String toString() {
178: StringBuffer buf = new StringBuffer();
179: if (values.size() > 0) {
180: buf.append(values.get(0));
181: for (int i = 1; i < values.size(); i++) {
182: buf.append(", " + values.get(i));
183: }
184: }
185: return buf.toString();
186: }
187:
188: } // end of PGPropMultiVal.java
|