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.plan;
028:
029: import java.util.Date;
030:
031: import org.cougaar.core.util.UniqueObject;
032: import org.cougaar.planning.ldm.asset.Asset;
033: import org.cougaar.util.TimeSpan;
034:
035: /**
036: * A RelationshipImpl is the encapsulation of a time phased relationship
037: *
038: **/
039:
040: public class RelationshipImpl extends ScheduleElementImpl implements
041: Relationship {
042:
043: private Role myRoleA;
044: private HasRelationships myA;
045: private Role myRoleB;
046: private HasRelationships myB;
047:
048: /** no-arg constructor */
049: public RelationshipImpl() {
050: super ();
051: }
052:
053: /** constructor for factory use that takes the start, end, role,
054: * direct and indirect objects
055: **/
056: public RelationshipImpl(TimeSpan timeSpan, Role role1,
057: HasRelationships object1, HasRelationships object2) {
058: this (timeSpan.getStartTime(), timeSpan.getEndTime(), role1,
059: object1, object2);
060: }
061:
062: /** constructor for factory use that takes the start, end, role,
063: * direct and indirect objects
064: **/
065: public RelationshipImpl(long startTime, long endTime, Role role1,
066: HasRelationships object1, HasRelationships object2) {
067: super (startTime, endTime);
068:
069: Role role2 = role1.getConverse();
070:
071: // Normalize on roles so that we don't end up with relationships which
072: // differ only in the A/B ordering, i.e.
073: // rel1.A == rel2.B && rel1.roleA == rel2.roleB &&
074: // rel2.A == rel1.B && rel2.roleA == rel1.roleB
075: if (role1.getName().compareTo(role2.getName()) < 0) {
076: myRoleA = role1;
077: myA = object1;
078: myRoleB = role2;
079: myB = object2;
080: } else {
081: myRoleA = role2;
082: myA = object2;
083: myRoleB = role1;
084: myB = object1;
085: }
086: }
087:
088: /**
089: * equals - performs field by field comparison
090: *
091: * @param object Object to compare
092: * @return boolean if 'same'
093: */
094: public boolean equals(Object object) {
095: if (object == this ) {
096: return true;
097: }
098:
099: if (!(object instanceof Relationship)) {
100: return false;
101: }
102:
103: Relationship other = (Relationship) object;
104:
105: return (getRoleA().equals(other.getRoleA())
106: && getA().equals(other.getA())
107: && getRoleB().equals(other.getRoleB())
108: && getB().equals(other.getB())
109: && getStartTime() == other.getStartTime() && getEndTime() == other
110: .getEndTime());
111: }
112:
113: public int hashCode() {
114: return (int) (getStartTime() + (getEndTime() * 1000)
115: + getA().hashCode() + getRoleA().hashCode()
116: + getB().hashCode() + getRoleB().hashCode());
117: }
118:
119: /** Role performed by HasRelationship A
120: * @return Role which HasRelationships A performs
121: */
122: public Role getRoleA() {
123: return myRoleA;
124: }
125:
126: /** Role performed by HasRelationships B
127: * @return Role which HasRelationships B performs
128: */
129: public Role getRoleB() {
130: return myRoleB;
131: }
132:
133: /**
134: * @return HasRelationships A
135: */
136: public HasRelationships getA() {
137: return myA;
138: }
139:
140: /**
141: * @return HasRelationships B
142: */
143: public HasRelationships getB() {
144: return myB;
145: }
146:
147: public String toString() {
148: String AStr;
149: if (getA() instanceof Asset) {
150: AStr = ((Asset) getA()).getItemIdentificationPG()
151: .getNomenclature();
152: } else if (getA() instanceof UniqueObject) {
153: AStr = ((UniqueObject) getA()).getUID().toString();
154: } else {
155: AStr = getA().toString();
156: }
157:
158: String BStr;
159: if (getB() instanceof Asset) {
160: BStr = ((Asset) getB()).getItemIdentificationPG()
161: .getNomenclature();
162: } else if (getB() instanceof UniqueObject) {
163: BStr = ((UniqueObject) getB()).getUID().toString();
164: } else {
165: BStr = getB().toString();
166: }
167:
168: return "<start:" + new Date(getStartTime()) + " end:"
169: + new Date(getEndTime()) + " roleA:" + getRoleA()
170: + " A:" + AStr + " roleB:" + getRoleB() + " B:" + BStr
171: + ">";
172: }
173: }
|