1 38 39 40 package com.sun.xml.fastinfoset.util; 41 42 public class CharArray implements CharSequence { 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 toString() { 78 return new String (ch, start, length); 79 } 80 81 public int hashCode() { 82 if (_hash == 0) { 83 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 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 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 subSequence(int start, int end) { 166 return new CharArray(ch, this.start + start, end - start, false); 167 } 168 } 169 | Popular Tags |