|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package io.microsphere.collection; |
| 19 | + |
| 20 | +import java.io.Serializable; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Queue; |
| 24 | +import java.util.Spliterator; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.function.Predicate; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import static io.microsphere.collection.CollectionUtils.unmodifiableIterator; |
| 30 | + |
| 31 | +/** |
| 32 | + * An unmodifiable view of a {@link Queue} which wraps a given delegate and prevents any modifications to it. |
| 33 | + * <p> |
| 34 | + * All mutation operations like {@link #add(Object)}, {@link #remove()}, or {@link #clear()} will throw an |
| 35 | + * {@link UnsupportedOperationException}. |
| 36 | + * </p> |
| 37 | + * |
| 38 | + * @param <E> the type of elements held in this queue |
| 39 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/> |
| 40 | + * @since 1.0.0 |
| 41 | + */ |
| 42 | +public class UnmodifiableQueue<E> implements Queue<E>, Serializable { |
| 43 | + |
| 44 | + private static final long serialVersionUID = -1L; |
| 45 | + |
| 46 | + private final Queue<E> delegate; |
| 47 | + |
| 48 | + UnmodifiableQueue(Queue<E> queue) { |
| 49 | + this.delegate = queue; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public int size() { |
| 54 | + return delegate.size(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean isEmpty() { |
| 59 | + return delegate.isEmpty(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean contains(Object o) { |
| 64 | + return delegate.contains(o); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Iterator<E> iterator() { |
| 69 | + return unmodifiableIterator(delegate.iterator()); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Object[] toArray() { |
| 74 | + return delegate.toArray(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public <T> T[] toArray(T[] a) { |
| 79 | + return delegate.toArray(a); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean add(E e) { |
| 84 | + throw new UnsupportedOperationException(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean remove(Object o) { |
| 89 | + throw new UnsupportedOperationException(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean offer(E e) { |
| 94 | + throw new UnsupportedOperationException(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public E remove() { |
| 99 | + throw new UnsupportedOperationException(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public E poll() { |
| 104 | + throw new UnsupportedOperationException(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public E element() { |
| 109 | + return delegate.element(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public E peek() { |
| 114 | + return delegate.peek(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean containsAll(Collection<?> c) { |
| 119 | + return delegate.containsAll(c); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean addAll(Collection<? extends E> c) { |
| 124 | + throw new UnsupportedOperationException(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean removeAll(Collection<?> c) { |
| 129 | + throw new UnsupportedOperationException(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean removeIf(Predicate<? super E> filter) { |
| 134 | + throw new UnsupportedOperationException(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean retainAll(Collection<?> c) { |
| 139 | + throw new UnsupportedOperationException(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void clear() { |
| 144 | + throw new UnsupportedOperationException(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean equals(Object o) { |
| 149 | + if (this == o) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + return delegate.equals(o); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + return delegate.hashCode(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public Spliterator<E> spliterator() { |
| 162 | + return delegate.spliterator(); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public Stream<E> stream() { |
| 167 | + return delegate.stream(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public Stream<E> parallelStream() { |
| 172 | + return delegate.parallelStream(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void forEach(Consumer<? super E> action) { |
| 177 | + delegate.forEach(action); |
| 178 | + } |
| 179 | +} |
0 commit comments