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 * @since 3.0 048 * @version $Id: PredicatedMap.html 972421 2015-11-14 20:00:04Z tn $ 049 */ 050public class PredicatedMap<K, V> 051 extends AbstractInputCheckedMapDecorator<K, V> 052 implements Serializable { 053 054 /** Serialization version */ 055 private static final long serialVersionUID = 7412622456128415156L; 056 057 /** The key predicate to use */ 058 protected final Predicate<? super K> keyPredicate; 059 060 /** The value predicate to use */ 061 protected final Predicate<? super V> valuePredicate; 062 063 /** 064 * Factory method to create a predicated (validating) map. 065 * <p> 066 * If there are any elements already in the list being decorated, they 067 * are validated. 068 * 069 * @param <K> the key type 070 * @param <V> the value type 071 * @param map the map to decorate, must not be null 072 * @param keyPredicate the predicate to validate the keys, null means no check 073 * @param valuePredicate the predicate to validate to values, null means no check 074 * @return a new predicated map 075 * @throws IllegalArgumentException if the map is null 076 * @since 4.0 077 */ 078 public static <K, V> PredicatedMap<K, V> predicatedMap(final Map<K, V> map, 079 final Predicate<? super K> keyPredicate, 080 final Predicate<? super V> valuePredicate) { 081 return new PredicatedMap<K, V>(map, keyPredicate, valuePredicate); 082 } 083 084 //----------------------------------------------------------------------- 085 /** 086 * Constructor that wraps (not copies). 087 * 088 * @param map the map to decorate, must not be null 089 * @param keyPredicate the predicate to validate the keys, null means no check 090 * @param valuePredicate the predicate to validate to values, null means no check 091 * @throws IllegalArgumentException if the map is null 092 */ 093 protected PredicatedMap(final Map<K, V> map, final Predicate<? super K> keyPredicate, 094 final Predicate<? super V> valuePredicate) { 095 super(map); 096 this.keyPredicate = keyPredicate; 097 this.valuePredicate = valuePredicate; 098 099 final Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); 100 while (it.hasNext()) { 101 final Map.Entry<K, V> entry = it.next(); 102 validate(entry.getKey(), entry.getValue()); 103 } 104 } 105 106 //----------------------------------------------------------------------- 107 /** 108 * Write the map out using a custom routine. 109 * 110 * @param out the output stream 111 * @throws IOException 112 * @since 3.1 113 */ 114 private void writeObject(final ObjectOutputStream out) throws IOException { 115 out.defaultWriteObject(); 116 out.writeObject(map); 117 } 118 119 /** 120 * Read the map in using a custom routine. 121 * 122 * @param in the input stream 123 * @throws IOException 124 * @throws ClassNotFoundException 125 * @since 3.1 126 */ 127 @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect 128 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 129 in.defaultReadObject(); 130 map = (Map<K, V>) in.readObject(); // (1) 131 } 132 133 //----------------------------------------------------------------------- 134 /** 135 * Validates a key value pair. 136 * 137 * @param key the key to validate 138 * @param value the value to validate 139 * @throws IllegalArgumentException if invalid 140 */ 141 protected void validate(final K key, final V value) { 142 if (keyPredicate != null && keyPredicate.evaluate(key) == false) { 143 throw new IllegalArgumentException("Cannot add key - Predicate rejected it"); 144 } 145 if (valuePredicate != null && valuePredicate.evaluate(value) == false) { 146 throw new IllegalArgumentException("Cannot add value - Predicate rejected it"); 147 } 148 } 149 150 /** 151 * Override to validate an object set into the map via <code>setValue</code>. 152 * 153 * @param value the value to validate 154 * @return the value itself 155 * @throws IllegalArgumentException if invalid 156 * @since 3.1 157 */ 158 @Override 159 protected V checkSetValue(final V value) { 160 if (valuePredicate.evaluate(value) == false) { 161 throw new IllegalArgumentException("Cannot set value - Predicate rejected it"); 162 } 163 return value; 164 } 165 166 /** 167 * Override to only return true when there is a value transformer. 168 * 169 * @return true if a value predicate is in use 170 * @since 3.1 171 */ 172 @Override 173 protected boolean isSetValueChecking() { 174 return valuePredicate != null; 175 } 176 177 //----------------------------------------------------------------------- 178 @Override 179 public V put(final K key, final V value) { 180 validate(key, value); 181 return map.put(key, value); 182 } 183 184 @Override 185 public void putAll(final Map<? extends K, ? extends V> mapToCopy) { 186 for (final Map.Entry<? extends K, ? extends V> entry : mapToCopy.entrySet()) { 187 validate(entry.getKey(), entry.getValue()); 188 } 189 super.putAll(mapToCopy); 190 } 191 192}