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 */ 017 package org.apache.commons.collections.bidimap; 018 019 import java.util.Collection; 020 import java.util.Map; 021 import java.util.Set; 022 023 import org.apache.commons.collections.BidiMap; 024 import org.apache.commons.collections.MapIterator; 025 import org.apache.commons.collections.OrderedBidiMap; 026 import org.apache.commons.collections.OrderedMapIterator; 027 import org.apache.commons.collections.Unmodifiable; 028 import org.apache.commons.collections.collection.UnmodifiableCollection; 029 import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator; 030 import org.apache.commons.collections.map.UnmodifiableEntrySet; 031 import org.apache.commons.collections.set.UnmodifiableSet; 032 033 /** 034 * Decorates another <code>OrderedBidiMap</code> to ensure it can't be altered. 035 * 036 * @since Commons Collections 3.0 037 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 038 * 039 * @author Stephen Colebourne 040 */ 041 public final class UnmodifiableOrderedBidiMap 042 extends AbstractOrderedBidiMapDecorator implements Unmodifiable { 043 044 /** The inverse unmodifiable map */ 045 private UnmodifiableOrderedBidiMap inverse; 046 047 /** 048 * Factory method to create an unmodifiable map. 049 * <p> 050 * If the map passed in is already unmodifiable, it is returned. 051 * 052 * @param map the map to decorate, must not be null 053 * @return an unmodifiable OrderedBidiMap 054 * @throws IllegalArgumentException if map is null 055 */ 056 public static OrderedBidiMap decorate(OrderedBidiMap map) { 057 if (map instanceof Unmodifiable) { 058 return map; 059 } 060 return new UnmodifiableOrderedBidiMap(map); 061 } 062 063 //----------------------------------------------------------------------- 064 /** 065 * Constructor that wraps (not copies). 066 * 067 * @param map the map to decorate, must not be null 068 * @throws IllegalArgumentException if map is null 069 */ 070 private UnmodifiableOrderedBidiMap(OrderedBidiMap map) { 071 super(map); 072 } 073 074 //----------------------------------------------------------------------- 075 public void clear() { 076 throw new UnsupportedOperationException(); 077 } 078 079 public Object put(Object key, Object value) { 080 throw new UnsupportedOperationException(); 081 } 082 083 public void putAll(Map mapToCopy) { 084 throw new UnsupportedOperationException(); 085 } 086 087 public Object remove(Object key) { 088 throw new UnsupportedOperationException(); 089 } 090 091 public Set entrySet() { 092 Set set = super.entrySet(); 093 return UnmodifiableEntrySet.decorate(set); 094 } 095 096 public Set keySet() { 097 Set set = super.keySet(); 098 return UnmodifiableSet.decorate(set); 099 } 100 101 public Collection values() { 102 Collection coll = super.values(); 103 return UnmodifiableCollection.decorate(coll); 104 } 105 106 //----------------------------------------------------------------------- 107 public Object removeValue(Object value) { 108 throw new UnsupportedOperationException(); 109 } 110 111 public MapIterator mapIterator() { 112 return orderedMapIterator(); 113 } 114 115 public BidiMap inverseBidiMap() { 116 return inverseOrderedBidiMap(); 117 } 118 119 //----------------------------------------------------------------------- 120 public OrderedMapIterator orderedMapIterator() { 121 OrderedMapIterator it = getOrderedBidiMap().orderedMapIterator(); 122 return UnmodifiableOrderedMapIterator.decorate(it); 123 } 124 125 public OrderedBidiMap inverseOrderedBidiMap() { 126 if (inverse == null) { 127 inverse = new UnmodifiableOrderedBidiMap(getOrderedBidiMap().inverseOrderedBidiMap()); 128 inverse.inverse = this; 129 } 130 return inverse; 131 } 132 133 }