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.marshall.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;
036   int column;
037   @SuppressWarnings({
038      "java:S1700" // Field name intentionally matches class name
039   })
040   int position;
041
042   /**
043    * Constructor.
044    *
045    * @param position The current byte position.
046    */
047   public Position(int position) {
048      this.line = -1;
049      this.column = -1;
050      this.position = position;
051   }
052
053   /**
054    * Constructor.
055    *
056    * @param line The current line number.
057    * @param column The current column number.
058    */
059   public Position(int line, int column) {
060      this.line = line;
061      this.column = column;
062      this.position = -1;
063   }
064
065   /**
066    * Returns the current column.
067    *
068    * @return The current column, or <c>-1</c> if not specified.
069    */
070   public int getColumn() { return column; }
071
072   /**
073    * Returns the current line.
074    *
075    * @return The current line, or <c>-1</c> if not specified.
076    */
077   public int getLine() { return line; }
078
079   /**
080    * Returns the current byte position.
081    *
082    * @return The current byte position, or <c>-1</c> if not specified.
083    */
084   public int getPosition() { return position; }
085
086   @Override /* Overridden from Object */
087   public String toString() {
088      List<String> l = list();
089      if (line != -1)
090         l.add("line " + line);
091      if (column != -1)
092         l.add("column " + column);
093      if (position != -1)
094         l.add("position " + position);
095      if (l.isEmpty())
096         l.add("unknown");
097      return join(l, ", ");
098   }
099}