View Javadoc
1   package io.jawk.intermediate;
2   
3   import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4   
5   /*-
6    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
7    * Jawk
8    * ჻჻჻჻჻჻
9    * Copyright (C) 2006 - 2026 MetricsHub
10   * ჻჻჻჻჻჻
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU General Lesser Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
24   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
25   */
26  
27  /**
28   * Marks a position within the tuple list (queue).
29   *
30   * @author Danny Daglas
31   */
32  public class PositionTracker {
33  
34  	private int idx = 0;
35  	private final java.util.List<Tuple> queue;
36  	private Tuple tuple;
37  
38  	/**
39  	 * Creates a tracker positioned at the first tuple in the queue.
40  	 *
41  	 * @param queue Tuple stream to traverse
42  	 */
43  	@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "PositionTracker must iterate over the shared tuple list")
44  	public PositionTracker(java.util.List<Tuple> queue) {
45  		this.queue = queue;
46  		this.tuple = queue.isEmpty() ? null : queue.get(idx);
47  	}
48  
49  	/**
50  	 * Indicates whether the tracker has moved past the last tuple.
51  	 *
52  	 * @return {@code true} when no current tuple remains
53  	 */
54  	public boolean isEOF() {
55  		return idx >= queue.size();
56  	}
57  
58  	/**
59  	 * Advances to the next tuple in sequence.
60  	 */
61  	public void next() {
62  		++idx;
63  		tuple = tuple.getNext();
64  	}
65  
66  	/**
67  	 * Jumps directly to the tuple identified by the supplied address.
68  	 *
69  	 * @param address Address to jump to
70  	 */
71  	public void jump(Address address) {
72  		idx = address.index();
73  		tuple = queue.get(idx);
74  	}
75  
76  	@Override
77  	public String toString() {
78  		return "[" + idx + "]-->" + tuple.toString();
79  	}
80  
81  	/**
82  	 * Returns the opcode of the current tuple.
83  	 *
84  	 * @return Current opcode
85  	 */
86  	public Opcode opcode() {
87  		return tuple.getOpcode();
88  	}
89  
90  	/**
91  	 * Returns the source line number associated with the current tuple.
92  	 *
93  	 * @return Tuple source line number
94  	 */
95  	public int lineNumber() {
96  		return tuple.getLineNumber();
97  	}
98  
99  	/**
100 	 * Returns the current tuple index.
101 	 *
102 	 * @return Current queue index
103 	 */
104 	public int currentIndex() {
105 		return idx;
106 	}
107 
108 	/**
109 	 * Returns the current typed tuple.
110 	 *
111 	 * @return current tuple
112 	 */
113 	@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Tuple mutation is limited to package-internal stream construction and deferred address resolution")
114 	public Tuple current() {
115 		return tuple;
116 	}
117 
118 	/**
119 	 * Jumps directly to the tuple at the supplied queue index.
120 	 *
121 	 * @param index Tuple index to jump to
122 	 */
123 	public void jump(int index) {
124 		this.idx = index;
125 		tuple = queue.get(this.idx);
126 	}
127 }