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