1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26/*******************************************************************************
27* Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
28*******************************************************************************/
29package gov.nist.javax.sip.header;
30
31/**
32 * The call identifer that goes into a callID header and a in-reply-to header.
33 *
34 * @author M. Ranganathan   <br/>
35 * @version 1.2 $Revision: 1.7 $ $Date: 2009/12/16 02:38:35 $
36 * @see CallID
37 * @see InReplyTo
38 * @since 1.1
39 */
40public final class CallIdentifier extends SIPObject {
41
42    /**
43     * Comment for <code>serialVersionUID</code>
44     */
45    private static final long serialVersionUID = 7314773655675451377L;
46
47    /**
48     * localId field
49     */
50    protected String localId;
51
52    /**
53     * host field
54     */
55    protected String host;
56
57    /**
58     * Default constructor
59     */
60    public CallIdentifier() {
61    }
62
63    /**
64     * Constructor
65     * @param localId id is the local id.
66     * @param host is the host.
67     */
68    public CallIdentifier(String localId, String host) {
69        this.localId = localId;
70        this.host = host;
71    }
72
73    /**
74     * constructor
75     * @param cid String to set
76     * @throws IllegalArgumentException if cid is null or is not a token,
77     * or token@token
78     */
79    public CallIdentifier(String cid) throws IllegalArgumentException {
80        setCallID(cid);
81    }
82
83    /**
84     * Get the encoded version of this id.
85     * @return String to set
86     */
87    public String encode() {
88        return encode(new StringBuffer()).toString();
89    }
90
91    public StringBuffer encode(StringBuffer buffer) {
92        buffer.append(localId);
93        if (host != null) {
94            buffer.append(AT).append(host);
95        }
96        return buffer;
97    }
98
99    /**
100     * Compare two call identifiers for equality.
101     * @param other Object to set
102     * @return true if the two call identifiers are equals, false
103     * otherwise
104     */
105    public boolean equals(Object other) {
106        if (other == null ) return false;
107        if (!other.getClass().equals(this.getClass())) {
108            return false;
109        }
110        CallIdentifier that = (CallIdentifier) other;
111        if (this.localId.compareTo(that.localId) != 0) {
112            return false;
113        }
114        if (this.host == that.host)
115            return true;
116        if ((this.host == null && that.host != null)
117            || (this.host != null && that.host == null))
118            return false;
119        if (host.compareToIgnoreCase(that.host) != 0) {
120            return false;
121        }
122        return true;
123    }
124
125    @Override
126    public int hashCode() {
127        if (this.localId  == null ) {
128             throw new UnsupportedOperationException("Hash code called before id is set");
129        }
130        return this.localId.hashCode();
131    }
132
133    /** get the LocalId field
134     * @return String
135     */
136    public String getLocalId() {
137        return localId;
138    }
139
140    /** get the host field
141     * @return host member String
142     */
143    public String getHost() {
144        return host;
145    }
146
147    /**
148     * Set the localId member
149     * @param localId String to set
150     */
151    public void setLocalId(String localId) {
152        this.localId = localId;
153    }
154
155    /** set the callId field
156     * @param cid Strimg to set
157     * @throws IllegalArgumentException if cid is null or is not a token or
158     * token@token
159     */
160    public void setCallID(String cid) throws IllegalArgumentException {
161        if (cid == null)
162            throw new IllegalArgumentException("NULL!");
163        int index = cid.indexOf('@');
164        if (index == -1) {
165            localId = cid;
166            host = null;
167        } else {
168            localId = cid.substring(0, index);
169            host = cid.substring(index + 1, cid.length());
170            if (localId == null || host == null) {
171                throw new IllegalArgumentException("CallID  must be token@token or token");
172            }
173        }
174    }
175
176    /**
177     * Set the host member
178     * @param host String to set
179     */
180    public void setHost(String host) {
181        this.host = host;
182    }
183}
184