SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TransformingList.java
1/*
2 * Class: TransformingList
3 * Description: List that dynamically transforms the elements of another list.
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util;
26
27import java.util.AbstractList;
28import java.util.List;
29import java.util.Iterator;
30import java.util.ListIterator;
31
44public abstract class TransformingList<OE, IE> extends AbstractList<OE> {
45 private List<IE> fromList;
46
52 public TransformingList(List<IE> fromList) {
53 this.fromList = fromList;
54 }
55
56 public List<IE> getInnerList() {
57 return fromList;
58 }
59
66 public abstract OE convertFromInnerType(IE e);
67
74 public abstract IE convertToInnerType(OE e);
75
76 public void add(int index, OE element) {
77 IE fe = convertToInnerType(element);
78 fromList.add(fe);
79 }
80
81 @Override
82 public void clear() {
83 fromList.clear();
84 }
85
86 @Override
87 public OE get(int index) {
88 return convertFromInnerType(fromList.get(index));
89 }
90
91 public Iterator<OE> iterator() {
92 return new MyIterator(fromList.iterator());
93 }
94
95 public ListIterator<OE> listIterator() {
96 return new MyListIterator(fromList.listIterator());
97 }
98
99 public ListIterator<OE> listIterator(int index) {
100 return new MyListIterator(fromList.listIterator(index));
101 }
102
103 @Override
104 public OE remove(int index) {
105 return convertFromInnerType(fromList.remove(index));
106 }
107
108 @Override
109 public OE set(int index, OE element) {
110 IE from = convertToInnerType(element);
111 from = fromList.set(index, from);
112 return convertFromInnerType(from);
113 }
114
115 @Override
116 public int size() {
117 return fromList.size();
118 }
119
120 private class MyIterator implements Iterator<OE> {
121 private Iterator<IE> itr;
122
123 public MyIterator(Iterator<IE> itr) {
124 this.itr = itr;
125 }
126
127 public boolean hasNext() {
128 return itr.hasNext();
129 }
130
131 public OE next() {
132 return convertFromInnerType(itr.next());
133 }
134
135 public void remove() {
136 itr.remove();
137 }
138 }
139
140 private class MyListIterator implements ListIterator<OE> {
141 private ListIterator<IE> itr;
142
143 public MyListIterator(ListIterator<IE> itr) {
144 this.itr = itr;
145 }
146
147 public void add(OE o) {
148 IE fe = convertToInnerType(o);
149 itr.add(fe);
150 }
151
152 public boolean hasNext() {
153 return itr.hasNext();
154 }
155
156 public boolean hasPrevious() {
157 return itr.hasPrevious();
158 }
159
160 public OE next() {
161 return convertFromInnerType(itr.next());
162 }
163
164 public int nextIndex() {
165 return itr.nextIndex();
166 }
167
168 public OE previous() {
169 return convertFromInnerType(itr.previous());
170 }
171
172 public int previousIndex() {
173 return itr.previousIndex();
174 }
175
176 public void remove() {
177 itr.remove();
178 }
179
180 public void set(OE o) {
181 IE fe = convertToInnerType(o);
182 itr.set(fe);
183 }
184 }
185
186}
abstract OE convertFromInnerType(IE e)
Converts an element in the inner list to an element of the outer type.
TransformingList(List< IE > fromList)
Creates a new transforming list wrapping the inner list fromList.
abstract IE convertToInnerType(OE e)
Converts an element of the outer type to an element for the inner list.