Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Jun 10, 2024
2 parents e4a628e + ea0f7b0 commit c446b64
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
34 changes: 34 additions & 0 deletions mihxil-math/src/main/java/org/meeuw/math/CollectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.*;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.meeuw.math.operators.OperatorInterface;

import static java.util.Collections.unmodifiableNavigableSet;
Expand Down Expand Up @@ -82,4 +84,36 @@ public V get() {
}
};
}


/**
* Wraps the given set in a new set, with the same elements.
* <p>
* The only difference will be that its {@link Set#contains(Object)} will simply return {@code false} if the argument is {@code null}.
* @since 0.12
*/
public static <P> Set<@NonNull P> nullSafeSet(@NonNull final Set<@NonNull P> set) {
return new AbstractSet<P>() {
@Override
public @NonNull Iterator<P> iterator() {
return set.iterator();
}

@Override
public int size() {
return set.size();
}

@Override
public boolean add(@NonNull P o) {
return set.add(o);
}

@Override
public boolean contains(@Nullable Object o) {
return o != null && set.contains(o);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
*/
package org.meeuw.test.math;

import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import lombok.SneakyThrows;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import org.meeuw.math.CollectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class CollectionUtilsTest {

@Test
Expand All @@ -42,6 +46,7 @@ public Integer get() {
Supplier<Integer> memoize = CollectionUtils.memoize(sup);
ExecutorService executor = Executors.newFixedThreadPool(10);


List<Future<Integer>> f = new ArrayList<>();
for (int i = 0; i < 5; i++) {
f.add(executor.submit(new Callable<Integer>() {
Expand All @@ -56,4 +61,24 @@ public Integer call() {
}
executor.shutdown();
}


@Test
void nullSafeSet() {
Set<String> set = Set.of("a", "b");

assertThatThrownBy(() -> set.contains(null)).isInstanceOf(NullPointerException.class);

Set<String> nullSafeSet = CollectionUtils.nullSafeSet(set);

assertThat(nullSafeSet.contains(null)).isFalse();
assertThat(nullSafeSet.contains("a")).isTrue();

assertThat(nullSafeSet.size()).isEqualTo(2);

assertThatThrownBy(() -> nullSafeSet.add(null)).isInstanceOf(UnsupportedOperationException.class);

assertThat(nullSafeSet.iterator()).asList().containsExactly("a", "b");
}

}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
<!-- test dependencies -->
<junit.version>5.10.1</junit.version>
<jacoco.version>0.8.11</jacoco.version>
<assertj.version>3.24.2</assertj.version>
<assertj.version>3.25.2</assertj.version>
<jqwik.version>1.7.4</jqwik.version> <!-- 1.8 is out, but fails a lot of things related to generics? -->
<reflections.version>0.10.2</reflections.version>
<hibernate-validator.version>7.0.5.Final</hibernate-validator.version>
<log4j.version>2.21.1</log4j.version>
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
<log4j.version>2.22.1</log4j.version>

</properties>

Expand Down

0 comments on commit c446b64

Please sign in to comment.