001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.trie; 018 019import java.io.Serializable; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.Comparator; 023import java.util.Map; 024import java.util.Objects; 025import java.util.Set; 026import java.util.SortedMap; 027 028import org.apache.commons.collections4.OrderedMapIterator; 029import org.apache.commons.collections4.Trie; 030import org.apache.commons.collections4.Unmodifiable; 031import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator; 032 033/** 034 * An unmodifiable {@link Trie}. 035 * 036 * @param <K> the type of the keys in this map 037 * @param <V> the type of the values in this map 038 * @since 4.0 039 */ 040public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable { 041 042 /** Serialization version */ 043 private static final long serialVersionUID = -7156426030315945159L; 044 045 /** 046 * Factory method to create an unmodifiable trie. 047 * 048 * @param <K> the key type 049 * @param <V> the value type 050 * @param trie the trie to decorate, must not be null 051 * @return a new unmodifiable trie 052 * @throws NullPointerException if trie is null 053 */ 054 public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) { 055 if (trie instanceof Unmodifiable) { 056 @SuppressWarnings("unchecked") // safe to upcast 057 final Trie<K, V> tmpTrie = (Trie<K, V>) trie; 058 return tmpTrie; 059 } 060 return new UnmodifiableTrie<>(trie); 061 } 062 063 /** 064 * The delegate Trie. 065 */ 066 private final Trie<K, V> delegate; 067 068 /** 069 * Constructor that wraps (not copies). 070 * 071 * @param trie the trie to decorate, must not be null 072 * @throws NullPointerException if trie is null 073 */ 074 public UnmodifiableTrie(final Trie<K, ? extends V> trie) { 075 @SuppressWarnings("unchecked") // safe to upcast 076 final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie"); 077 this.delegate = tmpTrie; 078 } 079 080 @Override 081 public void clear() { 082 throw new UnsupportedOperationException(); 083 } 084 085 @Override 086 public Comparator<? super K> comparator() { 087 return delegate.comparator(); 088 } 089 090 @Override 091 public boolean containsKey(final Object key) { 092 return delegate.containsKey(key); 093 } 094 095 @Override 096 public boolean containsValue(final Object value) { 097 return delegate.containsValue(value); 098 } 099 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}