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.util.Comparator;
020import java.util.SortedMap;
021
022import org.apache.commons.collections4.Predicate;
023
024/**
025 * Decorates another {@code SortedMap} to validate that additions
026 * match a specified predicate.
027 * <p>
028 * This map exists to provide validation for the decorated map.
029 * It is normally created to decorate an empty map.
030 * If an object cannot be added to the map, an IllegalArgumentException is thrown.
031 * </p>
032 * <p>
033 * One usage would be to ensure that no null keys are added to the map.
034 * </p>
035 * <pre>
036 *   SortedMap map =
037 *     PredicatedSortedMap.predicatedSortedMap(new TreeMap(),
038 *                                             NotNullPredicate.notNullPredicate(),
039 *                                             null);
040 * </pre>
041 * <p>
042 * <strong>Note that PredicatedSortedMap is not synchronized and is not thread-safe.</strong>
043 * If you wish to use this map from multiple threads concurrently, you must use
044 * appropriate synchronization. The simplest approach is to wrap this map
045 * using {@link java.util.Collections#synchronizedSortedMap}. This class may throw
046 * exceptions when accessed by concurrent threads without synchronization.
047 * </p>
048 * <p>
049 * This class is Serializable from Commons Collections 3.1.
050 * </p>
051 *
052 * @param <K> the type of the keys in this map
053 * @param <V> the type of the values in this map
054 * @since 3.0
055 */
056public class PredicatedSortedMap<K, V> extends PredicatedMap<K, V> implements SortedMap<K, V> {
057
058    /** Serialization version */
059    private static final long serialVersionUID = 3359846175935304332L;
060
061    /**
062     * Factory method to create a predicated (validating) sorted map.
063     * <p>
064     * If there are any elements already in the list being decorated, they
065     * are validated.
066     *
067     * @param <K>  the key type
068     * @param <V>  the value type
069     * @param map  the map to decorate, must not be null
070     * @param keyPredicate  the predicate to validate the keys, null means no check
071     * @param valuePredicate  the predicate to validate to values, null means no check
072     * @return a new predicated sorted map
073     * @throws NullPointerException if the map is null
074     * @since 4.0
075     */
076    public static <K, V> PredicatedSortedMap<K, V> predicatedSortedMap(final SortedMap<K, V> map,
077            final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate) {
078        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
079    }
080
081    /**
082     * Constructor that wraps (not copies).
083     *
084     * @param map  the map to decorate, must not be null
085     * @param keyPredicate  the predicate to validate the keys, null means no check
086     * @param valuePredicate  the predicate to validate to values, null means no check
087     * @throws NullPointerException if the map is null
088     */
089    protected PredicatedSortedMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
090            final Predicate<? super V> valuePredicate) {
091        super(map, keyPredicate, valuePredicate);
092    }
093
094    @Override
095    public Comparator<? super K> comparator() {
096        return getSortedMap().comparator();
097    }
098
099    @Override
100    public K firstKey() {
101        return getSortedMap().firstKey();
102    }
103
104    /**
105     * Gets the map being decorated.
106     *
107     * @return the decorated map
108     */
109    protected SortedMap<K, V> getSortedMap() {
110        return (SortedMap<K, V>) map;
111    }
112
113    @Override
114    public SortedMap<K, V> headMap(final K toKey) {
115        final SortedMap<K, V> map = getSortedMap().headMap(toKey);
116        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
117    }
118
119    @Override
120    public K lastKey() {
121        return getSortedMap().lastKey();
122    }
123
124    @Override
125    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
126        final SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
127        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
128    }
129
130    @Override
131    public SortedMap<K, V> tailMap(final K fromKey) {
132        final SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
133        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
134    }
135
136}