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.Map;
021import java.util.SortedMap;
022
023import org.apache.commons.collections4.Transformer;
024
025/**
026 * Decorates another <code>SortedMap </code> to transform objects that are added.
027 * <p>
028 * The Map put methods and Map.Entry setValue method are affected by this class.
029 * Thus objects must be removed or searched for using their transformed form.
030 * For example, if the transformation converts Strings to Integers, you must
031 * use the Integer form to remove objects.
032 * <p>
033 * <strong>Note that TransformedSortedMap is not synchronized and is not thread-safe.</strong>
034 * If you wish to use this map from multiple threads concurrently, you must use
035 * appropriate synchronization. The simplest approach is to wrap this map
036 * using {@link java.util.Collections#synchronizedSortedMap}. This class may throw
037 * exceptions when accessed by concurrent threads without synchronization.
038 * <p>
039 * This class is Serializable from Commons Collections 3.1.
040 *
041 * @since 3.0
042 * @version $Id: TransformedSortedMap.html 972421 2015-11-14 20:00:04Z tn $
043 */
044public class TransformedSortedMap<K, V>
045        extends TransformedMap<K, V>
046        implements SortedMap<K, V> {
047
048    /** Serialization version */
049    private static final long serialVersionUID = -8751771676410385778L;
050
051    /**
052     * Factory method to create a transforming sorted map.
053     * <p>
054     * If there are any elements already in the map being decorated, they are NOT transformed.
055     * Contrast this with {@link #transformedSortedMap(SortedMap, Transformer, Transformer)}.
056     *
057     * @param <K>  the key type
058     * @param <V>  the value type
059     * @param map  the map to decorate, must not be null
060     * @param keyTransformer  the predicate to validate the keys, null means no transformation
061     * @param valueTransformer  the predicate to validate to values, null means no transformation
062     * @return a new transformed sorted map
063     * @throws IllegalArgumentException if the map is null
064     * @since 4.0
065     */
066    public static <K, V> TransformedSortedMap<K, V> transformingSortedMap(final SortedMap<K, V> map,
067            final Transformer<? super K, ? extends K> keyTransformer,
068            final Transformer<? super V, ? extends V> valueTransformer) {
069        return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer);
070    }
071
072    /**
073     * Factory method to create a transforming sorted map that will transform
074     * existing contents of the specified map.
075     * <p>
076     * If there are any elements already in the map being decorated, they
077     * will be transformed by this method.
078     * Contrast this with {@link #transformingSortedMap(SortedMap, Transformer, Transformer)}.
079     *
080     * @param <K>  the key type
081     * @param <V>  the value type
082     * @param map  the map to decorate, must not be null
083     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
084     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
085     * @return a new transformed sorted map
086     * @throws IllegalArgumentException if map is null
087     * @since 4.0
088     */
089    public static <K, V> TransformedSortedMap<K, V> transformedSortedMap(final SortedMap<K, V> map,
090            final Transformer<? super K, ? extends K> keyTransformer,
091            final Transformer<? super V, ? extends V> valueTransformer) {
092
093        final TransformedSortedMap<K, V> decorated =
094                new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer);
095        if (map.size() > 0) {
096            final Map<K, V> transformed = decorated.transformMap(map);
097            decorated.clear();
098            decorated.decorated().putAll(transformed);  // avoids double transformation
099        }
100        return decorated;
101    }
102
103    //-----------------------------------------------------------------------
104    /**
105     * Constructor that wraps (not copies).
106     * <p>
107     * If there are any elements already in the collection being decorated, they
108     * are NOT transformed.</p>
109     *
110     * @param map  the map to decorate, must not be null
111     * @param keyTransformer  the predicate to validate the keys, null means no transformation
112     * @param valueTransformer  the predicate to validate to values, null means no transformation
113     * @throws IllegalArgumentException if the map is null
114     */
115    protected TransformedSortedMap(final SortedMap<K, V> map,
116            final Transformer<? super K, ? extends K> keyTransformer,
117            final Transformer<? super V, ? extends V> valueTransformer) {
118        super(map, keyTransformer, valueTransformer);
119    }
120
121    //-----------------------------------------------------------------------
122    /**
123     * Gets the map being decorated.
124     *
125     * @return the decorated map
126     */
127    protected SortedMap<K, V> getSortedMap() {
128        return (SortedMap<K, V>) map;
129    }
130
131    //-----------------------------------------------------------------------
132    public K firstKey() {
133        return getSortedMap().firstKey();
134    }
135
136    public K lastKey() {
137        return getSortedMap().lastKey();
138    }
139
140    public Comparator<? super K> comparator() {
141        return getSortedMap().comparator();
142    }
143
144    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
145        final SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
146        return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer);
147    }
148
149    public SortedMap<K, V> headMap(final K toKey) {
150        final SortedMap<K, V> map = getSortedMap().headMap(toKey);
151        return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer);
152    }
153
154    public SortedMap<K, V> tailMap(final K fromKey) {
155        final SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
156        return new TransformedSortedMap<K, V>(map, keyTransformer, valueTransformer);
157    }
158
159}