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