@Override public int hashCode() { int h = hash; final int length = endIndex - beginIndex; if (h == 0 && length > 0) { for (int i = 0; i < length; i++) h = 31 * h + charAt(i); hash = h; } return h; }
@Override public char charAt(int index) { if (index < 0 || index >= length()) throw new StringIndexOutOfBoundsException(index); return s.charAt(beginIndex + index); }
@Override public CharSequence subSequence(int start, int end) { int length = endIndex - beginIndex; if (length < 0) throw new StringIndexOutOfBoundsException("beginIndex > endIndex"); if (beginIndex < 0) throw new StringIndexOutOfBoundsException(beginIndex); if (endIndex >= length) throw new StringIndexOutOfBoundsException(endIndex); return new Substring(s, beginIndex + start, beginIndex + end); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj instanceof Substring) { final CharSequence other = (CharSequence) obj; int n = endIndex - beginIndex; if (n == other.length()) { int i = 0; while (n-- != 0) { if (charAt(i) != other.charAt(i)) return false; i++; } return true; } } return false; }