KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > types > IDRefs


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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 package org.apache.axis.types;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * Custom class for supporting XSD data type IDRefs
25  *
26  * @author Davanum Srinivas <dims@yahoo.com>
27  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#IDREFS">XML Schema 3.3.10 IDREFS</a>
28  */

29 public class IDRefs extends NCName {
30     private IDRef[] idrefs;
31
32     public IDRefs() {
33         super();
34     }
35     /**
36      * ctor for IDRefs
37      * @exception IllegalArgumentException will be thrown if validation fails
38      */

39     public IDRefs (String JavaDoc stValue) throws IllegalArgumentException JavaDoc {
40         setValue(stValue);
41     }
42
43     public void setValue(String JavaDoc stValue) {
44         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(stValue);
45         int count = tokenizer.countTokens();
46         idrefs = new IDRef[count];
47         for(int i=0;i<count;i++){
48             idrefs[i] = new IDRef(tokenizer.nextToken());
49         }
50     }
51
52     public String JavaDoc toString() {
53         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54         for (int i = 0; i < idrefs.length; i++) {
55             IDRef ref = idrefs[i];
56             if (i > 0) buf.append(" ");
57             buf.append(ref.toString());
58         }
59         return buf.toString();
60     }
61
62     /**
63      * IDREFs can be equal without having identical ordering because
64      * they represent a set of references. Hence we have to compare
65      * values here as a set, not a list.
66      *
67      * @param object an <code>Object</code> value
68      * @return a <code>boolean</code> value
69      */

70     public boolean equals(Object JavaDoc object) {
71         if (object == this) {
72             return true; // succeed quickly, when possible
73
}
74         if (object instanceof IDRefs) {
75             IDRefs that = (IDRefs)object;
76             if (this.idrefs.length == that.idrefs.length) {
77                 Set JavaDoc ourSet = new HashSet JavaDoc(Arrays.asList(this.idrefs));
78                 Set JavaDoc theirSet = new HashSet JavaDoc(Arrays.asList(that.idrefs));
79                 return ourSet.equals(theirSet);
80             } else {
81                 return false;
82             }
83         } else {
84             return false;
85         }
86     }
87
88     /**
89      * Returns the sum of the hashcodes of the underlying idrefs, an
90      * operation which is not sensitive to ordering.
91      *
92      * @return an <code>int</code> value
93      */

94     public int hashCode() {
95         int hash = 0;
96         for (int i = 0; i < idrefs.length; i++) {
97             hash += idrefs[i].hashCode();
98         }
99         return hash;
100     }
101 }
102
Popular Tags