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