source: other-projects/the-macronizer/trunk/src/java/util/FixedLinkedList.java@ 35719

Last change on this file since 35719 was 35719, checked in by cstephen, 2 years ago

Add support for JSON response to direct input queries. Cleanup other components.

File size: 3.2 KB
Line 
1
2package util;
3
4import java.util.Collection;
5import java.util.LinkedList;
6
7/**
8 * A fixed sized linked list implementation.
9 * Insertions at the front and back result in overflow being removed from the
10 * back and front respectively.
11 *
12 * @author University of Waikato - Te Whare Wānanga o Waikato
13 * @version 1.0
14 * @since 2014-11-20
15 */
16public class FixedLinkedList<T> extends LinkedList<T> {
17
18 private int fixedSize;
19
20 public FixedLinkedList(int fixedSize) {
21 this.fixedSize = fixedSize;
22 }
23
24 /**
25 *add T to the front of the link list
26 * @param T
27 */
28 @Override
29 public void addFirst(T e) {
30 super.addFirst(e);
31 if (size() > fixedSize) {
32 super.removeLast();
33 }
34 }
35 /**
36 *add T to the end of the link list
37 * @param T e
38 */
39 @Override
40 public void addLast(T e) {
41 super.addLast(e);
42 if (size() > fixedSize) {
43 super.removeFirst();
44 }
45 }
46
47 /**
48 *add T to the list with super.add the test to see if the list is bigger then its fixed size if so it removes the first item from the list.
49 * @param T e
50 */
51 @Override
52 public boolean add(T e) {
53 boolean result = super.add(e);
54 if (size() > fixedSize) {
55 super.removeFirst();
56 }
57 return result;
58 }
59
60 /**
61 *add T to the list at given index with super.add then test to see if the list is bigger then its fixed size if so it removes the last item from the end of the list.
62 * @param int index to be add at
63 * @param T e
64 */
65 @Override
66 public void add(int index, T element) {
67 super.add(index, element);
68 if (size() > fixedSize) {
69 super.removeLast();
70 }
71 }
72
73 /**
74 *Adds all items in a collection to the list with super.addAll. Then test to see is the list is bigger then the its fixed size. If so it removed the fist item in the list into the list is of size fixedSize.
75 * @param Collectiont Collection of items to be add to the list.
76 */
77 @Override
78 public boolean addAll(Collection<? extends T> c) {
79 boolean result = super.addAll(c);
80 while (size() > fixedSize) {
81 super.removeFirst();
82 }
83 return result;
84 }
85
86 /**
87 *add T to the front of the link list
88 * @param T e
89 */
90 @Override
91 public void push(T e) {
92 addFirst(e);
93 }
94
95 /**
96 *add T to the end of the link list
97 * @param T e
98 */
99 @Override
100 public boolean offer(T e) {
101 boolean result = super.offer(e);
102 if (size() > fixedSize) {
103 super.removeFirst();
104 }
105 return result;
106 }
107 /**
108 *Inserts the specified element at the front of this list.
109 * @param T e
110 */
111 @Override
112 public boolean offerFirst(T e) {
113 boolean result = super.offerFirst(e);
114 if (size() > fixedSize) {
115 super.removeLast();
116 }
117 return result;
118 }
119 /**
120 *Inserts the specified element at the end of this list.
121 * @param T e
122 */
123 @Override
124 public boolean offerLast(T e) {
125 boolean result = super.offerLast(e);
126 if (size() > fixedSize) {
127 super.removeFirst();
128 }
129 return result;
130 }
131
132
133
134}
Note: See TracBrowser for help on using the repository browser.