Java Code Examples for java.util.Collections.unmodifiableNavigableMap()

The following are Jave code examples for showing how to use unmodifiableNavigableMap() of the java.util.Collections class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: diorite-configs-java8   File: YamlCollectionCreator.java   View Source Code Vote up 4 votes
@SuppressWarnings({"unchecked", "rawtypes", "SuspiciousSystemArraycopy"})
@Nullable
public static <T> T makeUnmodifiable(Object collection)
{
    if (collection.getClass().isArray())
    {
        int length = Array.getLength(collection);
        if (length == 0)
        {
            return (T) DioriteArrayUtils.getEmptyObjectArray(collection.getClass().getComponentType());
        }
        Object copy = DioriteArrayUtils.newArray(collection.getClass().getComponentType(), length);
        System.arraycopy(collection, 0, copy, 0, length);
        return (T) copy;
    }
    Function function = unmodifiableWrappers.get(collection.getClass());
    if (function == null)
    {
        for (Entry<Class<?>, Function<?, ?>> entry : unmodifiableWrappers.entrySet())
        {
            if (entry.getKey().isInstance(collection))
            {
                function = entry.getValue();
                break;
            }
        }
        if (function != null)
        {
            unmodifiableWrappers.put(collection.getClass(), function);
            return (T) function.apply(collection);
        }

        if (collection instanceof Collection)
        {
            if (collection instanceof Set)
            {
                if (collection instanceof NavigableSet)
                {
                    return (T) Collections.unmodifiableNavigableSet((NavigableSet<?>) collection);
                }
                if (collection instanceof SortedSet)
                {
                    return (T) Collections.unmodifiableSortedSet((SortedSet<?>) collection);
                }
                return (T) Collections.unmodifiableSet((Set<?>) collection);
            }
            if (collection instanceof List)
            {
                return (T) Collections.unmodifiableList((List<?>) collection);
            }
            return (T) Collections.unmodifiableCollection((Collection<?>) collection);
        }
        else if (collection instanceof Map)
        {
            if (collection instanceof NavigableMap)
            {
                return (T) Collections.unmodifiableNavigableMap((NavigableMap<?, ?>) collection);
            }
            if (collection instanceof SortedMap)
            {
                return (T) Collections.unmodifiableSortedMap((SortedMap<?, ?>) collection);
            }
            return (T) Collections.unmodifiableMap((Map<?, ?>) collection);
        }
        else
        {
            new RuntimeException("Can't make this collection unmodifiable: " + collection.getClass().getName()).printStackTrace();
            return (T) collection;
        }
    }
    return (T) function.apply(collection);
}
 
Example 2
Project: cryptotrader   File: BitbankDepth.java   View Source Code Vote up 3 votes
@VisibleForTesting
NavigableMap<BigDecimal, BigDecimal> convert(BigDecimal[][] quotes, Comparator<BigDecimal> c) {

    NavigableMap<BigDecimal, BigDecimal> map = new TreeMap<>(c);

    if (ArrayUtils.isNotEmpty(quotes)) {

        for (BigDecimal[] quote : quotes) {

            if (quote.length != 2) {
                continue;
            }

            if (quote[0] == null || quote[0].signum() == 0) {
                continue;
            }

            if (quote[1] == null || quote[1].signum() == 0) {
                continue;
            }

            map.put(quote[0], quote[1]);

        }

    }

    return Collections.unmodifiableNavigableMap(map);

}