Skip to content

Commit

Permalink
Improve collection utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed May 4, 2021
1 parent cb78040 commit cdaa1a4
Show file tree
Hide file tree
Showing 28 changed files with 560 additions and 240 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Unreleased

## [0.1.3] - 2021-05-02
## [0.1.5] - 2021-05-04
### Added
- Initialize quark-common

[Unreleased]: https://github.com/coditory/quark-common/compare/v0.1.3...HEAD
[0.1.3]: https://github.com/coditory/quark-common/releases/tag/v0.1.3
[Unreleased]: https://github.com/coditory/quark-common/compare/v0.1.5...HEAD
[0.1.5]: https://github.com/coditory/quark-common/releases/tag/v0.1.5
85 changes: 0 additions & 85 deletions src/main/java/com/coditory/quark/common/bit/BitSets.java

This file was deleted.

79 changes: 0 additions & 79 deletions src/main/java/com/coditory/quark/common/bit/ImmutableBitSet.java

This file was deleted.

39 changes: 39 additions & 0 deletions src/main/java/com/coditory/quark/common/check/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

import static com.coditory.quark.common.util.Strings.quote;
import static java.lang.String.format;

public final class Args {
Expand Down Expand Up @@ -113,6 +114,44 @@ public static String checkNoBlanks(@Nullable String value, String name) {
return value;
}

public static String checkNotContains(@Nullable String value, char c) {
return checkNotContains(value, c, null);
}

public static String checkNotContains(@Nullable String value, char c, String name) {
return checkNotContains(value, "" + c, null);
}

public static String checkNotContains(@Nullable String value, String c) {
return checkNotContains(value, "" + c, null);
}

public static String checkNotContains(@Nullable String value, String chunk, String name) {
checkNotNull(value, "value");
checkNotNull(chunk, "chunk");
if (value.contains(chunk)) {
String field = name == null ? "string" : name;
String message = message(field + " to not contain: " + quote(chunk), value);
throw new IllegalArgumentException(message);
}
return value;
}

public static String checkNotContainsAnyChar(@Nullable String value, String chars) {
return checkNotContainsAnyChar(value, chars, null);
}

public static String checkNotContainsAnyChar(@Nullable String value, String chars, String name) {
checkNotNull(value, "value");
checkNotNull(chars, "chars");
if (Strings.containsAnyChar(value, chars)) {
String field = name == null ? "string" : name;
String message = message(field + " to not contain any character from: " + quote(chars), value);
throw new IllegalArgumentException(message);
}
return value;
}

public static String checkNotEmpty(@Nullable String value) {
return checkNotEmpty(value, null);
}
Expand Down
44 changes: 43 additions & 1 deletion src/main/java/com/coditory/quark/common/check/Asserts.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coditory.quark.common.check;

import com.coditory.quark.common.util.Strings;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
Expand All @@ -9,6 +10,9 @@
import java.util.function.Function;
import java.util.function.Supplier;

import static com.coditory.quark.common.check.Args.checkNotNegative;
import static com.coditory.quark.common.check.Args.checkNotNull;
import static com.coditory.quark.common.util.Strings.quote;
import static java.lang.String.format;

public final class Asserts {
Expand Down Expand Up @@ -70,7 +74,7 @@ public static int assertPositionIndex(int index, int size) {
}

public static int assertPositionIndex(int index, int size, String indexName) {
Args.checkNotNegative(size, "size");
checkNotNegative(size, "size");
if (index < 0 || index >= size) {
throw new IllegalStateException(badPositionIndex(index, size, indexName));
}
Expand Down Expand Up @@ -112,6 +116,44 @@ public static String assertNotBlank(@Nullable String value, String name) {
return value;
}

public static String assertNotContains(@Nullable String value, char c) {
return assertNotContains(value, c, null);
}

public static String assertNotContains(@Nullable String value, char c, String name) {
return assertNotContains(value, "" + c, null);
}

public static String assertNotContains(@Nullable String value, String c) {
return assertNotContains(value, "" + c, null);
}

public static String assertNotContains(@Nullable String value, String chunk, String name) {
checkNotNull(value, "value");
checkNotNull(chunk, "chunk");
if (value.contains(chunk)) {
String field = name == null ? "string" : name;
String message = message(field + " to not contain: " + quote(chunk), value);
throw new IllegalStateException(message);
}
return value;
}

public static String assertNotContainsAnyChar(@Nullable String value, String chars) {
return assertNotContainsAnyChar(value, chars, null);
}

public static String assertNotContainsAnyChar(@Nullable String value, String chars, String name) {
checkNotNull(value, "value");
checkNotNull(chars, "chars");
if (Strings.containsAnyChar(value, chars)) {
String field = name == null ? "string" : name;
String message = message(field + " to not contain any character from: " + quote(chars), value);
throw new IllegalStateException(message);
}
return value;
}

public static <E, C extends Collection<E>> C assertNotEmpty(@Nullable C collection) {
return assertNotEmpty(collection, null);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/coditory/quark/common/collection/Lists.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Random;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import static com.coditory.quark.common.check.Args.checkNotEmpty;
import static com.coditory.quark.common.check.Args.checkNotNull;
Expand All @@ -34,6 +35,14 @@ public static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {
.collect(toUnmodifiableList());
}

public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
checkNotNull(list, "list");
checkNotNull(predicate, "predicate");
return list.stream()
.filter(predicate)
.collect(toUnmodifiableList());
}

public static <T> List<T> nullToEmpty(@Nullable List<T> list) {
return list == null ? List.of() : list;
}
Expand Down
Loading

0 comments on commit cdaa1a4

Please sign in to comment.