Skip to content

Commit

Permalink
fix Set
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 16, 2024
1 parent 82da251 commit 769185b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 57 deletions.
18 changes: 16 additions & 2 deletions kala-collection/src/main/java/kala/collection/Set.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
/*
* Copyright 2024 Glavo
*
* 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 kala.collection;

import kala.collection.base.Iterators;
import kala.collection.factory.CollectionFactory;
import kala.collection.immutable.ImmutableHashSet;
import kala.collection.immutable.ImmutableSet;
import kala.collection.internal.convert.AsJavaConvert;
import kala.collection.internal.convert.FromJavaConvert;
import kala.collection.internal.view.SetViews;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.Iterator;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand Down Expand Up @@ -54,6 +67,7 @@ public interface Set<E> extends Collection<E>, SetLike<E>, AnySet<E> {
return ImmutableSet.of(value1, value2, value3, value4, value5);
}

@SafeVarargs
static <E> @NotNull Set<E> of(E... values) {
return ImmutableSet.of(values);
}
Expand Down
123 changes: 69 additions & 54 deletions kala-collection/src/main/java/kala/collection/SortedSet.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,69 @@
package kala.collection;

import kala.annotations.Covariant;
import kala.annotations.DelegateBy;
import kala.collection.base.OrderedTraversable;
import kala.collection.internal.convert.AsJavaConvert;
import kala.comparator.Comparators;
import kala.collection.factory.CollectionFactory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.Iterator;

public interface SortedSet<@Covariant E> extends Set<E>, OrderedTraversable<E> {

<U> @NotNull CollectionFactory<U, ?, ? extends SortedSet<U>> iterableFactory(Comparator<? super U> comparator);

@Override
default @NotNull java.util.SortedSet<E> asJava() {
return new AsJavaConvert.SortedSetAsJava<>(this);
}

default Comparator<? super E> comparator() {
return Comparators.naturalOrder();
}

@Contract(pure = true)
default E getFirst() {
return iterator().next();
}

@Contract(pure = true)
default E getLast() {
Iterator<E> iterator = iterator();
E res = iterator.next();
while (iterator.hasNext()) {
res = iterator.next();
}
return res;
}

@Override
@DelegateBy("getLast()")
default E max() {
return getLast();
}

@Override
@DelegateBy("getFirst()")
default E min() {
return getFirst();
}
}
/*
* Copyright 2024 Glavo
*
* 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 kala.collection;

import kala.annotations.Covariant;
import kala.annotations.DelegateBy;
import kala.collection.base.OrderedTraversable;
import kala.collection.internal.convert.AsJavaConvert;
import kala.comparator.Comparators;
import kala.collection.factory.CollectionFactory;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.Iterator;

public interface SortedSet<@Covariant E> extends Set<E>, OrderedTraversable<E> {

<U> @NotNull CollectionFactory<U, ?, ? extends SortedSet<U>> iterableFactory(Comparator<? super U> comparator);

@Override
default java.util.@NotNull SortedSet<E> asJava() {
return new AsJavaConvert.SortedSetAsJava<>(this);
}

default Comparator<? super E> comparator() {
return Comparators.naturalOrder();
}

@Contract(pure = true)
default E getFirst() {
return iterator().next();
}

@Contract(pure = true)
default E getLast() {
Iterator<E> iterator = iterator();
E res = iterator.next();
while (iterator.hasNext()) {
res = iterator.next();
}
return res;
}

@Override
@DelegateBy("getLast()")
default E max() {
return getLast();
}

@Override
@DelegateBy("getFirst()")
default E min() {
return getFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Glavo
*
* 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 kala.collection.mutable;

import kala.collection.SortedSet;
Expand All @@ -6,7 +21,7 @@

public interface MutableSortedSet<E> extends MutableSet<E>, SortedSet<E> {
@Override
default @NotNull java.util.SortedSet<E> asJava() {
default java.util.@NotNull SortedSet<E> asJava() {
return new AsJavaConvert.MutableSortedSetAsJava<>(this);
}
}

0 comments on commit 769185b

Please sign in to comment.