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 static org.apache.juneau.internal.CollectionUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.common.internal.*;
020
021/**
022 * Identifies a position in a reader or input stream.
023 *
024 * <h5 class='section'>See Also:</h5><ul>
025 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a>
026 * </ul>
027 */
028public class Position {
029
030   static final Position UNKNOWN = new Position(-1);
031
032   int line, column, position;
033
034   /**
035    * Constructor.
036    *
037    * @param line The current line number.
038    * @param column The current column number.
039    */
040   public Position(int line, int column) {
041      this.line = line;
042      this.column = column;
043      this.position = -1;
044   }
045
046   /**
047    * Constructor.
048    *
049    * @param position The current byte position.
050    */
051   public Position(int position) {
052      this.line = -1;
053      this.column = -1;
054      this.position = position;
055   }
056
057   @Override /* Object */
058   public String toString() {
059      List<String> l = list();
060      if (line != -1)
061         l.add("line " + line);
062      if (column != -1)
063         l.add("column " + column);
064      if (position != -1)
065         l.add("position " + position);
066      if (l.isEmpty())
067         l.add("unknown");
068      return StringUtils.join(l, ", ");
069   }
070
071   /**
072    * Returns the current line.
073    *
074    * @return The current line, or <c>-1</c> if not specified.
075    */
076   public int getLine() {
077      return line;
078   }
079
080   /**
081    * Returns the current column.
082    *
083    * @return The current column, or <c>-1</c> if not specified.
084    */
085   public int getColumn() {
086      return column;
087   }
088
089   /**
090    * Returns the current byte position.
091    *
092    * @return The current byte position, or <c>-1</c> if not specified.
093    */
094   public int getPosition() {
095      return position;
096   }
097}