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.map; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.io.Serializable; 023import java.util.Collection; 024import java.util.Map; 025import java.util.Set; 026 027import org.apache.commons.collections4.OrderedMap; 028import org.apache.commons.collections4.OrderedMapIterator; 029import org.apache.commons.collections4.Unmodifiable; 030import org.apache.commons.collections4.collection.UnmodifiableCollection; 031import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator; 032import org.apache.commons.collections4.set.UnmodifiableSet; 033 034/** 035 * Decorates another <code>OrderedMap</code> to ensure it can't be altered. 036 * <p> 037 * This class is Serializable from Commons Collections 3.1. 038 * <p> 039 * Attempts to modify it will result in an UnsupportedOperationException. 040 * 041 * @param <K> the type of the keys in this map 042 * @param <V> the type of the values in this map 043 * @since 3.0 044 */ 045public final class UnmodifiableOrderedMap<K, V> extends AbstractOrderedMapDecorator<K, V> implements 046 Unmodifiable, Serializable { 047 048 /** Serialization version */ 049 private static final long serialVersionUID = 8136428161720526266L; 050 051 /** 052 * Factory method to create an unmodifiable sorted map. 053 * 054 * @param <K> the key type 055 * @param <V> the value type 056 * @param map the map to decorate, must not be null 057 * @return a new ordered map 058 * @throws NullPointerException if map is null 059 * @since 4.0 060 */ 061 public static <K, V> OrderedMap<K, V> unmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) { 062 if (map instanceof Unmodifiable) { 063 @SuppressWarnings("unchecked") // safe to upcast 064 final OrderedMap<K, V> tmpMap = (OrderedMap<K, V>) map; 065 return tmpMap; 066 } 067 return new UnmodifiableOrderedMap<>(map); 068 } 069 070 //----------------------------------------------------------------------- 071 /** 072 * Constructor that wraps (not copies). 073 * 074 * @param map the map to decorate, must not be null 075 * @throws NullPointerException if map is null 076 */ 077 @SuppressWarnings("unchecked") // safe to upcast 078 private UnmodifiableOrderedMap(final OrderedMap<? extends K, ? extends V> map) { 079 super((OrderedMap<K, V>) map); 080 } 081 082 //----------------------------------------------------------------------- 083 /** 084 * Write the map out using a custom routine. 085 * 086 * @param out the output stream 087 * @throws IOException if an error occurs while writing to the stream 088 * @since 3.1 089 */ 090 private void writeObject(final ObjectOutputStream out) throws IOException { 091 out.defaultWriteObject(); 092 out.writeObject(map); 093 } 094 095 /** 096 * Read the map in using a custom routine. 097 * 098 * @param in the input stream 099 * @throws IOException if an error occurs while reading from the stream 100 * @throws ClassNotFoundException if an object read from the stream can not be loaded 101 * @since 3.1 102 */ 103 @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 104 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 105 in.defaultReadObject(); 106 map = (Map<K, V>) in.readObject(); // (1) 107 } 108 109 //----------------------------------------------------------------------- 110 @Override 111 public OrderedMapIterator<K, V> mapIterator() { 112 final OrderedMapIterator<K, V> it = decorated().mapIterator(); 113 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it); 114 } 115 116 @Override 117 public void clear() { 118 throw new UnsupportedOperationException(); 119 } 120 121 @Override 122 public V put(final K key, final V value) { 123 throw new UnsupportedOperationException(); 124 } 125 126 @Override 127 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 128 throw new UnsupportedOperationException(); 129 } 130 131 @Override 132 public V remove(final Object key) { 133 throw new UnsupportedOperationException(); 134 } 135 136 @Override 137 public Set<Map.Entry<K, V>> entrySet() { 138 final Set<Map.Entry<K, V>> set = super.entrySet(); 139 return UnmodifiableEntrySet.unmodifiableEntrySet(set); 140 } 141 142 @Override 143 public Set<K> keySet() { 144 final Set<K> set = super.keySet(); 145 return UnmodifiableSet.unmodifiableSet(set); 146 } 147 148 @Override 149 public Collection<V> values() { 150 final Collection<V> coll = super.values(); 151 return UnmodifiableCollection.unmodifiableCollection(coll); 152 } 153 154}