Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 - Introduced string predicates #11

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/main/java/io/github/avegera/predicate4j/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ private Predicates() {
//private constructor
}

//Object predicates

public static <T> Predicate<T> isEqualTo(T object) {
return it -> Objects.equals(it, object);
}
Expand Down Expand Up @@ -45,6 +47,8 @@ public static <T> Predicate<T> alwaysFalse() {
return it -> false;
}

//Boolean predicates

public static Predicate<Boolean> isTrue() {
return bool -> bool != null && bool;
}
Expand All @@ -61,6 +65,8 @@ public static Predicate<Boolean> notFalse() {
return bool -> bool == null || bool;
}

//Collection predicates

public static <T extends Collection<?>> Predicate<T> isEmpty() {
return collection -> collection == null || collection.isEmpty();
}
Expand All @@ -77,11 +83,53 @@ public static <T extends Collection<?>> Predicate<T> notHaveSize(int size) {
return collection -> (collection != null ? collection.size() : 0) != size;
}

public static <T extends Collection<R>, R> Predicate<T> contains(R element) {
public static <T extends Collection<R>, R> Predicate<T> containsElement(R element) {
return collection -> collection != null && collection.contains(element);
}

public static <T extends Collection<R>, R> Predicate<T> notContain(R element) {
public static <T extends Collection<R>, R> Predicate<T> notContainsElement(R element) {
return collection -> collection == null || !collection.contains(element);
}

//String predicates

public static Predicate<String> isEmptyString() {
return str -> str == null || str.isEmpty();
}

public static Predicate<String> notEmptyString() {
return str -> str != null && !str.isEmpty();
}

public static Predicate<String> containsSubstring(String substring) {
return str -> str != null && str.contains(substring);
}

public static Predicate<String> notContainsSubstring(String substring) {
return str -> str == null || !str.contains(substring);
}

public static Predicate<String> startsWith(String prefix) {
return str -> str != null && prefix != null && str.startsWith(prefix);
}

public static Predicate<String> notStartsWith(String prefix) {
return str -> str == null || prefix == null || !str.startsWith(prefix);
}

public static Predicate<String> endsWith(String suffix) {
return str -> str != null && suffix != null && str.endsWith(suffix);
}

public static Predicate<String> notEndsWith(String suffix) {
return str -> str == null || suffix == null || !str.endsWith(suffix);
}

public static Predicate<String> matches(String regex) {
return str -> str != null && regex != null && str.matches(regex);
}

public static Predicate<String> notMatches(String regex) {
return str -> str == null || regex == null || !str.matches(regex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface WhereList<T, R> extends WhereObject<T, List<R>> {

RichPredicate<T> contains(R element);

RichPredicate<T> notContain(R element);
RichPredicate<T> notContains(R element);
}
24 changes: 24 additions & 0 deletions src/main/java/io/github/avegera/predicate4j/api/WhereString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.avegera.predicate4j.api;

public interface WhereString<T> extends WhereObject<T, String> {

RichPredicate<T> isEmpty();

RichPredicate<T> notEmpty();

RichPredicate<T> contains(String substring);

RichPredicate<T> notContains(String substring);

RichPredicate<T> startsWith(String prefix);

RichPredicate<T> notStartsWith(String prefix);

RichPredicate<T> endsWith(String suffix);

RichPredicate<T> notEndsWith(String suffix);

RichPredicate<T> matches(String regex);

RichPredicate<T> notMatches(String regex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface WhereType {
<T> WhereBoolean<T> booleanValue(Function<T, Boolean> mapper);

<T, R> WhereList<T, R> list(Function<T, List<R>> mapper);

<T> WhereString<T> string(Function<T, String> mapper);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public RichPredicate<T> notHaveSize(int size) {

@Override
public RichPredicate<T> contains(R element) {
return getPredicate(Predicates.contains(element));
return getPredicate(Predicates.containsElement(element));
}

@Override
public RichPredicate<T> notContain(R element) {
return getPredicate(Predicates.notContain(element));
public RichPredicate<T> notContains(R element) {
return getPredicate(Predicates.notContainsElement(element));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.avegera.predicate4j.impl;

import io.github.avegera.predicate4j.Predicates;
import io.github.avegera.predicate4j.api.RichPredicate;
import io.github.avegera.predicate4j.api.WhereString;

import java.util.function.Function;

public class WhereStringImpl<T> extends WhereObjectImpl<T, String> implements WhereString<T> {

public WhereStringImpl(Function<T, String> mapper) {
super(mapper);
}

@Override
public RichPredicate<T> isEmpty() {
return getPredicate(Predicates.isEmptyString());
}

@Override
public RichPredicate<T> notEmpty() {
return getPredicate(Predicates.notEmptyString());
}

@Override
public RichPredicate<T> contains(String substring) {
return getPredicate(Predicates.containsSubstring(substring));
}

@Override
public RichPredicate<T> notContains(String substring) {
return getPredicate(Predicates.notContainsSubstring(substring));
}

@Override
public RichPredicate<T> startsWith(String prefix) {
return getPredicate(Predicates.startsWith(prefix));
}

@Override
public RichPredicate<T> notStartsWith(String prefix) {
return getPredicate(Predicates.notStartsWith(prefix));
}

@Override
public RichPredicate<T> endsWith(String suffix) {
return getPredicate(Predicates.endsWith(suffix));
}

@Override
public RichPredicate<T> notEndsWith(String suffix) {
return getPredicate(Predicates.notEndsWith(suffix));
}

@Override
public RichPredicate<T> matches(String regex) {
return getPredicate(Predicates.matches(regex));
}

@Override
public RichPredicate<T> notMatches(String regex) {
return getPredicate(Predicates.notMatches(regex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.avegera.predicate4j.api.WhereBoolean;
import io.github.avegera.predicate4j.api.WhereList;
import io.github.avegera.predicate4j.api.WhereString;
import io.github.avegera.predicate4j.api.WhereType;

import java.util.List;
Expand All @@ -18,4 +19,9 @@ public <T> WhereBoolean<T> booleanValue(Function<T, Boolean> mapper) {
public <T, R> WhereList<T, R> list(Function<T, List<R>> mapper) {
return new WhereListImpl<>(mapper);
}

@Override
public <T> WhereString<T> string(Function<T, String> mapper) {
return new WhereStringImpl<>(mapper);
}
}
Loading