1
2
3 /***
4 * ***************************************************************
5 * JADE - Java Agent DEvelopment Framework is a framework to develop
6 * multi-agent systems in compliance with the FIPA specifications.
7 * Copyright (C) 2000 CSELT S.p.A.
8 * GNU Lesser General Public License
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
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 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 * **************************************************************
22 */
23 package JADE_SL;
24
25
26 /***
27 * A name string, with case insensitive comparison and equality operations.
28 * This class holds a <code>String</code> inside, preserving the case; however,
29 * all the equality and comparision operations are performed in a case
30 * insensitive fashion.
31 * @author Giovanni Rimassa - Universita` di Parma
32 * @version $Date: 2003/10/09 13:00:35 $ $Revision: 1.1.1.1 $
33 *
34 * Updated 1/06/2001 12:50 by Dmitri Toropov - Siemens AG
35 */
36 public class CaseInsensitiveString {
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 s = name;
49 }
50
51 /***
52 * Converts the <code>Name</code> object into a string.
53 * @return The string stored inside by the constructor.
54 */
55 public String toString() {
56 return s.toString();
57 }
58
59 /***
60 * Equality operation. This method compares a <code>Name</code> object with
61 * another or with a Java <code>String</code>. The comparison is case
62 * insensitive.
63 * @param o The Java object to compare this <code>Name</code> to.
64 * @return <code>true</code> if the strings contained within the two objects
65 * are equal, apart from case.
66 */
67 public boolean equals(Object o) {
68 if (o instanceof String) {
69 return equalsIgnoreCase(s, (String) o);
70 }
71
72 try {
73 CaseInsensitiveString sn = (CaseInsensitiveString) o;
74
75 return equalsIgnoreCase(s, sn.s);
76 }
77 catch (ClassCastException cce) {
78 return false;
79 }
80 }
81
82 /***
83 * Hash code. This method returns an hash code in such a way that two
84 * <code>Name</code> objects differing only in case have the same hash code.
85 * @return The hash code for this <code>Name</code> object.
86 */
87 public int hashCode() {
88 return s.toLowerCase().hashCode();
89 }
90
91 /***
92 * Static method for case insensitive string comparasion.
93 * For comparasion used the regionMatches approach which
94 * doesn't allocate any additional memory.
95 * @param s1, s2 The <code>String</code> objects to compare
96 * @return <code>true</code> if the strings are equal, apart from case.
97 */
98 public static boolean equalsIgnoreCase(String s1, String s2) {
99 if (s1 == null || s2 == null) {
100 return false;
101 }
102 else {
103 return ((s1.length() == s2.length())
104 && s1.regionMatches(true, 0, s2, 0, s1.length()));
105 }
106 }
107
108 }
109