|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc; |
|
27 |
|
28 import java.util.*; |
|
29 |
|
30 /** |
|
31 * The {@code HistoryList} class encapsulates a traversable list and a pointer |
|
32 * to the "current" insertion point in that list. The pointer can be |
|
33 * incremented or decremented (within the range of the list) without affecting |
|
34 * the data within the list. |
|
35 * <p> |
|
36 * When an element is added to the list, all elements from the current position |
|
37 * of the pointer to the end of the list are first removed. |
|
38 */ |
|
39 public class HistoryList<E> implements Stackable<E> { |
|
40 // |
|
41 // Instance data |
|
42 // |
|
43 |
|
44 private List<E> data = new LinkedList<E>(); |
|
45 private int pointer; |
|
46 |
|
47 // |
|
48 // Stackable methods |
|
49 // |
|
50 |
|
51 /** |
|
52 * Removes all elements from the list and sets the pointer to zero. |
|
53 */ |
|
54 @Override |
|
55 public void clear() { |
|
56 data.clear(); |
|
57 pointer = 0; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Gets the number of items before the pointer. |
|
62 */ |
|
63 @Override |
|
64 public int getCount() { |
|
65 return pointer; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Retrieves the element immediately before the position of the pointer, or |
|
70 * {@code null} if the pointer is at the beginning of the list. Note that |
|
71 * this only returns the last element of the list if the pointer is at the |
|
72 * end of the list. |
|
73 */ |
|
74 @Override |
|
75 public E peek() { |
|
76 try { |
|
77 return peekOrErr(); |
|
78 } catch (NoSuchElementException e) { |
|
79 return null; |
|
80 } |
|
81 } |
|
82 |
|
83 /** |
|
84 * Retrieves the element immediately before the position of the pointer. |
|
85 * Note that this only returns the last element of the list if the pointer |
|
86 * is at the end of the list. |
|
87 * |
|
88 * @exception NoSuchElementException |
|
89 * if the pointer is at the beginning of the list |
|
90 */ |
|
91 @Override |
|
92 public E peekOrErr() { |
|
93 try { |
|
94 return data.get(pointer - 1); |
|
95 } catch (IndexOutOfBoundsException e) { |
|
96 throw new NoSuchElementException(); |
|
97 } |
|
98 } |
|
99 |
|
100 @Override |
|
101 public E peekOrErr(int n) { |
|
102 return data.get(pointer - 1 - n); |
|
103 } |
|
104 |
|
105 /** |
|
106 * Removes the element immediately before the position of the pointer, and |
|
107 * all elements following. |
|
108 * |
|
109 * @return the element immediately before the position of the pointer, |
|
110 * prior to the removal |
|
111 * |
|
112 * @exception NoSuchElementException |
|
113 * if the pointer is at the beginning of the list |
|
114 */ |
|
115 @Override |
|
116 public E pop() { |
|
117 try { |
|
118 E datum = data.remove(pointer - 1); |
|
119 goBack(); |
|
120 clearForward(); |
|
121 return datum; |
|
122 } catch (IndexOutOfBoundsException e) { |
|
123 throw new NoSuchElementException(); |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * Removes all elements from the position of the pointer to the end of the |
|
129 * list, then adds the given element the list end of the list. |
|
130 */ |
|
131 @Override |
|
132 public void push(E element) { |
|
133 clearForward(); |
|
134 data.add(element); |
|
135 goForward(); |
|
136 } |
|
137 |
|
138 // |
|
139 // HistoryList methods |
|
140 // |
|
141 |
|
142 /** |
|
143 * Removes all elements from the position of the pointer to the end of the |
|
144 * list. |
|
145 */ |
|
146 public void clearForward() { |
|
147 for (int i = data.size() - 1; i >= pointer; i--) { |
|
148 data.remove(i); |
|
149 } |
|
150 } |
|
151 |
|
152 /** |
|
153 * Gets the full number of items in the list, regardless of the position of |
|
154 * the pointer. |
|
155 */ |
|
156 public int getFullCount() { |
|
157 return data.size(); |
|
158 } |
|
159 |
|
160 /** |
|
161 * Returns the current insertion point in the history list. |
|
162 */ |
|
163 public int getPointer() { |
|
164 return pointer; |
|
165 } |
|
166 |
|
167 /** |
|
168 * Decrements the position of the pointer. |
|
169 * |
|
170 * @exception NoSuchElementException |
|
171 * if the pointer is at the beginning of the list |
|
172 */ |
|
173 public void goBack() { |
|
174 if (pointer == 0) { |
|
175 throw new NoSuchElementException( |
|
176 "cannot move beyond beginning of list"); |
|
177 } |
|
178 pointer--; |
|
179 } |
|
180 |
|
181 /** |
|
182 * Increments the position of the pointer. |
|
183 * |
|
184 * @exception NoSuchElementException |
|
185 * if the pointer is at the end of the list |
|
186 */ |
|
187 public void goForward() { |
|
188 if (pointer == data.size()) { |
|
189 throw new NoSuchElementException( |
|
190 "cannot move beyond end of list"); |
|
191 } |
|
192 pointer++; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Retrieves the element at the position of the pointer. Note that this |
|
197 * returns the element immediately <i>after</i> the element returned by |
|
198 * {@link #peek}. |
|
199 * |
|
200 * @exception NoSuchElementException |
|
201 * if the pointer is at the end of the list |
|
202 */ |
|
203 public E peekForward() { |
|
204 try { |
|
205 return data.get(pointer); |
|
206 } catch (IndexOutOfBoundsException e) { |
|
207 throw new NoSuchElementException(); |
|
208 } |
|
209 } |
|
210 } |