src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/CharArray.java

changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28 package com.sun.xml.internal.fastinfoset.util;
29
30 public class CharArray implements CharSequence {
31 public char[] ch;
32 public int start;
33 public int length;
34
35 protected int _hash;
36
37 protected CharArray() {
38 }
39
40 public CharArray(char[] _ch, int _start, int _length, boolean copy) {
41 set(_ch, _start, _length, copy);
42 }
43
44 public final void set(char[] _ch, int _start, int _length, boolean copy) {
45 if (copy) {
46 ch = new char[_length];
47 start = 0;
48 length = _length;
49 System.arraycopy(_ch, _start, ch, 0, _length);
50 } else {
51 ch = _ch;
52 start = _start;
53 length = _length;
54 }
55 _hash = 0;
56 }
57
58 public final void cloneArray() {
59 char[] _ch = new char[length];
60 System.arraycopy(ch, start, _ch, 0, length);
61 ch = _ch;
62 start = 0;
63 }
64
65 public String toString() {
66 return new String(ch, start, length);
67 }
68
69 public int hashCode() {
70 if (_hash == 0) {
71 // Same hash code algorithm as used for String
72 // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
73 for (int i = start; i < start + length; i++) {
74 _hash = 31*_hash + ch[i];
75 }
76 }
77 return _hash;
78 }
79
80 public static final int hashCode(char[] ch, int start, int length) {
81 // Same hash code algorithm as used for String
82 // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
83 int hash = 0;
84 for (int i = start; i < start + length; i++) {
85 hash = 31*hash + ch[i];
86 }
87
88 return hash;
89 }
90
91 public final boolean equalsCharArray(CharArray cha) {
92 if (this == cha) {
93 return true;
94 }
95
96 if (length == cha.length) {
97 int n = length;
98 int i = start;
99 int j = cha.start;
100 while (n-- != 0) {
101 if (ch[i++] != cha.ch[j++])
102 return false;
103 }
104 return true;
105 }
106
107 return false;
108 }
109
110 public final boolean equalsCharArray(char[] ch, int start, int length) {
111 if (this.length == length) {
112 int n = this.length;
113 int i = this.start;
114 int j = start;
115 while (n-- != 0) {
116 if (this.ch[i++] != ch[j++])
117 return false;
118 }
119 return true;
120 }
121
122 return false;
123 }
124
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129 if (obj instanceof CharArray) {
130 CharArray cha = (CharArray)obj;
131 if (length == cha.length) {
132 int n = length;
133 int i = start;
134 int j = cha.start;
135 while (n-- != 0) {
136 if (ch[i++] != cha.ch[j++])
137 return false;
138 }
139 return true;
140 }
141 }
142 return false;
143 }
144
145 // CharSequence interface
146
147 public final int length() {
148 return length;
149 }
150
151 public final char charAt(int index) {
152 return ch[start + index];
153 }
154
155 public final CharSequence subSequence(int start, int end) {
156 return new CharArray(ch, this.start + start, end - start, false);
157 }
158 }

mercurial