001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.servicediscovery.description;
027:
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.util.List;
031:
032: import org.cougaar.core.util.UID;
033: import org.cougaar.core.util.UniqueObject;
034: import org.cougaar.util.log.Logger;
035: import org.cougaar.util.log.Logging;
036: import org.cougaar.util.TimeSpan;
037:
038: import org.cougaar.planning.ldm.plan.ScheduleElement;
039: import org.cougaar.planning.ldm.plan.ScheduleElementImpl;
040: import org.cougaar.planning.ldm.plan.Schedule;
041: import org.cougaar.planning.ldm.plan.ScheduleImpl;
042:
043: /**
044: * A Collection which maintains an ordered list of agent names - reflecting
045: * the superior/subordinate chain from a root agent.
046: * Iteration starts with the current agent and goes to the root.
047: */
048:
049: public class LineageImpl extends Lineage {
050: private static Logger myLogger = Logging
051: .getLogger(LineageImpl.class);
052:
053: private UID myUID = null;
054: private ArrayList myList = new ArrayList();
055: private int myType = -1;
056: private Schedule mySchedule = new ScheduleImpl();
057:
058: public LineageImpl() {
059: }
060:
061: public LineageImpl(int type) {
062: super ();
063:
064: if (!validType(type)) {
065: throw new IllegalArgumentException(
066: "Unrecognized lineage type: " + type);
067: }
068:
069: myType = type;
070:
071: // Default to always valid
072: mySchedule.add(new ScheduleElementImpl(TimeSpan.MIN_VALUE,
073: TimeSpan.MAX_VALUE));
074: }
075:
076: public LineageImpl(int type, List list) {
077: this (type);
078:
079: myList.addAll(list);
080: }
081:
082: public LineageImpl(int type, List list, Schedule schedule) {
083: this (type);
084:
085: myList.addAll(list);
086:
087: mySchedule = new ScheduleImpl(schedule);
088: }
089:
090: public LineageImpl(Lineage lineage) {
091: this (lineage.getType(), lineage.getList(), lineage
092: .getSchedule());
093: }
094:
095: public void setUID(UID newUID) {
096: if (myUID != null) {
097: myLogger.error("Attempt to reset UID.");
098: return;
099: }
100:
101: myUID = newUID;
102: }
103:
104: public UID getUID() {
105: return myUID;
106: }
107:
108: /**
109: * @return the type of the lineage.
110: * Should be one of the defined lineage types.
111: **/
112: public int getType() {
113: return myType;
114: }
115:
116: /**
117: * @param type of the lineage.
118: * Should be one of the defined lineage types.
119: **/
120: public void setType(int type) {
121: // Not allowed to set type more than once
122: if (myType != -1) {
123: myLogger.error("Attempt to reset lineage type.");
124: return;
125: }
126:
127: if (!validType(type)) {
128: throw new IllegalArgumentException(
129: "Unrecognized lineage type: " + type);
130: }
131:
132: myType = type;
133: }
134:
135: /**
136: * @return the name of the agent at the end of the lineage
137: * Null if lineage has no members
138: */
139: public String getLeaf() {
140: return (String) ((myList.size() > 0) ? (myList.get(myList
141: .size() - 1)) : null);
142: }
143:
144: /**
145: * @return the name of the agent at the root of the lineage.
146: */
147: public String getRoot() {
148: return (String) ((myList.size() > 0) ? myList.get(0) : null);
149: }
150:
151: /**
152: * @return the number of links in the lineage between the two agents.
153: * Returns -1 if the agents are not linked.
154: */
155: public int countHops(String startingAgent, String endingAgent) {
156: return Lineage.countHops(getList(), startingAgent, endingAgent);
157: }
158:
159: /*
160: * @return an unmodifiable list of all agents in the lineage
161: */
162: public List getList() {
163: return Collections.unmodifiableList(myList);
164: }
165:
166: /*
167: * Sets the associated lineage list
168: */
169: public synchronized void setList(List list) {
170: myList = new ArrayList(list);
171: }
172:
173: /**
174: * @return the time periods for which the lineage is valid
175: */
176: public Schedule getSchedule() {
177: return mySchedule;
178: }
179:
180: /*
181: * Sets the Schedule
182: */
183: public void setSchedule(Schedule schedule) {
184: mySchedule = new ScheduleImpl(schedule);
185: }
186:
187: public String toString() {
188: StringBuffer buf = new StringBuffer();
189: buf.append("UID=" + getUID());
190:
191: String typeStr = "";
192: switch (myType) {
193: case (ADCON):
194: typeStr = ADCON_STRING;
195: break;
196: case (OPCON):
197: typeStr = OPCON_STRING;
198: break;
199: case (SUPPORT):
200: typeStr = SUPPORT_STRING;
201: break;
202: default:
203: typeStr = UNDEFINED_STRING;
204: }
205: buf.append(", type=" + typeStr);
206:
207: buf.append(", list=[");
208: for (int i = 0; i < myList.size(); i++) {
209: buf.append(String.valueOf(myList.get(i)));
210: if (i < (myList.size() - 1))
211: buf.append(", ");
212: }
213: buf.append("]");
214:
215: buf.append(", schedule=" + getSchedule().toString());
216: return buf.toString();
217: }
218:
219: public boolean equals(Object o) {
220: if (o instanceof LineageImpl) {
221: LineageImpl otherLineage = (LineageImpl) o;
222: if ((getUID() == null) || (otherLineage.getUID() == null)) {
223: return ((otherLineage.getUID() == null)
224: && (getUID() == null)
225: && (getList().equals(otherLineage.getList())) && (getSchedule()
226: .equals(otherLineage.getSchedule())));
227: } else {
228: return (otherLineage.getUID().equals(getUID()));
229: }
230: } else {
231: return false;
232: }
233: }
234:
235: public int hashCode() {
236: return getUID().hashCode();
237: }
238: }
|