Skip to content

Commit

Permalink
Merge pull request #8 from avegera/7-add-list-predicates
Browse files Browse the repository at this point in the history
#7 - Introduced list predicates
  • Loading branch information
avegera authored Jul 2, 2024
2 parents 1450395 + 7a59882 commit 70ed066
Show file tree
Hide file tree
Showing 8 changed files with 760 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/io/github/avegera/predicate4j/Predicates.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.avegera.predicate4j;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Predicate;

Expand Down Expand Up @@ -59,4 +60,28 @@ public static Predicate<Boolean> isFalse() {
public static Predicate<Boolean> notFalse() {
return bool -> bool == null || bool;
}

public static <T extends Collection<?>> Predicate<T> isEmpty() {
return collection -> collection == null || collection.isEmpty();
}

public static <T extends Collection<?>> Predicate<T> notEmpty() {
return collection -> collection != null && !collection.isEmpty();
}

public static <T extends Collection<?>> Predicate<T> hasSize(int size) {
return collection -> (collection != null ? collection.size() : 0) == size;
}

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) {
return collection -> collection != null && collection.contains(element);
}

public static <T extends Collection<R>, R> Predicate<T> notContain(R element) {
return collection -> collection == null || !collection.contains(element);
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/github/avegera/predicate4j/api/WhereList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.avegera.predicate4j.api;

import java.util.List;

public interface WhereList<T, R> extends WhereObject<T, List<R>> {

RichPredicate<T> isEmpty();

RichPredicate<T> notEmpty();

RichPredicate<T> hasSize(int size);

RichPredicate<T> notHaveSize(int size);

RichPredicate<T> contains(R element);

RichPredicate<T> notContain(R element);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.github.avegera.predicate4j.api;

import java.util.List;
import java.util.function.Function;

public interface WhereType {

<T> WhereBoolean<T> booleanValue(Function<T, Boolean> mapper);

<T, R> WhereList<T, R> list(Function<T, List<R>> mapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.WhereList;

import java.util.List;
import java.util.function.Function;

public class WhereListImpl<T, R> extends WhereObjectImpl<T, List<R>> implements WhereList<T, R> {

public WhereListImpl(Function<T, List<R>> mapper) {
super(mapper);
}

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

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

@Override
public RichPredicate<T> hasSize(int size) {
return getPredicate(Predicates.hasSize(size));
}

@Override
public RichPredicate<T> notHaveSize(int size) {
return getPredicate(Predicates.notHaveSize(size));
}

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

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

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

import java.util.List;
import java.util.function.Function;

public class WhereTypeImpl implements WhereType {
Expand All @@ -11,4 +13,9 @@ public class WhereTypeImpl implements WhereType {
public <T> WhereBoolean<T> booleanValue(Function<T, Boolean> mapper) {
return new WhereBooleanImpl<>(mapper);
}

@Override
public <T, R> WhereList<T, R> list(Function<T, List<R>> mapper) {
return new WhereListImpl<>(mapper);
}
}
Loading

0 comments on commit 70ed066

Please sign in to comment.