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