Example usage for com.google.common.collect ImmutableSortedMap comparator

List of usage examples for com.google.common.collect ImmutableSortedMap comparator

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedMap comparator.

Prototype

Comparator comparator

To view the source code for com.google.common.collect ImmutableSortedMap comparator.

Click Source Link

Usage

From source file:google.registry.model.common.TimedTransitionProperty.java

/**
 * Converts the provided value map into the equivalent transition map, using transition objects
 * of the given TimedTransition subclass.  The value map must be sorted according to the natural
 * ordering of its DateTime keys, and keys cannot be earlier than START_OF_TIME.
 */// w  ww.  java 2 s .c o m
// NB: The Class<T> parameter could be eliminated by getting the class via reflection, but then
// the callsite cannot infer T, so unless you explicitly call this as .<V, T>fromValueMap() it
// will default to using just TimedTransition<V>, which fails at runtime.
private static <V, T extends TimedTransition<V>> NavigableMap<DateTime, T> makeTransitionMap(
        ImmutableSortedMap<DateTime, V> valueMap, final Class<T> timedTransitionSubclass) {
    checkArgument(Ordering.natural().equals(valueMap.comparator()),
            "Timed transition value map must have transition time keys in chronological order");
    return Maps.transformEntries(valueMap, new Maps.EntryTransformer<DateTime, V, T>() {
        // For each entry in the input value map, make the output map have an entry at the
        // corresponding time that points to a transition containing that time and that value.
        @Override
        public T transformEntry(DateTime transitionTime, V value) {
            checkArgument(!transitionTime.isBefore(START_OF_TIME),
                    "Timed transition times cannot be earlier than START_OF_TIME / Unix Epoch");
            T subclass = TypeUtils.instantiate(timedTransitionSubclass);
            ((TimedTransition<V>) subclass).transitionTime = transitionTime;
            subclass.setValue(value);
            return subclass;
        }
    });
}