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.core.mts;
028:
029: import java.io.IOException;
030: import java.io.ObjectInput;
031: import java.io.ObjectOutput;
032:
033: /**
034: * A simple {@link MessageAddress} containing an agent name.
035: */
036: public class SimpleMessageAddress extends MessageAddress {
037: protected transient byte[] addressBytes;
038: protected transient String _as = null;
039:
040: // public for externalizable use
041: public SimpleMessageAddress() {
042: }
043:
044: protected SimpleMessageAddress(String address) {
045: this .addressBytes = address.getBytes();
046: _as = address.intern();
047: }
048:
049: public final String getAddress() {
050: return _as;
051: }
052:
053: public boolean equals(SimpleMessageAddress ma) {
054: return (ma != null && _as == ma._as);
055: }
056:
057: public boolean equals(Object o) {
058: if (this == o)
059: return true;
060: // use == since the strings are interned.
061: if (o instanceof MessageAddress) {
062: MessageAddress oma = (MessageAddress) o;
063: return (_as == oma.toAddress());
064: } else {
065: return false;
066: }
067: }
068:
069: public String toAddress() {
070: return _as;
071: }
072:
073: public final int hashCode() {
074: return _as.hashCode();
075: }
076:
077: public void writeExternal(ObjectOutput out) throws IOException {
078: int l = addressBytes.length;
079: out.writeByte(l);
080: out.write(addressBytes, 0, l);
081: }
082:
083: public void readExternal(ObjectInput in)
084: throws ClassNotFoundException, IOException {
085: int l = in.readByte();
086: addressBytes = new byte[l];
087: in.readFully(addressBytes, 0, l);
088: _as = new String(addressBytes).intern();
089: }
090:
091: protected Object readResolve() {
092: return cacheSimpleMessageAddress(this );
093: }
094:
095: private static java.util.HashMap cache = new java.util.HashMap(89);
096:
097: public static SimpleMessageAddress getSimpleMessageAddress(String as) {
098: if (as == null)
099: return null;
100: as = as.intern();
101: synchronized (cache) {
102: SimpleMessageAddress a = (SimpleMessageAddress) cache
103: .get(as);
104: if (a != null) {
105: return a;
106: } else {
107: a = new SimpleMessageAddress(as);
108: cache.put(as, a);
109: return a;
110: }
111: }
112: }
113:
114: public static SimpleMessageAddress cacheSimpleMessageAddress(
115: SimpleMessageAddress a) {
116: if (a == null)
117: return null;
118: synchronized (cache) {
119: String as = a._as;
120: SimpleMessageAddress x = (SimpleMessageAddress) cache
121: .get(as);
122: if (x != null) {
123: return x;
124: } else {
125: cache.put(as, a);
126: return a;
127: }
128: }
129: }
130:
131: }
|