1 /******************************************************************
2 JADE - Java Agent DEvelopment Framework is a framework to develop
3 multi-agent systems in compliance with the FIPA specifications.
4 Copyright (C) 2000 CSELT S.p.A.
5
6 GNU Lesser General Public License
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation,
11 version 2.1 of the License.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22 *****************************************************************/
23 package sl;
24
25 import java.io.Serializable;
26
27 /***
28 A name string, with case insensitive comparison and equality operations.
29 This class holds a <code>String</code> inside, preserving the case; however,
30 all the equality and comparision operations are performed in a case
31 insensitive fashion.
32
33 @author Giovanni Rimassa - Universita` di Parma
34 @version $Date: 2003/10/09 13:00:37 $ $Revision: 1.1.1.1 $
35 */
36 public class CaseInsensitiveString implements Serializable {
37
38 /***
39 @serial
40 */
41 String s;
42
43 /***
44 Create a new <code>Name</code> object.
45 @param name The string that will be kept inside this object.
46 */
47 public CaseInsensitiveString(String name) {
48
49 s = name;
50
51 }
52
53
54 /***
55 Converts the <code>Name</code> object into a string.
56 @return The string stored inside by the constructor.
57 */
58 public String toString() {
59
60 return s.toString();
61
62 }
63
64 /***
65 Equality operation. This method compares a <code>Name</code> object with
66 another or with a Java <code>String</code>. The comparison is case
67 insensitive.
68 @param o The Java object to compare this <code>Name</code> to.
69 @return <code>true</code> if the strings contained within the two objects
70 are equal, apart from case.
71 */
72 public boolean equals(Object o) {
73
74 if(o instanceof String) {
75 return s.equalsIgnoreCase((String)o);
76 }
77 try {
78 CaseInsensitiveString sn = (CaseInsensitiveString)o;
79 return s.equalsIgnoreCase(sn.s);
80 }
81 catch(ClassCastException cce) {
82 return false;
83 }
84
85 }
86
87
88 /***
89 Hash code. This method returns an hash code in such a way that two
90 <code>Name</code> objects differing only in case have the same hash code.
91 @return The hash code for this <code>Name</code> object.
92 */
93 public int hashCode() {
94 return s.toLowerCase().hashCode();
95 }
96
97
98
99 }