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 * 25 * <p>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 * 29 * <p>If more than one field position is needed, the method 30 * {@link NumberFormat#formatToCharacterIterator(Object)} should be used. 31 */ 32 public class FieldPosition { 33 34 private int field; 35 private int beginIndex; 36 private int endIndex; 37 private Format.Field attribute; 38 39 /** 40 * Constructs a new {@code FieldPosition} for the given field id. 41 */ FieldPosition(int field)42 public FieldPosition(int field) { 43 this.field = field; 44 } 45 46 /** 47 * Constructs a new {@code FieldPosition} for the given {@code Field} attribute. 48 */ FieldPosition(Format.Field attribute)49 public FieldPosition(Format.Field attribute) { 50 this.attribute = attribute; 51 this.field = -1; 52 } 53 54 /** 55 * Constructs a new {@code FieldPosition} for the given {@code Field} attribute and field id. 56 */ FieldPosition(Format.Field attribute, int field)57 public FieldPosition(Format.Field attribute, int field) { 58 this.attribute = attribute; 59 this.field = field; 60 } 61 62 /** 63 * Compares the given object to this field position and indicates if 64 * they are equal. In order to be equal, {@code object} must be an instance 65 * of {@code FieldPosition} with the same field, begin index and end index. 66 */ equals(Object object)67 @Override public boolean equals(Object object) { 68 if (!(object instanceof FieldPosition)) { 69 return false; 70 } 71 FieldPosition pos = (FieldPosition) object; 72 return field == pos.field && this.attribute == pos.attribute && 73 beginIndex == pos.beginIndex && endIndex == pos.endIndex; 74 } 75 76 /** 77 * Returns the index of the beginning of the field. 78 */ getBeginIndex()79 public int getBeginIndex() { 80 return beginIndex; 81 } 82 83 /** 84 * Returns the index one past the end of the field. 85 */ getEndIndex()86 public int getEndIndex() { 87 return endIndex; 88 } 89 90 /** 91 * Returns the field which is being identified. 92 */ getField()93 public int getField() { 94 return field; 95 } 96 97 /** 98 * Returns the attribute which is being identified. 99 */ getFieldAttribute()100 public Format.Field getFieldAttribute() { 101 return attribute; 102 } 103 hashCode()104 @Override public int hashCode() { 105 int attributeHash = (attribute == null) ? 0 : attribute.hashCode(); 106 return attributeHash + field * 10 + beginIndex * 100 + endIndex; 107 } 108 109 /** 110 * Sets the index of the beginning of the field. 111 */ setBeginIndex(int index)112 public void setBeginIndex(int index) { 113 beginIndex = index; 114 } 115 116 /** 117 * Sets the index of the end of the field. 118 */ setEndIndex(int index)119 public void setEndIndex(int index) { 120 endIndex = index; 121 } 122 123 /** 124 * Returns the string representation of this field position. 125 */ toString()126 @Override public String toString() { 127 return getClass().getName() + "[" + 128 "attribute=" + attribute + 129 ",field=" + field + 130 ",beginIndex=" + beginIndex + 131 ",endIndex=" + endIndex + 132 "]"; 133 } 134 } 135