View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.asteriskjava.live;
18  
19  import org.asteriskjava.util.AstUtil;
20  
21  import java.io.Serializable;
22  
23  /***
24   * Represents a Caller*ID containing name and number.
25   * <p/>
26   * Objects of this type are immutable.
27   *
28   * @author srt
29   * @version $Id: CallerId.java 837 2007-07-09 06:44:23Z srt $
30   * @since 0.3
31   */
32  public class CallerId implements Serializable
33  {
34      /***
35       * Serial version identifier.
36       */
37      private static final long serialVersionUID = 6498024163374551005L;
38      private final String name;
39      private final String number;
40  
41      /***
42       * Creates a new CallerId.
43       *
44       * @param name   the Caller*ID name.
45       * @param number the Caller*ID number.
46       */
47      public CallerId(String name, String number)
48      {
49          this.number = (AstUtil.isNull(number)) ? null : number;
50          this.name = (AstUtil.isNull(name)) ? null : name;
51      }
52  
53      /***
54       * Returns the Caller*ID name.
55       *
56       * @return the Caller*ID name.
57       */
58      public String getName()
59      {
60          return name;
61      }
62  
63      /***
64       * Returns the the Caller*ID number.
65       *
66       * @return the Caller*ID number.
67       */
68      public String getNumber()
69      {
70          return number;
71      }
72  
73      /***
74       * Parses a caller id string in the form
75       * <code>"Some Name" &lt;1234&gt;</code> to a CallerId object.
76       *
77       * @param s the caller id string to parse.
78       * @return the corresponding CallerId object which is never <code>null</code>.
79       * @see AstUtil#parseCallerId(String)
80       */
81      public static CallerId valueOf(String s)
82      {
83          final String[] parsedCallerId;
84  
85          parsedCallerId = AstUtil.parseCallerId(s);
86          return new CallerId(parsedCallerId[0], parsedCallerId[1]);
87      }
88  
89      /***
90       * Returns a string representation of this CallerId in the form
91       * <code>"Some Name" &lt;1234&gt;</code>.
92       */
93      @Override
94      public String toString()
95      {
96          final StringBuilder sb;
97  
98          sb = new StringBuilder();
99          if (name != null)
100         {
101             sb.append("\"");
102             sb.append(name);
103             sb.append("\"");
104             if (number != null)
105             {
106                 sb.append(" ");
107             }
108         }
109 
110         if (number != null)
111         {
112             sb.append("<");
113             sb.append(number);
114             sb.append(">");
115         }
116         return sb.toString();
117     }
118 
119     @Override
120     public int hashCode()
121     {
122         final int PRIME = 31;
123         int result = 1;
124         result = PRIME * result + ((name == null) ? 0 : name.hashCode());
125         result = PRIME * result + ((number == null) ? 0 : number.hashCode());
126         return result;
127     }
128 
129     @Override
130     public boolean equals(Object obj)
131     {
132         if (this == obj)
133         {
134             return true;
135         }
136         if (obj == null)
137         {
138             return false;
139         }
140         if (getClass() != obj.getClass())
141         {
142             return false;
143         }
144         final CallerId other = (CallerId) obj;
145         if (name == null)
146         {
147             if (other.name != null)
148             {
149                 return false;
150             }
151         }
152         else if (!name.equals(other.name))
153         {
154             return false;
155         }
156         if (number == null)
157         {
158             if (other.number != null)
159             {
160                 return false;
161             }
162         }
163         else if (!number.equals(other.number))
164         {
165             return false;
166         }
167         return true;
168     }
169 }