    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 *      https://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     * </p>
    067     *
    068     * @param <K>  the key type
    069     * @param <V>  the value type
    070     * @param map  The map to decorate, must not be null
    071     * @param keyPredicate  The predicate to validate the keys, null means no check
    072     * @param valuePredicate  The predicate to validate to values, null means no check
    073     * @return A new predicated sorted map
    074     * @throws NullPointerException if the map is null
    075     * @since 4.0
    076     */
    077    public static <K, V> PredicatedSortedMap<K, V> predicatedSortedMap(final SortedMap<K, V> map,
    078            final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate) {
    079        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
    080    }
    081
    082    /**
    083     * Constructor that wraps (not copies).
    084     *
    085     * @param map  The map to decorate, must not be null
    086     * @param keyPredicate  The predicate to validate the keys, null means no check
    087     * @param valuePredicate  The predicate to validate to values, null means no check
    088     * @throws NullPointerException if the map is null
    089     */
    090    protected PredicatedSortedMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
    091            final Predicate<? super V> valuePredicate) {
    092        super(map, keyPredicate, valuePredicate);
    093    }
    094
    095    @Override
    096    public Comparator<? super K> comparator() {
    097        return getSortedMap().comparator();
    098    }
    099
    100    @Override
    101    public K firstKey() {
    102        return getSortedMap().firstKey();
    103    }
    104
    105    /**
    106     * Gets the map being decorated.
    107     *
    108     * @return The decorated map
    109     */
    110    protected SortedMap<K, V> getSortedMap() {
    111        return (SortedMap<K, V>) map;
    112    }
    113
    114    @Override
    115    public SortedMap<K, V> headMap(final K toKey) {
    116        final SortedMap<K, V> map = getSortedMap().headMap(toKey);
    117        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
    118    }
    119
    120    @Override
    121    public K lastKey() {
    122        return getSortedMap().lastKey();
    123    }
    124
    125    @Override
    126    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
    127        final SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
    128        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
    129    }
    130
    131    @Override
    132    public SortedMap<K, V> tailMap(final K fromKey) {
    133        final SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
    134        return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
    135    }
    136
    137}