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.Iterator; 024import java.util.Map; 025 026import org.apache.commons.collections4.Predicate; 027 028/** 029 * Decorates another <code>Map</code> to validate that additions 030 * match a specified predicate. 031 * <p> 032 * This map exists to provide validation for the decorated map. 033 * It is normally created to decorate an empty map. 034 * If an object cannot be added to the map, an IllegalArgumentException is thrown. 035 * <p> 036 * One usage would be to ensure that no null keys are added to the map. 037 * <pre>Map map = PredicatedSet.decorate(new HashMap(), NotNullPredicate.INSTANCE, null);</pre> 038 * <p> 039 * <strong>Note that PredicatedMap is not synchronized and is not thread-safe.</strong> 040 * If you wish to use this map from multiple threads concurrently, you must use 041 * appropriate synchronization. The simplest approach is to wrap this map 042 * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw 043 * exceptions when accessed by concurrent threads without synchronization. 044 * <p> 045 * This class is Serializable from Commons Collections 3.1. 046 * 047 * @param <K> the type of the keys in this map 048 * @param <V> the type of the values in this map 049 * @since 3.0 050 */ 051public class PredicatedMap<K, V> 052 extends AbstractInputCheckedMapDecorator<K, V> 053 implements Serializable { 054 055 /** Serialization version */ 056 private static final long serialVersionUID = 7412622456128415156L; 057 058 /** The key predicate to use */ 059 protected final Predicate<? super K> keyPredicate; 060 061 /** The value predicate to use */ 062 protected final Predicate<? super V> valuePredicate; 063 064 /** 065 * Factory method to create a predicated (validating) map. 066 * <p> 067 * If there are any elements already in the list being decorated, they 068 * are validated. 069 * 070 * @param <K> the key type 071 * @param <V> the value type 072 * @param map the map to decorate, must not be null 073 * @param keyPredicate the predicate to validate the keys, null means no check 074 * @param valuePredicate the predicate to validate to values, null means no check 075 * @return a new predicated map 076 * @throws NullPointerException if the map is null 077 * @since 4.0 078 */ 079 public static <K, V> PredicatedMap<K, V> predicatedMap(final Map<K, V> map, 080 final Predicate<? super K> keyPredicate, 081 final Predicate<? super V> valuePredicate) { 082 return new PredicatedMap<>(map, keyPredicate, valuePredicate); 083 } 084 085 //----------------------------------------------------------------------- 086 /** 087 * Constructor that wraps (not copies). 088 * 089 * @param map the map to decorate, must not be null 090 * @param keyPredicate the predicate to validate the keys, null means no check 091 * @param valuePredicate the predicate to validate to values, null means no check 092 * @throws NullPointerException if the map is null 093 */ 094 protected PredicatedMap(final Map<K, V> map, final Predicate<? super K> keyPredicate, 095 final Predicate<? super V> valuePredicate) { 096 super(map); 097 this.keyPredicate = keyPredicate; 098 this.valuePredicate = valuePredicate; 099 100 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); 101 while (it.hasNext()) { 102 final Map.Entry<K, V> entry = it.next(); 103 validate(entry.getKey(), entry.getValue()); 104 } 105 } 106 107 //----------------------------------------------------------------------- 108 /** 109 * Write the map out using a custom routine. 110 * 111 * @param out the output stream 112 * @throws IOException if an error occurs while writing to the stream 113 * @since 3.1 114 */ 115 private void writeObject(final ObjectOutputStream out) throws IOException { 116 out.defaultWriteObject(); 117 out.writeObject(map); 118 } 119 120 /** 121 * Read the map in using a custom routine. 122 * 123 * @param in the input stream 124 * @throws IOException if an error occurs while reading from the stream 125 * @throws ClassNotFoundException if an object read from the stream can not be loaded 126 * @since 3.1 127 */ 128 @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 129 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 130 in.defaultReadObject(); 131 map = (Map<K, V>) in.readObject(); // (1) 132 } 133 134 //----------------------------------------------------------------------- 135 /** 136 * Validates a key value pair. 137 * 138 * @param key the key to validate 139 * @param value the value to validate 140 * @throws IllegalArgumentException if invalid 141 */ 142 protected void validate(final K key, final V value) { 143 if (keyPredicate != null && keyPredicate.evaluate(key) == false) { 144 throw new IllegalArgumentException("Cannot add key - Predicate rejected it"); 145 } 146 if (valuePredicate != null && valuePredicate.evaluate(value) == false) { 147 throw new IllegalArgumentException("Cannot add value - Predicate rejected it"); 148 } 149 } 150 151 /** 152 * Override to validate an object set into the map via <code>setValue</code>. 153 * 154 * @param value the value to validate 155 * @return the value itself 156 * @throws IllegalArgumentException if invalid 157 * @since 3.1 158 */ 159 @Override 160 protected V checkSetValue(final V value) { 161 if (valuePredicate.evaluate(value) == false) { 162 throw new IllegalArgumentException("Cannot set value - Predicate rejected it"); 163 } 164 return value; 165 } 166 167 /** 168 * Override to only return true when there is a value transformer. 169 * 170 * @return true if a value predicate is in use 171 * @since 3.1 172 */ 173 @Override 174 protected boolean isSetValueChecking() { 175 return valuePredicate != null; 176 } 177 178 //----------------------------------------------------------------------- 179 @Override 180 public V put(final K key, final V value) { 181 validate(key, value); 182 return map.put(key, value); 183 } 184 185 @Override 186 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 187 for (final Map.Entry<? extends K, ? extends V> entry : mapToCopy.entrySet()) { 188 validate(entry.getKey(), entry.getValue()); 189 } 190 super.putAll(mapToCopy); 191 } 192 193}