All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.ImmutableSortedSet Maven / Gradle / Ivy

There is a newer version: 1.10.1
Show newest version
/*
 * Copyright (C) 2017 HaiYang Li
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.landawn.abacus.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * 
 * @since 1.1.4
 * 
 * @author Haiyang Li
 */
public class ImmutableSortedSet extends ImmutableSet implements SortedSet {

    @SuppressWarnings("rawtypes")
    private static final ImmutableSortedSet EMPTY = new ImmutableSortedSet(N.emptySortedSet());

    private final SortedSet sortedSet;

    ImmutableSortedSet(SortedSet sortedSet) {
        super(sortedSet);
        this.sortedSet = (SortedSet) sortedSet;
    }

    public static  ImmutableSortedSet empty() {
        return EMPTY;
    }

    @SafeVarargs
    public static > ImmutableSortedSet of(final E... a) {
        if (N.isNullOrEmpty(a)) {
            return empty();
        }

        return new ImmutableSortedSet<>(new TreeSet<>(Arrays.asList(a)));
    }

    /**
     * 
     * @param sortedSet the elements in this Set are shared by the returned ImmutableSortedSet.
     * @return
     */
    public static  ImmutableSortedSet of(final SortedSet sortedSet) {
        if (sortedSet == null) {
            return empty();
        } else if (sortedSet instanceof ImmutableSortedSet) {
            return (ImmutableSortedSet) sortedSet;
        }

        return new ImmutableSortedSet<>(sortedSet);
    }

    /**
     * 
     * @param sortedSet
     * @return
     */
    public static  ImmutableSortedSet copyOf(final SortedSet sortedSet) {
        if (N.isNullOrEmpty(sortedSet)) {
            return empty();
        }

        return new ImmutableSortedSet<>(new TreeSet<>(sortedSet));
    }

    @Deprecated
    public static  ImmutableSet of(final Set set) {
        throw new UnsupportedOperationException();
    }

    @Deprecated
    public static  ImmutableSet copyOf(final Collection set) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Comparator comparator() {
        return sortedSet.comparator();
    }

    @Override
    public SortedSet subSet(E fromElement, E toElement) {
        return of(sortedSet.subSet(fromElement, toElement));
    }

    @Override
    public SortedSet headSet(E toElement) {
        return of(sortedSet.headSet(toElement));
    }

    @Override
    public SortedSet tailSet(E fromElement) {
        return of(sortedSet.tailSet(fromElement));
    }

    @Override
    public E first() {
        return sortedSet.first();
    }

    @Override
    public E last() {
        return sortedSet.last();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy