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: /**
027: * Custom class for supporting XSD data type IDRefs
028: *
029: * @see <a href="http://www.w3.org/TR/xmlschema-2/#IDREFS">XML Schema 3.3.10 IDREFS</a>
030: */
031: public class IDRefs extends NCName {
032:
033: private static final long serialVersionUID = 5394097798361075902L;
034:
035: private IDRef[] idrefs;
036:
037: public IDRefs() {
038: super ();
039: }
040:
041: /**
042: * ctor for IDRefs
043: *
044: * @throws IllegalArgumentException will be thrown if validation fails
045: */
046: public IDRefs(String stValue) throws IllegalArgumentException {
047: setValue(stValue);
048: }
049:
050: public void setValue(String stValue) {
051: StringTokenizer tokenizer = new StringTokenizer(stValue);
052: int count = tokenizer.countTokens();
053: idrefs = new IDRef[count];
054: for (int i = 0; i < count; i++) {
055: idrefs[i] = new IDRef(tokenizer.nextToken());
056: }
057: }
058:
059: public String toString() {
060: StringBuffer buf = new StringBuffer();
061: for (int i = 0; i < idrefs.length; i++) {
062: IDRef ref = idrefs[i];
063: if (i > 0)
064: buf.append(" ");
065: buf.append(ref.toString());
066: }
067: return buf.toString();
068: }
069:
070: /**
071: * IDREFs can be equal without having identical ordering because they represent a set of
072: * references. Hence we have to compare values here as a set, not a list.
073: *
074: * @param object an <code>Object</code> value
075: * @return a <code>boolean</code> value
076: */
077: public boolean equals(Object object) {
078: if (object == this ) {
079: return true; // succeed quickly, when possible
080: }
081: if (object instanceof IDRefs) {
082: IDRefs that = (IDRefs) object;
083: if (this .idrefs.length == that.idrefs.length) {
084: Set ourSet = new HashSet(Arrays.asList(this .idrefs));
085: Set theirSet = new HashSet(Arrays.asList(that.idrefs));
086: return ourSet.equals(theirSet);
087: } else {
088: return false;
089: }
090: } else {
091: return false;
092: }
093: }
094:
095: /**
096: * Returns the sum of the hashcodes of the underlying idrefs, an operation which is not
097: * sensitive to ordering.
098: *
099: * @return an <code>int</code> value
100: */
101: public int hashCode() {
102: int hash = 0;
103: for (int i = 0; i < idrefs.length; i++) {
104: hash += idrefs[i].hashCode();
105: }
106: return hash;
107: }
108: }
|