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.collections.map;
18  
19  import java.util.Comparator;
20  import java.util.SortedMap;
21  
22  import org.apache.commons.collections.Predicate;
23  
24  /**
25   * Decorates another <code>SortedMap </code> 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   * One usage would be to ensure that no null keys are added to the map.
33   * <pre>SortedMap map = PredicatedSortedSet.decorate(new TreeMap(), NotNullPredicate.INSTANCE, null);</pre>
34   * <p>
35   * <strong>Note that PredicatedSortedMap is not synchronized and is not thread-safe.</strong>
36   * If you wish to use this map from multiple threads concurrently, you must use
37   * appropriate synchronization. The simplest approach is to wrap this map
38   * using {@link java.util.Collections#synchronizedSortedMap}. This class may throw 
39   * exceptions when accessed by concurrent threads without synchronization.
40   * <p>
41   * This class is Serializable from Commons Collections 3.1.
42   *
43   * @since 3.0
44   * @version $Id: PredicatedSortedMap.java 1429905 2013-01-07 17:15:14Z ggregory $
45   */
46  public class PredicatedSortedMap<K, V> extends PredicatedMap<K, V> implements SortedMap<K, V> {
47  
48      /** Serialization version */
49      private static final long serialVersionUID = 3359846175935304332L;
50  
51      /**
52       * Factory method to create a predicated (validating) sorted map.
53       * <p>
54       * If there are any elements already in the list being decorated, they
55       * are validated.
56       * 
57       * @param <K>  the key type
58       * @param <V>  the value type
59       * @param map  the map to decorate, must not be null
60       * @param keyPredicate  the predicate to validate the keys, null means no check
61       * @param valuePredicate  the predicate to validate to values, null means no check
62       * @return a new predicated sorted map
63       * @throws IllegalArgumentException if the map is null
64       */
65      public static <K, V> PredicatedSortedMap<K, V> predicatedSortedMap(final SortedMap<K, V> map,
66              final Predicate<? super K> keyPredicate, final Predicate<? super V> valuePredicate) {
67          return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
68      }
69  
70      //-----------------------------------------------------------------------
71      /**
72       * Constructor that wraps (not copies).
73       * 
74       * @param map  the map to decorate, must not be null
75       * @param keyPredicate  the predicate to validate the keys, null means no check
76       * @param valuePredicate  the predicate to validate to values, null means no check
77       * @throws IllegalArgumentException if the map is null
78       */
79      protected PredicatedSortedMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
80              final Predicate<? super V> valuePredicate) {
81          super(map, keyPredicate, valuePredicate);
82      }
83  
84      //-----------------------------------------------------------------------
85      /**
86       * Gets the map being decorated.
87       * 
88       * @return the decorated map
89       */
90      protected SortedMap<K, V> getSortedMap() {
91          return (SortedMap<K, V>) map;
92      }
93  
94      //-----------------------------------------------------------------------
95      public K firstKey() {
96          return getSortedMap().firstKey();
97      }
98  
99      public K lastKey() {
100         return getSortedMap().lastKey();
101     }
102 
103     public Comparator<? super K> comparator() {
104         return getSortedMap().comparator();
105     }
106 
107     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
108         final SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
109         return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
110     }
111 
112     public SortedMap<K, V> headMap(final K toKey) {
113         final SortedMap<K, V> map = getSortedMap().headMap(toKey);
114         return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
115     }
116 
117     public SortedMap<K, V> tailMap(final K fromKey) {
118         final SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
119         return new PredicatedSortedMap<K, V>(map, keyPredicate, valuePredicate);
120     }
121 
122 }