Skip to content

Commit

Permalink
Provide IntersectionMatcher. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglifan committed May 20, 2018
1 parent 6a74b6b commit f441ca2
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

}

interface Handler {
}

@AndroidV1Matcher
class AndroidV1Handler implements Handler {

Expand Down
4 changes: 4 additions & 0 deletions navi-core/src/test/java/com/github/navi/core/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.navi.core;

public interface Handler {
}
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 {

}

0 comments on commit f441ca2

Please sign in to comment.