-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
navi-core/src/main/java/com/github/navi/core/matcher/IntersectionMatcher.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,79 @@ | ||
package com.github.navi.core.matcher; | ||
|
||
import com.github.navi.core.MatchResult; | ||
import com.github.navi.core.MatcherType; | ||
import org.apache.commons.collections.CollectionUtils; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@MatcherType(processor = IntersectionMatcher.Processor.class) | ||
public @interface IntersectionMatcher { | ||
String propertyPath(); | ||
|
||
String[] expectValue(); | ||
|
||
String separator() default ","; | ||
|
||
class Processor extends OnePropertyMatcherProcessor<IntersectionMatcher> { | ||
|
||
@Override | ||
protected String getPropertyPath(IntersectionMatcher matcherAnnotation) { | ||
return matcherAnnotation.propertyPath(); | ||
} | ||
|
||
@Override | ||
protected MatchResult doProcess(Object property, IntersectionMatcher matcherAnnotation) { | ||
List<String> expectValueList = Arrays.asList(matcherAnnotation.expectValue()); | ||
|
||
boolean isIntersectedOnCollection = isIntersectOnCollection(property, expectValueList); | ||
boolean isIntersectionOnString = isIntersectOnString(property, expectValueList, | ||
matcherAnnotation.separator()); | ||
boolean isIntersected = isIntersectedOnCollection || isIntersectionOnString; | ||
return isIntersected ? MatchResult.ACCEPT : MatchResult.REJECT; | ||
} | ||
|
||
private boolean isIntersectOnString(Object property, List<String> expectValueList, String separator) { | ||
if (!(property instanceof String)) { | ||
return false; | ||
} | ||
|
||
String propertyValue = (String) property; | ||
List<String> propertyValues = Arrays.asList(propertyValue.split(separator)); | ||
return isIntersectOnCollection(propertyValues, expectValueList); | ||
} | ||
|
||
// TODO Check generic type | ||
@SuppressWarnings("unchecked") | ||
private boolean isIntersectOnCollection(Object property, List<String> expectValueList) { | ||
try { | ||
if (property instanceof Collection) { | ||
Collection<String> collection = new ArrayList<>(); | ||
for (String v : (Collection<String>) property) { | ||
collection.add(v.trim()); | ||
} | ||
|
||
Collection intersectResult = CollectionUtils.intersection(expectValueList, collection); | ||
return !intersectResult.isEmpty(); | ||
} else { | ||
return false; | ||
} | ||
} catch (Exception e) { | ||
// TODO Log | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,9 +18,6 @@ | |
|
||
} | ||
|
||
interface Handler { | ||
} | ||
|
||
@AndroidV1Matcher | ||
class AndroidV1Handler implements Handler { | ||
|
||
|
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,4 @@ | ||
package com.github.navi.core; | ||
|
||
public interface Handler { | ||
} |
32 changes: 32 additions & 0 deletions
32
navi-core/src/test/java/com/github/navi/core/matcher/IntersectionMatcherTests.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,32 @@ | ||
package com.github.navi.core.matcher; | ||
|
||
import com.github.navi.core.Handler; | ||
import com.github.navi.core.SimpleSelector; | ||
import org.assertj.core.util.Sets; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
|
||
public class IntersectionMatcherTests { | ||
@Test | ||
public void intersect_string() { | ||
Map<String, String> request = new HashMap<>(); | ||
request.put("name", "stark, thor"); | ||
|
||
SimpleSelector simpleSelector = new SimpleSelector(); | ||
simpleSelector.registerCandidates(Handler.class, Sets.newLinkedHashSet(new IntersectHandler())); | ||
|
||
Handler handler = simpleSelector.select(request, Handler.class); | ||
|
||
assertThat(handler).isInstanceOf(IntersectHandler.class); | ||
} | ||
} | ||
|
||
|
||
@IntersectionMatcher(propertyPath = "name", expectValue = {"stark", "rogers"}) | ||
class IntersectHandler implements Handler { | ||
|
||
} |