-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
266 additions
and
68 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
litool-core/src/main/java/io/leaderli/litool/core/function/Chain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.leaderli.litool.core.function; | ||
|
||
public interface Chain<T> { | ||
|
||
Chain<T> addHead(T filter); | ||
|
||
Chain<T> add(T filter); | ||
} |
51 changes: 51 additions & 0 deletions
51
litool-core/src/main/java/io/leaderli/litool/core/lang/FilterChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.leaderli.litool.core.lang; | ||
|
||
import io.leaderli.litool.core.function.Chain; | ||
import io.leaderli.litool.core.function.Filter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FilterChain<T> implements Filter<T>, Chain<Filter<T>> { | ||
|
||
private final List<Filter<T>> chain = new ArrayList<>(); | ||
|
||
public FilterChain() { | ||
|
||
} | ||
|
||
public FilterChain(Filter<T> filter) { | ||
add(filter); | ||
} | ||
|
||
@Override | ||
public Boolean apply(T t) { | ||
for (Filter<T> filter : chain) { | ||
if (!filter.apply(t)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public FilterChain<T> addHead(Filter<T> filter) { | ||
if (filter instanceof FilterChain) { | ||
((FilterChain<Object>) filter).chain.forEach(f -> addHead((Filter<T>) f)); | ||
} else { | ||
chain.add(0, filter); | ||
} | ||
return this; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public FilterChain<T> add(Filter<T> filter) { | ||
if (filter instanceof FilterChain) { | ||
((FilterChain<Object>) filter).chain.forEach(f -> add((Filter<T>) f)); | ||
} else { | ||
chain.add(filter); | ||
} | ||
return this; | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
litool-core/src/main/java/io/leaderli/litool/core/type/MethodFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.leaderli.litool.core.type; | ||
|
||
import io.leaderli.litool.core.function.Chain; | ||
import io.leaderli.litool.core.function.Filter; | ||
import io.leaderli.litool.core.lang.FilterChain; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Objects; | ||
|
||
public class MethodFilter implements Filter<Method>, Chain<Filter<Method>> { | ||
|
||
private final FilterChain<Method> filter = new FilterChain<>(); | ||
|
||
|
||
@Override | ||
public Boolean apply(Method method) { | ||
return filter.apply(method); | ||
} | ||
|
||
|
||
// public static class Builder { | ||
// | ||
// | ||
public static MethodFilter isMethod() { | ||
return new MethodFilter().add(Objects::nonNull); | ||
} | ||
|
||
public static MethodFilter name(String name) { | ||
return new MethodFilter().add(m -> m.getName().equals(name)); | ||
} | ||
|
||
public static MethodFilter isPublic() { | ||
return new MethodFilter().add(ModifierUtil::isPublic); | ||
} | ||
|
||
public static MethodFilter of(Filter<Method> filter) { | ||
return new MethodFilter().add(filter); | ||
} | ||
|
||
// public MethodFilter name(String name) { | ||
// return new MethodFilter().add(m -> m.getName().equals(name)); | ||
// } | ||
// | ||
// public void returnType(Class<?> returnType) { | ||
// this.returnType = returnType; | ||
// } | ||
// | ||
// public void parameterType(Class<?>[] parameterType) { | ||
// this.parameterType = parameterType; | ||
// } | ||
// | ||
// public void parameterlenth(Class<?>[] parameterlenth) { | ||
// this.parameterlenth = parameterlenth; | ||
// } | ||
// | ||
// public void annotated(Class<? extends Annotation> annotated) { | ||
// this.annotated = annotated; | ||
// } | ||
// | ||
public void modifiers(int modifiers) { | ||
} | ||
|
||
@Override | ||
public MethodFilter addHead(Filter<Method> filter) { | ||
this.filter.addHead(filter); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MethodFilter add(Filter<Method> filter) { | ||
this.filter.add(filter); | ||
return this; | ||
} | ||
|
||
|
||
// public void isAbstract() { | ||
// this.modifiers |= Modifier.ABSTRACT; | ||
// } | ||
// | ||
// public void isStatic() { | ||
// this.modifiers |= Modifier.STATIC; | ||
// } | ||
|
||
|
||
// } | ||
|
||
|
||
} |
55 changes: 55 additions & 0 deletions
55
litool-core/src/test/java/io/leaderli/litool/core/lang/FilterChainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.leaderli.litool.core.lang; | ||
|
||
import io.leaderli.litool.core.meta.LiBox; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class FilterChainTest { | ||
|
||
@Test | ||
void test() { | ||
|
||
FilterChain<Integer> filter = new FilterChain<>(); | ||
filter.add(i -> i > 10); | ||
|
||
Assertions.assertTrue(filter.apply(11)); | ||
Assertions.assertFalse(filter.apply(9)); | ||
filter.add(i -> i < 100); | ||
Assertions.assertTrue(filter.apply(11)); | ||
Assertions.assertFalse(filter.apply(101)); | ||
|
||
FilterChain<Integer> filter2 = new FilterChain<>(); | ||
filter2.add(filter); | ||
Assertions.assertFalse(filter2.apply(9)); | ||
Assertions.assertTrue(filter2.apply(11)); | ||
Assertions.assertFalse(filter2.apply(101)); | ||
|
||
FilterChain<Integer> filter3 = new FilterChain<>(); | ||
filter3.add(filter2); | ||
filter3.add(i -> i % 2 == 0); | ||
Assertions.assertFalse(filter3.apply(9)); | ||
Assertions.assertTrue(filter3.apply(12)); | ||
Assertions.assertFalse(filter3.apply(11)); | ||
Assertions.assertFalse(filter3.apply(101)); | ||
|
||
FilterChain<Integer> filter4 = new FilterChain<>(); | ||
LiBox<Integer> box = LiBox.of(0); | ||
filter4.add(i -> { | ||
box.value(1); | ||
return true; | ||
}); | ||
filter4.add(i -> { | ||
box.value(2); | ||
return false; | ||
}); | ||
filter4.apply(1); | ||
|
||
Assertions.assertEquals(2, box.value()); | ||
box.value(0); | ||
filter4.addHead(i -> false); | ||
filter4.apply(1); | ||
Assertions.assertEquals(0, box.value()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
litool-test/src/main/java/io/leaderli/litool/test/MethodFilter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.