1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * 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 package java.text; 19 20 /** 21 * Identifies fields in formatted strings. If a {@code FieldPosition} is passed 22 * to the format method with such a parameter, then the indices will be set to 23 * the start and end indices of the field in the formatted string. 24 * <p> 25 * A {@code FieldPosition} can be created by using the integer constants in the 26 * various format classes (for example {@code NumberFormat.INTEGER_FIELD}) or 27 * one of the fields of type {@code Format.Field}. 28 * <p> 29 * If more than one field information is needed, the method 30 * {@link NumberFormat#formatToCharacterIterator(Object)} should be used. 31 */ 32 public class FieldPosition { 33 34 private int myField, beginIndex, endIndex; 35 36 private Format.Field myAttribute; 37 38 /** 39 * Constructs a new {@code FieldPosition} for the specified field. 40 * 41 * @param field 42 * the field to identify. 43 */ FieldPosition(int field)44 public FieldPosition(int field) { 45 myField = field; 46 } 47 48 /** 49 * Constructs a new {@code FieldPosition} for the specified {@code Field} 50 * attribute. 51 * 52 * @param attribute 53 * the field attribute to identify. 54 */ FieldPosition(Format.Field attribute)55 public FieldPosition(Format.Field attribute) { 56 myAttribute = attribute; 57 myField = -1; 58 } 59 60 /** 61 * Constructs a new {@code FieldPosition} for the specified {@code Field} 62 * attribute and field id. 63 * 64 * @param attribute 65 * the field attribute to identify. 66 * @param field 67 * the field to identify. 68 */ FieldPosition(Format.Field attribute, int field)69 public FieldPosition(Format.Field attribute, int field) { 70 myAttribute = attribute; 71 myField = field; 72 } 73 clear()74 void clear() { 75 beginIndex = endIndex = 0; 76 } 77 78 /** 79 * Compares the specified object to this field position and indicates if 80 * they are equal. In order to be equal, {@code object} must be an instance 81 * of {@code FieldPosition} with the same field, begin index and end index. 82 * 83 * @param object 84 * the object to compare with this object. 85 * @return {@code true} if the specified object is equal to this field 86 * position; {@code false} otherwise. 87 * @see #hashCode 88 */ 89 @Override equals(Object object)90 public boolean equals(Object object) { 91 if (!(object instanceof FieldPosition)) { 92 return false; 93 } 94 FieldPosition pos = (FieldPosition) object; 95 return myField == pos.myField && myAttribute == pos.myAttribute 96 && beginIndex == pos.beginIndex && endIndex == pos.endIndex; 97 } 98 99 /** 100 * Returns the index of the beginning of the field. 101 * 102 * @return the first index of the field. 103 */ getBeginIndex()104 public int getBeginIndex() { 105 return beginIndex; 106 } 107 108 /** 109 * Returns the index one past the end of the field. 110 * 111 * @return one past the index of the last character in the field. 112 */ getEndIndex()113 public int getEndIndex() { 114 return endIndex; 115 } 116 117 /** 118 * Returns the field which is being identified. 119 * 120 * @return the field constant. 121 */ getField()122 public int getField() { 123 return myField; 124 } 125 126 /** 127 * Returns the attribute which is being identified. 128 * 129 * @return the field. 130 */ getFieldAttribute()131 public Format.Field getFieldAttribute() { 132 return myAttribute; 133 } 134 135 @Override hashCode()136 public int hashCode() { 137 int attributeHash = (myAttribute == null) ? 0 : myAttribute.hashCode(); 138 return attributeHash + myField * 10 + beginIndex * 100 + endIndex; 139 } 140 141 /** 142 * Sets the index of the beginning of the field. 143 * 144 * @param index 145 * the index of the first character in the field. 146 */ setBeginIndex(int index)147 public void setBeginIndex(int index) { 148 beginIndex = index; 149 } 150 151 /** 152 * Sets the index of the end of the field. 153 * 154 * @param index 155 * one past the index of the last character in the field. 156 */ setEndIndex(int index)157 public void setEndIndex(int index) { 158 endIndex = index; 159 } 160 161 /** 162 * Returns the string representation of this field position. 163 * 164 * @return the string representation of this field position. 165 */ 166 @Override toString()167 public String toString() { 168 return getClass().getName() + "[attribute=" + myAttribute + ", field=" 169 + myField + ", beginIndex=" + beginIndex + ", endIndex=" 170 + endIndex + "]"; 171 } 172 } 173