View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.map;
18  
19  import java.util.Comparator;
20  import java.util.SortedMap;
21  
22  import org.apache.commons.collections4.Predicate;
23  
24  /**
25   * Decorates another {@code SortedMap} to validate that additions
26   * match a specified predicate.
27   * <p>
28   * This map exists to provide validation for the decorated map.
29   * It is normally created to decorate an empty map.
30   * If an object cannot be added to the map, an IllegalArgumentException is thrown.
31   * </p>
32   * <p>
33   * One usage would be to ensure that no null keys are added to the map.
34   * </p>
35   * <pre>
36   *   SortedMap map =
37   *     PredicatedSortedMap.predicatedSortedMap(new TreeMap(),
38   *                                             NotNullPredicate.notNullPredicate(),
39   *                                             null);
40   * </pre>
41   * <p>
42   * <strong>Note that PredicatedSortedMap is not synchronized and is not thread-safe.</strong>
43   * If you wish to use this map from multiple threads concurrently, you must use
44   * appropriate synchronization. The simplest approach is to wrap this map
45   * using {@link java.util.Collections#synchronizedSortedMap}. This class may throw
46   * exceptions when accessed by concurrent threads without synchronization.
47   * </p>
48   * <p>
49   * This class is Serializable from Commons Collections 3.1.
50   * </p>
51   *
52   * @param <K> the type of the keys in this map
53   * @param <V> the type of the values in this map
54   * @since 3.0
55   */
56  public class PredicatedSortedMap<K, V> extends PredicatedMap<K, V> implements SortedMap<K, V> {
57  
58      /** Serialization version */
59      private static final long serialVersionUID = 3359846175935304332L;
60  
61      /**
62       * Factory method to create a predicated (validating) sorted map.
63       * <p>
64       * If there are any elements already in the list being decorated, they
65       * are validated.
66       * </p>
67       *
68       * @param <K>  the key type
69       * @param <V>  the value type
70       * @param map  the map to decorate, must not be null
71       * @param keyPredicate  the predicate to validate the keys, null means no check
72       * @param valuePredicate  the predicate to validate to values, null means no check
73       * @return a new predicated sorted map
74       * @throws NullPointerException if the map is null
75       * @since 4.0
76       */
77      public static <K, V> PredicatedSortedMap<K, V> predicatedSortedMap(final SortedMap<K, V> map,
78              final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate) {
79          return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
80      }
81  
82      /**
83       * Constructor that wraps (not copies).
84       *
85       * @param map  the map to decorate, must not be null
86       * @param keyPredicate  the predicate to validate the keys, null means no check
87       * @param valuePredicate  the predicate to validate to values, null means no check
88       * @throws NullPointerException if the map is null
89       */
90      protected PredicatedSortedMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
91              final Predicate<? super V> valuePredicate) {
92          super(map, keyPredicate, valuePredicate);
93      }
94  
95      @Override
96      public Comparator<? super K> comparator() {
97          return getSortedMap().comparator();
98      }
99  
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 }