    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 } 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     *
    063     * @param <K>  the key type
    064     * @param <V>  the value type
    065     * @param map  the map to decorate, must not be null
    066     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
    067     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
    068     * @return a new transformed sorted map
    069     * @throws NullPointerException if map is null
    070     * @since 4.0
    071     */
    072    public static <K, V> TransformedSortedMap<K, V> transformedSortedMap(final SortedMap<K, V> map,
    073            final Transformer<? super K, ? extends K> keyTransformer,
    074            final Transformer<? super V, ? extends V> valueTransformer) {
    075
    076        final TransformedSortedMap<K, V> decorated =
    077                new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
    078        if (!map.isEmpty()) {
    079            final Map<K, V> transformed = decorated.transformMap(map);
    080            decorated.clear();
    081            decorated.decorated().putAll(transformed);  // avoids double transformation
    082        }
    083        return decorated;
    084    }
    085
    086    /**
    087     * Factory method to create a transforming sorted map.
    088     * <p>
    089     * If there are any elements already in the map being decorated, they are NOT transformed.
    090     * Contrast this with {@link #transformedSortedMap(SortedMap, Transformer, Transformer)}.
    091     *
    092     * @param <K>  the key type
    093     * @param <V>  the value type
    094     * @param map  the map to decorate, must not be null
    095     * @param keyTransformer  the predicate to validate the keys, null means no transformation
    096     * @param valueTransformer  the predicate to validate to values, null means no transformation
    097     * @return a new transformed sorted map
    098     * @throws NullPointerException if the map is null
    099     * @since 4.0
    100     */
    101    public static <K, V> TransformedSortedMap<K, V> transformingSortedMap(final SortedMap<K, V> map,
    102            final Transformer<? super K, ? extends K> keyTransformer,
    103            final Transformer<? super V, ? extends V> valueTransformer) {
    104        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
    105    }
    106
    107    /**
    108     * Constructor that wraps (not copies).
    109     * <p>
    110     * If there are any elements already in the collection being decorated, they
    111     * are NOT transformed.</p>
    112     *
    113     * @param map  the map to decorate, must not be null
    114     * @param keyTransformer  the predicate to validate the keys, null means no transformation
    115     * @param valueTransformer  the predicate to validate to values, null means no transformation
    116     * @throws NullPointerException if the map is null
    117     */
    118    protected TransformedSortedMap(final SortedMap<K, V> map,
    119            final Transformer<? super K, ? extends K> keyTransformer,
    120            final Transformer<? super V, ? extends V> valueTransformer) {
    121        super(map, keyTransformer, valueTransformer);
    122    }
    123
    124    @Override
    125    public Comparator<? super K> comparator() {
    126        return getSortedMap().comparator();
    127    }
    128
    129    @Override
    130    public K firstKey() {
    131        return getSortedMap().firstKey();
    132    }
    133
    134    /**
    135     * Gets the map being decorated.
    136     *
    137     * @return the decorated map
    138     */
    139    protected SortedMap<K, V> getSortedMap() {
    140        return (SortedMap<K, V>) map;
    141    }
    142
    143    @Override
    144    public SortedMap<K, V> headMap(final K toKey) {
    145        final SortedMap<K, V> map = getSortedMap().headMap(toKey);
    146        return new TransformedSortedMap<>(map, keyTransformer, valueTransformer);
    147    }
    148
    149    @Override
    150    public K lastKey() {
    151        return getSortedMap().lastKey();
    152    }
    153
    154    @Override
    155    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
    156        final SortedMap<K, V> map = getSortedMap().subMap(fromKey, 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}