KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > set > AbstractCharSet


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.set;
20
21 import bak.pcj.AbstractCharCollection;
22 import bak.pcj.CharIterator;
23 import bak.pcj.hash.DefaultCharHashFunction;
24
25 /**
26  * This class represents an abstract base for implementing
27  * sets of char values. All operations that can be implemented
28  * using iterators and the <tt>get()</tt> and <tt>set()</tt> methods
29  * are implemented as such. In most cases, this is
30  * hardly an efficient solution, and at least some of those
31  * methods should be overridden by sub-classes.
32  *
33  * @author S&oslash;ren Bak
34  * @version 1.1 2003/1/10
35  * @since 1.0
36  */

37 public abstract class AbstractCharSet extends AbstractCharCollection implements CharSet {
38
39     /** Default constructor to be invoked by sub-classes. */
40     protected AbstractCharSet() { }
41
42     public boolean equals(Object JavaDoc obj) {
43         if (!(obj instanceof CharSet))
44             return false;
45         CharSet s = (CharSet)obj;
46         if (s.size() != size())
47             return false;
48         return containsAll(s);
49     }
50
51     public int hashCode() {
52         int h = 0;
53         CharIterator i = iterator();
54         while (i.hasNext())
55             h += DefaultCharHashFunction.INSTANCE.hash(i.next());
56         return h;
57     }
58
59 }
Popular Tags