com.landawn.abacus.util.ImmutableSortedSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* 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 extends E> 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 extends E> 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 extends E> sortedSet) {
if (N.isNullOrEmpty(sortedSet)) {
return empty();
}
return new ImmutableSortedSet<>(new TreeSet<>(sortedSet));
}
@Deprecated
public static ImmutableSet of(final Set extends E> set) {
throw new UnsupportedOperationException();
}
@Deprecated
public static ImmutableSet copyOf(final Collection extends E> set) {
throw new UnsupportedOperationException();
}
@Override
public Comparator super E> 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();
}
}