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

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

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.3.16
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;

/**
 *
 * @author Haiyang Li
 * @param 
 * @since 1.1.4
 */
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;
    }

    /**
     *
     * @param 
     * @return
     */
    public static  ImmutableSortedSet empty() {
        return EMPTY;
    }

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

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

    /**
     *
     * @param 
     * @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 
     * @param sortedSet
     * @return
     */
    public static  ImmutableSortedSet copyOf(final SortedSet sortedSet) {
        if (N.isNullOrEmpty(sortedSet)) {
            return empty();
        }

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

    /**
     *
     * @param 
     * @param set
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    public static  ImmutableSet of(final Set set) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param 
     * @param set
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    public static  ImmutableSet copyOf(final Collection set) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

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

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

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

    /**
     *
     * @param fromElement
     * @return
     */
    @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