001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tec.units.ri.internal.format.l10n; 031 032/* 033 * 034 * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights 035 * Reserved. Use is subject to license terms. 036 */ 037 038/* 039 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 040 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved 041 * 042 * The original version of this source code and documentation is copyrighted 043 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 044 * materials are provided under terms of a License Agreement between Taligent 045 * and Sun. This technology is protected by multiple US and International 046 * patents. This notice and attribution to Taligent may not be removed. 047 * Taligent is a registered trademark of Taligent, Inc. 048 * 049 */ 050 051/** 052 * <code>ParsePosition</code> is a simple class used by <code>Format</code> and its subclasses to keep track of the current position during parsing. 053 * The <code>parseObject</code> method in the various <code>Format</code> classes requires a <code>ParsePosition</code> object as an argument. 054 * 055 * <p> 056 * By design, as you parse through a string with different formats, you can use the same <code>ParsePosition</code>, since the index parameter records 057 * the current position. 058 * 059 * @author Mark Davis 060 * @see Format 061 */ 062 063public class ParsePosition { 064 065 /** 066 * Input: the place you start parsing. <br> 067 * Output: position where the parse stopped. This is designed to be used serially, with each call setting index up for the next one. 068 */ 069 int index = 0; 070 int errorIndex = -1; 071 072 /** 073 * Retrieve the current parse position. On input to a parse method, this is the index of the character at which parsing will begin; on output, it is 074 * the index of the character following the last character parsed. 075 */ 076 public int getIndex() { 077 return index; 078 } 079 080 /** 081 * Set the current parse position. 082 */ 083 public void setIndex(int index) { 084 this.index = index; 085 } 086 087 /** 088 * Create a new ParsePosition with the given initial index. 089 */ 090 public ParsePosition(int index) { 091 this.index = index; 092 } 093 094 /** 095 * Set the index at which a parse error occurred. Formatters should set this before returning an error code from their parseObject method. The 096 * default value is -1 if this is not set. 097 * 098 * @since 1.2 099 */ 100 public void setErrorIndex(int ei) { 101 errorIndex = ei; 102 } 103 104 /** 105 * Retrieve the index at which an error occurred, or -1 if the error index has not been set. 106 * 107 * @since 1.2 108 */ 109 public int getErrorIndex() { 110 return errorIndex; 111 } 112 113 /** 114 * Overrides equals 115 */ 116 public boolean equals(Object obj) { 117 if (obj == null) 118 return false; 119 if (!(obj instanceof ParsePosition)) 120 return false; 121 ParsePosition other = (ParsePosition) obj; 122 return (index == other.index && errorIndex == other.errorIndex); 123 } 124 125 /** 126 * Returns a hash code for this ParsePosition. 127 * 128 * @return a hash code value for this object 129 */ 130 public int hashCode() { 131 return (errorIndex << 16) | index; 132 } 133 134 /** 135 * Return a string representation of this ParsePosition. 136 * 137 * @return a string representation of this object 138 */ 139 public String toString() { 140 return getClass().getName() + "[index=" + index + ",errorIndex=" + errorIndex + ']'; 141 } 142}