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 * @param <K> the type of the keys in this map
042 * @param <V> the type of the values in this map
043 * @since 3.0
044 */
045public class TransformedSortedMap<K, V>
046        extends TransformedMap<K, V>
047        implements SortedMap<K, V> {
048
049    /** Serialization version */
050    private static final long serialVersionUID = -8751771676410385778L;
051
052    /**
053     * Factory method to create a transforming sorted map.
054     * <p>
055     * If there are any elements already in the map being decorated, they are NOT transformed.
056     * Contrast this with {@link #transformedSortedMap(SortedMap, Transformer, Transformer)}.
057     *
058     * @param <K>  the key type
059     * @param <V>  the value type
060     * @param map  the map to decorate, must not be null
061     * @param keyTransformer  the predicate to validate the keys, null means no transformation
062     * @param valueTransformer  the predicate to validate to values, null means no transformation
063     * @return a new transformed sorted map
064     * @throws NullPointerException if the map is null
065     * @since 4.0
066     */
067    public static <K, V> TransformedSortedMap<K, V> transformingSortedMap(final SortedMap<K, V> map,
068            final Transformer<? super K, ? extends K> keyTransformer,
069            final Transformer<? super V, ? extends V> valueTransformer) {
070        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
071    }
072
073    /**
074     * Factory method to create a transforming sorted map that will transform
075     * existing contents of the specified map.
076     * <p>
077     * If there are any elements already in the map being decorated, they
078     * will be transformed by this method.
079     * Contrast this with {@link #transformingSortedMap(SortedMap, Transformer, Transformer)}.
080     *
081     * @param <K>  the key type
082     * @param <V>  the value type
083     * @param map  the map to decorate, must not be null
084     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
085     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
086     * @return a new transformed sorted map
087     * @throws NullPointerException if map is null
088     * @since 4.0
089     */
090    public static <K, V> TransformedSortedMap<K, V> transformedSortedMap(final SortedMap<K, V> map,
091            final Transformer<? super K, ? extends K> keyTransformer,
092            final Transformer<? super V, ? extends V> valueTransformer) {
093
094        final TransformedSortedMap<K, V> decorated =
095                new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
096        if (map.size() > 0) {
097            final Map<K, V> transformed = decorated.transformMap(map);
098            decorated.clear();
099            decorated.decorated().putAll(transformed);  // avoids double transformation
100        }
101        return decorated;
102    }
103
104    //-----------------------------------------------------------------------
105    /**
106     * Constructor that wraps (not copies).
107     * <p>
108     * If there are any elements already in the collection being decorated, they
109     * are NOT transformed.</p>
110     *
111     * @param map  the map to decorate, must not be null
112     * @param keyTransformer  the predicate to validate the keys, null means no transformation
113     * @param valueTransformer  the predicate to validate to values, null means no transformation
114     * @throws NullPointerException if the map is null
115     */
116    protected TransformedSortedMap(final SortedMap<K, V> map,
117            final Transformer<? super K, ? extends K> keyTransformer,
118            final Transformer<? super V, ? extends V> valueTransformer) {
119        super(map, keyTransformer, valueTransformer);
120    }
121
122    //-----------------------------------------------------------------------
123    /**
124     * Gets the map being decorated.
125     *
126     * @return the decorated map
127     */
128    protected SortedMap<K, V> getSortedMap() {
129        return (SortedMap<K, V>) map;
130    }
131
132    //-----------------------------------------------------------------------
133    @Override
134    public K firstKey() {
135        return getSortedMap().firstKey();
136    }
137
138    @Override
139    public K lastKey() {
140        return getSortedMap().lastKey();
141    }
142
143    @Override
144    public Comparator<? super K> comparator() {
145        return getSortedMap().comparator();
146    }
147
148    @Override
149    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
150        final SortedMap<K, V> map = getSortedMap().subMap(fromKey, toKey);
151        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
152    }
153
154    @Override
155    public SortedMap<K, V> headMap(final K toKey) {
156        final SortedMap<K, V> map = getSortedMap().headMap(toKey);
157        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
158    }
159
160    @Override
161    public SortedMap<K, V> tailMap(final K fromKey) {
162        final SortedMap<K, V> map = getSortedMap().tailMap(fromKey);
163        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
164    }
165
166}