aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.util; aoqi@0: aoqi@0: public class CharArray implements CharSequence { aoqi@0: public char[] ch; aoqi@0: public int start; aoqi@0: public int length; aoqi@0: aoqi@0: protected int _hash; aoqi@0: aoqi@0: protected CharArray() { aoqi@0: } aoqi@0: aoqi@0: public CharArray(char[] _ch, int _start, int _length, boolean copy) { aoqi@0: set(_ch, _start, _length, copy); aoqi@0: } aoqi@0: aoqi@0: public final void set(char[] _ch, int _start, int _length, boolean copy) { aoqi@0: if (copy) { aoqi@0: ch = new char[_length]; aoqi@0: start = 0; aoqi@0: length = _length; aoqi@0: System.arraycopy(_ch, _start, ch, 0, _length); aoqi@0: } else { aoqi@0: ch = _ch; aoqi@0: start = _start; aoqi@0: length = _length; aoqi@0: } aoqi@0: _hash = 0; aoqi@0: } aoqi@0: aoqi@0: public final void cloneArray() { aoqi@0: char[] _ch = new char[length]; aoqi@0: System.arraycopy(ch, start, _ch, 0, length); aoqi@0: ch = _ch; aoqi@0: start = 0; aoqi@0: } aoqi@0: aoqi@0: public String toString() { aoqi@0: return new String(ch, start, length); aoqi@0: } aoqi@0: aoqi@0: public int hashCode() { aoqi@0: if (_hash == 0) { aoqi@0: // Same hash code algorithm as used for String aoqi@0: // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] aoqi@0: for (int i = start; i < start + length; i++) { aoqi@0: _hash = 31*_hash + ch[i]; aoqi@0: } aoqi@0: } aoqi@0: return _hash; aoqi@0: } aoqi@0: aoqi@0: public static final int hashCode(char[] ch, int start, int length) { aoqi@0: // Same hash code algorithm as used for String aoqi@0: // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] aoqi@0: int hash = 0; aoqi@0: for (int i = start; i < start + length; i++) { aoqi@0: hash = 31*hash + ch[i]; aoqi@0: } aoqi@0: aoqi@0: return hash; aoqi@0: } aoqi@0: aoqi@0: public final boolean equalsCharArray(CharArray cha) { aoqi@0: if (this == cha) { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: if (length == cha.length) { aoqi@0: int n = length; aoqi@0: int i = start; aoqi@0: int j = cha.start; aoqi@0: while (n-- != 0) { aoqi@0: if (ch[i++] != cha.ch[j++]) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public final boolean equalsCharArray(char[] ch, int start, int length) { aoqi@0: if (this.length == length) { aoqi@0: int n = this.length; aoqi@0: int i = this.start; aoqi@0: int j = start; aoqi@0: while (n-- != 0) { aoqi@0: if (this.ch[i++] != ch[j++]) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: public boolean equals(Object obj) { aoqi@0: if (this == obj) { aoqi@0: return true; aoqi@0: } aoqi@0: if (obj instanceof CharArray) { aoqi@0: CharArray cha = (CharArray)obj; aoqi@0: if (length == cha.length) { aoqi@0: int n = length; aoqi@0: int i = start; aoqi@0: int j = cha.start; aoqi@0: while (n-- != 0) { aoqi@0: if (ch[i++] != cha.ch[j++]) aoqi@0: return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // CharSequence interface aoqi@0: aoqi@0: public final int length() { aoqi@0: return length; aoqi@0: } aoqi@0: aoqi@0: public final char charAt(int index) { aoqi@0: return ch[start + index]; aoqi@0: } aoqi@0: aoqi@0: public final CharSequence subSequence(int start, int end) { aoqi@0: return new CharArray(ch, this.start + start, end - start, false); aoqi@0: } aoqi@0: }