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.logistics.plugin.inventory;
028:
029: import org.cougaar.util.log.Logger;
030: import org.cougaar.core.logging.NullLoggingServiceImpl;
031:
032: import java.io.Serializable;
033: import java.util.HashMap;
034:
035: /**
036: * Trimmed down, representation of an asset
037: **/
038: public class MaintainedItem implements Serializable {
039:
040: protected transient Logger logger;
041: protected transient UtilsProvider utilsPlugin;
042:
043: protected String maintainedItemType = null;
044: protected String typeIdentification = null;
045: protected String itemIdentification = null;
046: protected String nomenclature = null;
047: private static HashMap cache = new HashMap();
048:
049: public MaintainedItem() {
050: }
051:
052: public MaintainedItem(String type, String typeId, String itemId,
053: String nomen, UtilsProvider aPlugin) {
054: maintainedItemType = type;
055: typeIdentification = typeId;
056: itemIdentification = itemId;
057: nomenclature = nomen;
058: utilsPlugin = aPlugin;
059: if (utilsPlugin == null) {
060: logger = NullLoggingServiceImpl.getLoggingService();
061: } else {
062: logger = (Logger) utilsPlugin.getLoggingService(this );
063: }
064: }
065:
066: public String getMaintainedItemType() {
067: return maintainedItemType;
068: }
069:
070: public String getTypeIdentification() {
071: return typeIdentification;
072: }
073:
074: public String getItemIdentification() {
075: return itemIdentification;
076: }
077:
078: public String getNomenclature() {
079: return nomenclature;
080: }
081:
082: public static MaintainedItem findOrMakeMaintainedItem(String type,
083: String typeId, String itemId, String nomen,
084: UtilsProvider aPlugin) {
085: if (type == null || typeId == null) {
086: if (aPlugin != null) {
087: Logger aLogger = (Logger) aPlugin
088: .getLoggingService("MaintainedItem");
089: aLogger
090: .error("Type and/or TypeIdentification cannot be null");
091: }
092: return null;
093: }
094: String key = type + typeId;
095: if (itemId != null)
096: key = key + itemId;
097: MaintainedItem item = (MaintainedItem) cache.get(key);
098: if (item == null) {
099: item = new MaintainedItem(type, typeId, itemId, nomen,
100: aPlugin);
101: cache.put(key, item);
102: }
103: return item;
104: }
105:
106: public String toString() {
107: return "MaintainedType: " + typeIdentification + ", Item "
108: + itemIdentification + ", " + nomenclature;
109: }
110:
111: private transient int _hc = 0;
112:
113: public int hashCode() {
114: if (_hc != 0)
115: return _hc;
116: int hc = 1;
117: if (maintainedItemType != null)
118: hc += maintainedItemType.hashCode();
119: if (typeIdentification != null)
120: hc += typeIdentification.hashCode();
121: if (itemIdentification != null)
122: hc += itemIdentification.hashCode();
123: _hc = hc;
124: return hc;
125: }
126:
127: /** Equals for MaintainedItems is equivalent to the test for equal
128: * assets. Assumes
129: **/
130: public boolean equals(Object o) {
131: if (this == o)
132: return true;
133: if (o == null)
134: return false;
135: if (!(this .getClass() == o.getClass()))
136: return false;
137: MaintainedItem oi = (MaintainedItem) o;
138: String oType = oi.getMaintainedItemType();
139: if (oType == null || maintainedItemType == null
140: || !(maintainedItemType.equals(oType)))
141: return false;
142: String oTypeID = oi.getTypeIdentification();
143: if (oTypeID == null || typeIdentification == null
144: || !(typeIdentification.equals(oTypeID)))
145: return false;
146: String oItemID = oi.getItemIdentification();
147: if (oItemID == itemIdentification)
148: return true;
149: if (itemIdentification != null) {
150: return itemIdentification.equals(oItemID);
151: } else {
152: return false;
153: }
154: }
155: }
|