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      private final Trie<K, V> delegate;
64  
65      /**
66       * Constructor that wraps (not copies).
67       *
68       * @param trie  the trie to decorate, must not be null
69       * @throws NullPointerException if trie is null
70       */
71      public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
72          @SuppressWarnings("unchecked") // safe to upcast
73          final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie");
74          this.delegate = tmpTrie;
75      }
76  
77      @Override
78      public void clear() {
79          throw new UnsupportedOperationException();
80      }
81  
82      @Override
83      public Comparator<? super K> comparator() {
84          return delegate.comparator();
85      }
86  
87      @Override
88      public boolean containsKey(final Object key) {
89          return delegate.containsKey(key);
90      }
91  
92      @Override
93      public boolean containsValue(final Object value) {
94          return delegate.containsValue(value);
95      }
96  
97      @Override
98      public Set<Entry<K, V>> entrySet() {
99          return Collections.unmodifiableSet(delegate.entrySet());
100     }
101 
102     @Override
103     public boolean equals(final Object obj) {
104         return delegate.equals(obj);
105     }
106 
107     @Override
108     public K firstKey() {
109         return delegate.firstKey();
110     }
111 
112     @Override
113     public V get(final Object key) {
114         return delegate.get(key);
115     }
116 
117     @Override
118     public int hashCode() {
119         return delegate.hashCode();
120     }
121 
122     @Override
123     public SortedMap<K, V> headMap(final K toKey) {
124         return Collections.unmodifiableSortedMap(delegate.headMap(toKey));
125     }
126 
127     @Override
128     public boolean isEmpty() {
129         return delegate.isEmpty();
130     }
131 
132     @Override
133     public Set<K> keySet() {
134         return Collections.unmodifiableSet(delegate.keySet());
135     }
136 
137     @Override
138     public K lastKey() {
139         return delegate.lastKey();
140     }
141 
142     @Override
143     public OrderedMapIterator<K, V> mapIterator() {
144         final OrderedMapIterator<K, V> it = delegate.mapIterator();
145         return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
146     }
147 
148     @Override
149     public K nextKey(final K key) {
150         return delegate.nextKey(key);
151     }
152 
153     @Override
154     public SortedMap<K, V> prefixMap(final K key) {
155         return Collections.unmodifiableSortedMap(delegate.prefixMap(key));
156     }
157 
158     @Override
159     public K previousKey(final K key) {
160         return delegate.previousKey(key);
161     }
162 
163     @Override
164     public V put(final K key, final V value) {
165         throw new UnsupportedOperationException();
166     }
167 
168     @Override
169     public void putAll(final Map<? extends K, ? extends V> m) {
170         throw new UnsupportedOperationException();
171     }
172 
173     @Override
174     public V remove(final Object key) {
175         throw new UnsupportedOperationException();
176     }
177 
178     @Override
179     public int size() {
180         return delegate.size();
181     }
182 
183     @Override
184     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
185         return Collections.unmodifiableSortedMap(delegate.subMap(fromKey, toKey));
186     }
187 
188     @Override
189     public SortedMap<K, V> tailMap(final K fromKey) {
190         return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
191     }
192 
193     @Override
194     public String toString() {
195         return delegate.toString();
196     }
197 
198     @Override
199     public Collection<V> values() {
200         return Collections.unmodifiableCollection(delegate.values());
201     }
202 
203 }