01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /*
18: * @author Oleg V. Khaschansky
19: * @version $Revision$
20: */
21:
22: package java.awt.font;
23:
24: import org.apache.harmony.misc.HashCode;
25:
26: public final class TextHitInfo {
27: private int charIdx; // Represents character index in the line
28: private boolean isTrailing;
29:
30: private TextHitInfo(int idx, boolean isTrailing) {
31: charIdx = idx;
32: this .isTrailing = isTrailing;
33: }
34:
35: @Override
36: public String toString() {
37: return new String("TextHitInfo[" + charIdx + ", " + //$NON-NLS-1$ //$NON-NLS-2$
38: (isTrailing ? "Trailing" : "Leading") + "]" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
39: );
40: }
41:
42: @Override
43: public boolean equals(Object obj) {
44: if (obj instanceof TextHitInfo) {
45: return equals((TextHitInfo) obj);
46: }
47: return false;
48: }
49:
50: public boolean equals(TextHitInfo thi) {
51: return thi != null && thi.charIdx == charIdx
52: && thi.isTrailing == isTrailing;
53: }
54:
55: public TextHitInfo getOffsetHit(int offset) {
56: return new TextHitInfo(charIdx + offset, isTrailing);
57: }
58:
59: public TextHitInfo getOtherHit() {
60: return isTrailing ? new TextHitInfo(charIdx + 1, false)
61: : new TextHitInfo(charIdx - 1, true);
62: }
63:
64: public boolean isLeadingEdge() {
65: return !isTrailing;
66: }
67:
68: @Override
69: public int hashCode() {
70: return HashCode.combine(charIdx, isTrailing);
71: }
72:
73: public int getInsertionIndex() {
74: return isTrailing ? charIdx + 1 : charIdx;
75: }
76:
77: public int getCharIndex() {
78: return charIdx;
79: }
80:
81: public static TextHitInfo trailing(int charIndex) {
82: return new TextHitInfo(charIndex, true);
83: }
84:
85: public static TextHitInfo leading(int charIndex) {
86: return new TextHitInfo(charIndex, false);
87: }
88:
89: public static TextHitInfo beforeOffset(int offset) {
90: return new TextHitInfo(offset - 1, true);
91: }
92:
93: public static TextHitInfo afterOffset(int offset) {
94: return new TextHitInfo(offset, false);
95: }
96: }
|