001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.parser; 014 015import java.util.*; 016 017import org.apache.juneau.internal.*; 018 019/** 020 * Identifies a position in a reader or input stream. 021 */ 022public class Position { 023 024 static final Position UNKNOWN = new Position(-1); 025 026 int line, column, position; 027 028 /** 029 * Constructor. 030 * 031 * @param line The current line number. 032 * @param column The current column number. 033 */ 034 public Position(int line, int column) { 035 this.line = line; 036 this.column = column; 037 this.position = -1; 038 } 039 040 /** 041 * Constructor. 042 * 043 * @param position The current byte position. 044 */ 045 public Position(int position) { 046 this.line = -1; 047 this.column = -1; 048 this.position = position; 049 } 050 051 @Override /* Object */ 052 public String toString() { 053 List<String> l = new ArrayList<>(); 054 if (line != -1) 055 l.add("line " + line); 056 if (column != -1) 057 l.add("column " + column); 058 if (position != -1) 059 l.add("position " + position); 060 if (l.isEmpty()) 061 l.add("unknown"); 062 return StringUtils.join(l, ", "); 063 } 064 065 /** 066 * Returns the current line. 067 * 068 * @return The current line, or <code>-1</code> if not specified. 069 */ 070 public int getLine() { 071 return line; 072 } 073 074 /** 075 * Returns the current column. 076 * 077 * @return The current column, or <code>-1</code> if not specified. 078 */ 079 public int getColumn() { 080 return column; 081 } 082 083 /** 084 * Returns the current byte position. 085 * 086 * @return The current byte position, or <code>-1</code> if not specified. 087 */ 088 public int getPosition() { 089 return position; 090 } 091}