KickJava   Java API By Example, From Geeks To Geeks.

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


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 NMTokens
25  *
26  * @author Davanum Srinivas <dims@yahoo.com>
27  */

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

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

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

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