View Javadoc
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  package org.apache.commons.collections4.trie;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.Map;
24  import java.util.Objects;
25  import java.util.Set;
26  import java.util.SortedMap;
27  
28  import org.apache.commons.collections4.OrderedMapIterator;
29  import org.apache.commons.collections4.Trie;
30  import org.apache.commons.collections4.Unmodifiable;
31  import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
32  
33  /**
34   * An unmodifiable {@link Trie}.
35   *
36   * @param <K> the type of the keys in this map
37   * @param <V> the type of the values in this map
38   * @since 4.0
39   */
40  public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable {
41  
42      /** Serialization version */
43      private static final long serialVersionUID = -7156426030315945159L;
44  
45      /**
46       * Factory method to create an unmodifiable trie.
47       *
48       * @param <K>  the key type
49       * @param <V>  the value type
50       * @param trie  the trie to decorate, must not be null
51       * @return a new unmodifiable trie
52       * @throws NullPointerException if trie is null
53       */
54      public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
55          if (trie instanceof Unmodifiable) {
56              @SuppressWarnings("unchecked") // safe to upcast
57              final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
58              return tmpTrie;
59          }
60          return new UnmodifiableTrie<>(trie);
61      }
62  
63      /**
64       * The delegate Trie.
65       */
66      private final Trie<K, V> delegate;
67  
68      /**
69       * Constructor that wraps (not copies).
70       *
71       * @param trie  the trie to decorate, must not be null
72       * @throws NullPointerException if trie is null
73       */
74      public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
75          @SuppressWarnings("unchecked") // safe to upcast
76          final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie");
77          this.delegate = tmpTrie;
78      }
79  
80      @Override
81      public void clear() {
82          throw new UnsupportedOperationException();
83      }
84  
85      @Override
86      public Comparator<? super K> comparator() {
87          return delegate.comparator();
88      }
89  
90      @Override
91      public boolean containsKey(final Object key) {
92          return delegate.containsKey(key);
93      }
94  
95      @Override
96      public boolean containsValue(final Object value) {
97          return delegate.containsValue(value);
98      }
99  
100     @Override
101     public Set<Entry<K, V>> entrySet() {
102         return Collections.unmodifiableSet(delegate.entrySet());
103     }
104 
105     @Override
106     public boolean equals(final Object obj) {
107         return delegate.equals(obj);
108     }
109 
110     @Override
111     public K firstKey() {
112         return delegate.firstKey();
113     }
114 
115     @Override
116     public V get(final Object key) {
117         return delegate.get(key);
118     }
119 
120     @Override
121     public int hashCode() {
122         return delegate.hashCode();
123     }
124 
125     @Override
126     public SortedMap<K, V> headMap(final K toKey) {
127         return Collections.unmodifiableSortedMap(delegate.headMap(toKey));
128     }
129 
130     @Override
131     public boolean isEmpty() {
132         return delegate.isEmpty();
133     }
134 
135     @Override
136     public Set<K> keySet() {
137         return Collections.unmodifiableSet(delegate.keySet());
138     }
139 
140     @Override
141     public K lastKey() {
142         return delegate.lastKey();
143     }
144 
145     @Override
146     public OrderedMapIterator<K, V> mapIterator() {
147         final OrderedMapIterator<K, V> it = delegate.mapIterator();
148         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
149     }
150 
151     @Override
152     public K nextKey(final K key) {
153         return delegate.nextKey(key);
154     }
155 
156     @Override
157     public SortedMap<K, V> prefixMap(final K key) {
158         return Collections.unmodifiableSortedMap(delegate.prefixMap(key));
159     }
160 
161     @Override
162     public K previousKey(final K key) {
163         return delegate.previousKey(key);
164     }
165 
166     @Override
167     public V put(final K key, final V value) {
168         throw new UnsupportedOperationException();
169     }
170 
171     @Override
172     public void putAll(final Map<? extends K, ? extends V> m) {
173         throw new UnsupportedOperationException();
174     }
175 
176     @Override
177     public V remove(final Object key) {
178         throw new UnsupportedOperationException();
179     }
180 
181     @Override
182     public int size() {
183         return delegate.size();
184     }
185 
186     @Override
187     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
188         return Collections.unmodifiableSortedMap(delegate.subMap(fromKey, toKey));
189     }
190 
191     @Override
192     public SortedMap<K, V> tailMap(final K fromKey) {
193         return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
194     }
195 
196     @Override
197     public String toString() {
198         return delegate.toString();
199     }
200 
201     @Override
202     public Collection<V> values() {
203         return Collections.unmodifiableCollection(delegate.values());
204     }
205 
206 }