KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > util > CharArray


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.util;
41
42 public class CharArray implements CharSequence JavaDoc {
43     public char[] ch;
44     public int start;
45     public int length;
46                                                                                 
47     protected int _hash;
48
49     protected CharArray() {
50     }
51     
52     public CharArray(char[] _ch, int _start, int _length, boolean copy) {
53         set(_ch, _start, _length, copy);
54     }
55     
56     public final void set(char[] _ch, int _start, int _length, boolean copy) {
57         if (copy) {
58             ch = new char[_length];
59             start = 0;
60             length = _length;
61             System.arraycopy(_ch, _start, ch, 0, _length);
62         } else {
63             ch = _ch;
64             start = _start;
65             length = _length;
66         }
67         _hash = 0;
68     }
69     
70     public final void cloneArray() {
71         char[] _ch = new char[length];
72         System.arraycopy(ch, start, _ch, 0, length);
73         ch = _ch;
74         start = 0;
75     }
76                                                                                 
77     public String JavaDoc toString() {
78         return new String JavaDoc(ch, start, length);
79     }
80                                                                                 
81     public int hashCode() {
82         if (_hash == 0) {
83             // Same hash code algorithm as used for String
84
// s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
85
for (int i = start; i < start + length; i++) {
86                 _hash = 31*_hash + ch[i];
87             }
88         }
89         return _hash;
90     }
91
92     public static final int hashCode(char[] ch, int start, int length) {
93         int hash = 0;
94         for (int i = start; i < start + length; i++) {
95             hash = 31*hash + ch[i];
96         }
97         
98         return hash;
99     }
100     
101     public final boolean equalsCharArray(CharArray cha) {
102         if (this == cha) {
103             return true;
104         }
105         
106         if (length == cha.length) {
107             int n = length;
108             int i = start;
109             int j = cha.start;
110             while (n-- != 0) {
111                 if (ch[i++] != cha.ch[j++])
112                     return false;
113             }
114             return true;
115         }
116         
117         return false;
118     }
119
120     public final boolean equalsCharArray(char[] ch, int start, int length) {
121         if (this.length == length) {
122             int n = this.length;
123             int i = this.start;
124             int j = start;
125             while (n-- != 0) {
126                 if (this.ch[i++] != ch[j++])
127                     return false;
128             }
129             return true;
130         }
131         
132         return false;
133     }
134     
135     public boolean equals(Object JavaDoc obj) {
136         if (this == obj) {
137             return true;
138         }
139         if (obj instanceof CharArray) {
140             CharArray cha = (CharArray)obj;
141             if (length == cha.length) {
142                 int n = length;
143                 int i = start;
144                 int j = cha.start;
145                 while (n-- != 0) {
146                     if (ch[i++] != cha.ch[j++])
147                         return false;
148                 }
149                 return true;
150             }
151         }
152         return false;
153     }
154
155     // CharSequence interface
156

157     public final int length() {
158         return length;
159     }
160
161     public final char charAt(int index) {
162         return ch[start + index];
163     }
164
165     public final CharSequence JavaDoc subSequence(int start, int end) {
166         return new CharArray(ch, this.start + start, end - start, false);
167     }
168 }
169
Popular Tags