001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.types;
020:
021: import java.util.Arrays;
022: import java.util.HashSet;
023: import java.util.Set;
024: import java.util.StringTokenizer;
025:
026: /** Custom class for supporting XSD data type NMTokens */
027: public class NMTokens extends NCName {
028: private static final long serialVersionUID = -2435854824216181165L;
029: private NMToken[] tokens;
030:
031: public NMTokens() {
032: super ();
033: }
034:
035: /**
036: * ctor for NMTokens
037: *
038: * @throws IllegalArgumentException will be thrown if validation fails
039: */
040: public NMTokens(String stValue) throws IllegalArgumentException {
041: setValue(stValue);
042: }
043:
044: public void setValue(String stValue) {
045: StringTokenizer tokenizer = new StringTokenizer(stValue);
046: int count = tokenizer.countTokens();
047: tokens = new NMToken[count];
048: for (int i = 0; i < count; i++) {
049: tokens[i] = new NMToken(tokenizer.nextToken());
050: }
051: }
052:
053: public String toString() {
054: StringBuffer buf = new StringBuffer();
055: for (int i = 0; i < tokens.length; i++) {
056: NMToken token = tokens[i];
057: if (i > 0)
058: buf.append(" ");
059: buf.append(token.toString());
060: }
061: return buf.toString();
062: }
063:
064: /**
065: * NMTokens can be equal without having identical ordering because they represent a set of
066: * references. Hence we have to compare values here as a set, not a list.
067: *
068: * @param object an <code>Object</code> value
069: * @return a <code>boolean</code> value
070: */
071: public boolean equals(Object object) {
072: if (object == this ) {
073: return true; // succeed quickly, when possible
074: }
075: if (object instanceof NMTokens) {
076: NMTokens that = (NMTokens) object;
077: if (this .tokens.length == that.tokens.length) {
078: Set ourSet = new HashSet(Arrays.asList(this .tokens));
079: Set theirSet = new HashSet(Arrays.asList(that.tokens));
080: return ourSet.equals(theirSet);
081: } else {
082: return false;
083: }
084: } else {
085: return false;
086: }
087: }
088:
089: /**
090: * Returns the sum of the hashcodes of the underlying tokens, an operation which is not
091: * sensitive to ordering.
092: *
093: * @return an <code>int</code> value
094: */
095: public int hashCode() {
096: int hash = 0;
097: for (int i = 0; i < tokens.length; i++) {
098: hash += tokens[i].hashCode();
099: }
100: return hash;
101: }
102: }
|