001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.util;
028:
029: import java.io.Serializable;
030:
031: /**
032: * An argument name (a String), and its value, an arbitrary object (possibly null).
033: * Construct one with the arg name and the Object value. Alternatively, a convenience
034: * constructor allows you to supply a string, and a separator in that string
035: * which separates the Arg and the Value.
036: * Note that the argument cannot be null, and in this case the value is a String
037: *
038: * @see Serializable
039: */
040: public class ArgValue implements Serializable {
041: private String arg;
042: private Object value;
043:
044: /**
045: * Creates a new <code>ArgValue</code> instance.
046: *
047: * @param arg a <code>String</code> argument (non-null)
048: * @param value an <code>Object</code> value
049: */
050: public ArgValue(String arg, Object value) {
051: if (arg == null) {
052: // error!
053: throw new IllegalArgumentException(
054: "Need a non-null argument");
055: }
056: this .arg = arg;
057: this .value = value;
058: }
059:
060: /**
061: * Creates a new <code>ArgValue</code> instance.
062: * The pair is a string with both arg and value in it, with an arbitrary separator.
063: * Note that this works only for the case where the value is a String
064: *
065: * @param pair a <code>String</code> Containing both Arg and Value
066: * @param sep a <code>String</code> that separates Arg and Value
067: */
068: public ArgValue(String pair, String sep) {
069: // the pair must be split on sep to produce arg and value
070: if (pair == null || sep == null) {
071: // error!
072: throw new IllegalArgumentException(
073: "Can't parse with null Strings");
074: }
075:
076: int start = pair.indexOf(sep);
077: if (start < 1) {
078: // error!
079: throw new IllegalArgumentException(
080: "Separator not found, or null Argument");
081: }
082:
083: int stop = start + sep.length();
084:
085: //this(pair.substring(0, start), pair.substring(stop, pair.length() - 1));
086: this .arg = pair.substring(0, start);
087: if (this .arg == null) {
088: throw new IllegalArgumentException(
089: "Need a non-null argument");
090: }
091: this .value = pair.substring(stop, pair.length());
092: }
093:
094: /**
095: * Creates a new <code>ArgValue</code> instance.
096: * Take a string containg both the argument and the value
097: * Assume the separator is " = "
098: * Note that this works only for the case where the value is a String
099: *
100: * @param pair a <code>String</code> with arg and value
101: */
102: public ArgValue(String pair) {
103: this (pair, " = ");
104: }
105:
106: /**
107: * @return a <code>String</code> Argument
108: */
109: public String getArg() {
110: return this .arg;
111: }
112:
113: /**
114: * @return an <code>Object</code> Value, possibly null
115: */
116: public Object getValue() {
117: return this .value;
118: }
119:
120: /**
121: * Override the current value for this ArgValue pair with a new value
122: * Use with caution!
123: *
124: * @param val an <code>Object</code> value for this ArgValue pair
125: */
126: public void setValue(Object val) {
127: this .value = val;
128: }
129:
130: /**
131: * Two ArgValues are equal if BOTH the Argument and the value are String.equals
132: *
133: * @param o an <code>Object</code> to compare
134: * @return a <code>boolean</code>, true if equal
135: */
136: public boolean equals(Object o) {
137: if (o instanceof ArgValue) {
138: ArgValue t = (ArgValue) o;
139: if (t.getValue() != null) {
140: return ((t.getValue().equals(this .value)) && (t
141: .getArg().equals(this .arg)));
142: } else {
143: return (this .value == null && (t.getArg()
144: .equals(this .arg)));
145: }
146: }
147: return super .equals(o);
148: }
149:
150: public String toString() {
151: if (this .value != null) {
152: return (this .arg + " = " + this .value.toString());
153: } else {
154: return (this .arg + " = (null)");
155: }
156: }
157: } // ArgValue.java
|