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       *
67       * @param <K>  the key type
68       * @param <V>  the value type
69       * @param map  the map to decorate, must not be null
70       * @param keyPredicate  the predicate to validate the keys, null means no check
71       * @param valuePredicate  the predicate to validate to values, null means no check
72       * @return a new predicated sorted map
73       * @throws NullPointerException if the map is null
74       * @since 4.0
75       */
76      public static <K, V> PredicatedSortedMap<K, V> predicatedSortedMap(final SortedMap<K, V> map,
77              final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate) {
78          return new PredicatedSortedMap<>(map, keyPredicate, valuePredicate);
79      }
80  
81      /**
82       * Constructor that wraps (not copies).
83       *
84       * @param map  the map to decorate, must not be null
85       * @param keyPredicate  the predicate to validate the keys, null means no check
86       * @param valuePredicate  the predicate to validate to values, null means no check
87       * @throws NullPointerException if the map is null
88       */
89      protected PredicatedSortedMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
90              final Predicate<? super V> valuePredicate) {
91          super(map, keyPredicate, valuePredicate);
92      }
93  
94      @Override
95      public Comparator<? super K> comparator() {
96          return getSortedMap().comparator();
97      }
98  
99      @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 }