View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.parser;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.commons.utils.StringUtils.*;
21  
22  import java.util.*;
23  
24  /**
25   * Identifies a position in a reader or input stream.
26   *
27   * <h5 class='section'>See Also:</h5><ul>
28   * 	<li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
29   * </ul>
30   */
31  public class Position {
32  
33  	static final Position UNKNOWN = new Position(-1);
34  
35  	int line, column, position;
36  
37  	/**
38  	 * Constructor.
39  	 *
40  	 * @param position The current byte position.
41  	 */
42  	public Position(int position) {
43  		this.line = -1;
44  		this.column = -1;
45  		this.position = position;
46  	}
47  
48  	/**
49  	 * Constructor.
50  	 *
51  	 * @param line The current line number.
52  	 * @param column The current column number.
53  	 */
54  	public Position(int line, int column) {
55  		this.line = line;
56  		this.column = column;
57  		this.position = -1;
58  	}
59  
60  	/**
61  	 * Returns the current column.
62  	 *
63  	 * @return The current column, or <c>-1</c> if not specified.
64  	 */
65  	public int getColumn() { return column; }
66  
67  	/**
68  	 * Returns the current line.
69  	 *
70  	 * @return The current line, or <c>-1</c> if not specified.
71  	 */
72  	public int getLine() { return line; }
73  
74  	/**
75  	 * Returns the current byte position.
76  	 *
77  	 * @return The current byte position, or <c>-1</c> if not specified.
78  	 */
79  	public int getPosition() { return position; }
80  
81  	@Override /* Overridden from Object */
82  	public String toString() {
83  		List<String> l = list();
84  		if (line != -1)
85  			l.add("line " + line);
86  		if (column != -1)
87  			l.add("column " + column);
88  		if (position != -1)
89  			l.add("position " + position);
90  		if (l.isEmpty())
91  			l.add("unknown");
92  		return join(l, ", ");
93  	}
94  }