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 */ 017 package org.apache.commons.collections.map; 018 019 import java.util.Comparator; 020 import java.util.SortedMap; 021 022 import org.apache.commons.collections.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>SortedMap map = PredicatedSortedSet.decorate(new TreeMap(), NotNullPredicate.INSTANCE, null);</pre> 034 * <p> 035 * <strong>Note that PredicatedSortedMap is not synchronized and is not thread-safe.</strong> 036 * If you wish to use this map from multiple threads concurrently, you must use 037 * appropriate synchronization. The simplest approach is to wrap this map 038 * using {@link java.util.Collections#synchronizedSortedMap}. This class may throw 039 * exceptions when accessed by concurrent threads without synchronization. 040 * <p> 041 * This class is Serializable from Commons Collections 3.1. 042 * 043 * @since Commons Collections 3.0 044 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 045 * 046 * @author Stephen Colebourne 047 * @author Paul Jack 048 */ 049 public class PredicatedSortedMap 050 extends PredicatedMap 051 implements SortedMap { 052 053 /** Serialization version */ 054 private static final long serialVersionUID = 3359846175935304332L; 055 056 /** 057 * Factory method to create a predicated (validating) sorted map. 058 * <p> 059 * If there are any elements already in the list being decorated, they 060 * are validated. 061 * 062 * @param map the map to decorate, must not be null 063 * @param keyPredicate the predicate to validate the keys, null means no check 064 * @param valuePredicate the predicate to validate to values, null means no check 065 * @throws IllegalArgumentException if the map is null 066 */ 067 public static SortedMap decorate(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) { 068 return new PredicatedSortedMap(map, keyPredicate, valuePredicate); 069 } 070 071 //----------------------------------------------------------------------- 072 /** 073 * Constructor that wraps (not copies). 074 * 075 * @param map the map to decorate, must not be null 076 * @param keyPredicate the predicate to validate the keys, null means no check 077 * @param valuePredicate the predicate to validate to values, null means no check 078 * @throws IllegalArgumentException if the map is null 079 */ 080 protected PredicatedSortedMap(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) { 081 super(map, keyPredicate, valuePredicate); 082 } 083 084 //----------------------------------------------------------------------- 085 /** 086 * Gets the map being decorated. 087 * 088 * @return the decorated map 089 */ 090 protected SortedMap getSortedMap() { 091 return (SortedMap) map; 092 } 093 094 //----------------------------------------------------------------------- 095 public Object firstKey() { 096 return getSortedMap().firstKey(); 097 } 098 099 public Object lastKey() { 100 return getSortedMap().lastKey(); 101 } 102 103 public Comparator comparator() { 104 return getSortedMap().comparator(); 105 } 106 107 public SortedMap subMap(Object fromKey, Object toKey) { 108 SortedMap map = getSortedMap().subMap(fromKey, toKey); 109 return new PredicatedSortedMap(map, keyPredicate, valuePredicate); 110 } 111 112 public SortedMap headMap(Object toKey) { 113 SortedMap map = getSortedMap().headMap(toKey); 114 return new PredicatedSortedMap(map, keyPredicate, valuePredicate); 115 } 116 117 public SortedMap tailMap(Object fromKey) { 118 SortedMap map = getSortedMap().tailMap(fromKey); 119 return new PredicatedSortedMap(map, keyPredicate, valuePredicate); 120 } 121 122 }