2
* Copyright Terracotta, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package net.sf.ehcache.util;
19
import java.util.Collection;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.ListIterator;
26
* Wraps a set to provide a list interface.
27
* All list methods not application to set throws an {@link UnsupportedOperationException}
29
* @author Chris Dennis
32
public class SetAsList<E> implements List<E> {
34
private final Set<E> set;
35
private transient Object[] array;
40
public SetAsList(Set<E> set) {
54
public boolean isEmpty() {
61
public boolean contains(Object o) {
62
return set.contains(o);
68
public Iterator<E> iterator() {
69
return set.iterator();
75
public Object[] toArray() {
82
public <T> T[] toArray(T[] a) {
83
return set.toArray(a);
89
public boolean add(E e) {
96
public boolean remove(Object o) {
103
public boolean containsAll(Collection<?> c) {
104
return set.containsAll(c);
110
public boolean addAll(Collection<? extends E> c) {
111
return set.addAll(c);
115
* Does not support List methods {@link UnsupportedOperationException}.
117
public boolean addAll(int index, Collection<? extends E> c) {
118
throw new UnsupportedOperationException("Delegates to set, operation not supported");
124
public boolean removeAll(Collection<?> c) {
125
return set.removeAll(c);
131
public boolean retainAll(Collection<?> c) {
132
return set.retainAll(c);
138
public void clear() {
143
* Does not support List methods {@link UnsupportedOperationException}.
147
public E get(int index) {
148
if (this.array == null) {
149
this.array = toArray();
151
if (array.length <= index) {
152
throw new IndexOutOfBoundsException();
154
return (E) this.array[index];
158
* Does not support List methods {@link UnsupportedOperationException}.
160
public E set(int index, E element) {
161
throw new UnsupportedOperationException("Delegates to set, operation not supported");
165
* Does not support List methods {@link UnsupportedOperationException}.
167
public void add(int index, E element) {
168
throw new UnsupportedOperationException("Delegates to set, operation not supported");
172
* Does not support List methods {@link UnsupportedOperationException}.
174
public E remove(int index) {
175
throw new UnsupportedOperationException("Delegates to set, operation not supported");
179
* Does not support List methods {@link UnsupportedOperationException}.
181
public int indexOf(Object o) {
182
throw new UnsupportedOperationException("Delegates to set, operation not supported");
186
* Does not support List methods {@link UnsupportedOperationException}.
188
public int lastIndexOf(Object o) {
189
throw new UnsupportedOperationException("Delegates to set, operation not supported");
193
* Does not support List methods {@link UnsupportedOperationException}.
195
public ListIterator<E> listIterator() {
196
throw new UnsupportedOperationException("Delegates to set, operation not supported");
200
* Does not support List methods {@link UnsupportedOperationException}.
202
public ListIterator<E> listIterator(int index) {
203
throw new UnsupportedOperationException("Delegates to set, operation not supported");
207
* Does not support List methods {@link UnsupportedOperationException}.
209
public List<E> subList(int fromIndex, int toIndex) {
210
throw new UnsupportedOperationException("Delegates to set, operation not supported");