[Major] moved StringMatchMode to ch.eitchnet.utils

This commit is contained in:
Robert von Burg 2014-08-25 22:52:25 +02:00
parent 30b9787ef9
commit 022ea46958
5 changed files with 41 additions and 72 deletions

View File

@ -39,6 +39,7 @@ import li.strolch.model.query.Selection;
import li.strolch.model.query.StrolchElementSelectionVisitor;
import li.strolch.persistence.api.StrolchDao;
import li.strolch.runtime.query.inmemory.ParameterSelector.StringParameterSelector;
import ch.eitchnet.utils.StringMatchMode;
import ch.eitchnet.utils.dbc.DBC;
/**
@ -139,8 +140,7 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
@Override
public void visit(NameSelection selection) {
addSelector(new NameSelector<T>(selection.getName()).caseInsensitive(selection.isCaseInsensitive()).contains(
selection.isContains()));
addSelector(new NameSelector<T>(selection.getName(), selection.getMatchMode()));
}
@Override
@ -150,9 +150,7 @@ public abstract class InMemoryQueryVisitor<T extends GroupedParameterizedElement
selection.getBagKey(), //
selection.getParamKey(), //
selection.getValue() //
)//
.contains(selection.isContains()) //
.caseInsensitive(selection.isCaseInsensitive());
, StringMatchMode.EQUALS_CASE_SENSITIVE);
addSelector(stringSelector);
}

View File

@ -16,6 +16,7 @@
package li.strolch.runtime.query.inmemory;
import li.strolch.model.StrolchElement;
import ch.eitchnet.utils.StringMatchMode;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -23,44 +24,24 @@ import li.strolch.model.StrolchElement;
*/
public class NameSelector<T extends StrolchElement> implements Selector<T> {
private StringMatchMode matchMode;
private String name;
private boolean contains;
private boolean caseInsensitive;
public NameSelector(String name) {
public NameSelector(String name, StringMatchMode matchMode) {
this.name = name;
this.matchMode = matchMode;
}
@Override
public boolean select(T element) {
String name = element.getName();
if (this.contains && this.caseInsensitive)
return name.toLowerCase().contains(this.name.toLowerCase());
if (this.caseInsensitive)
return name.toLowerCase().equals(this.name.toLowerCase());
if (this.contains)
return name.contains(this.name);
return name.equals(this.name);
return this.matchMode.matches(this.name, name);
}
public boolean isContains() {
return this.contains;
}
public boolean isCaseInsensitive() {
return this.caseInsensitive;
}
public NameSelector<T> contains(boolean contains) {
this.contains = contains;
return this;
}
public NameSelector<T> caseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
return this;
/**
* @return the matchMode
*/
public StringMatchMode getMatchMode() {
return this.matchMode;
}
}

View File

@ -27,6 +27,7 @@ import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongParameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import ch.eitchnet.utils.StringMatchMode;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -45,8 +46,8 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
public abstract boolean select(GroupedParameterizedElement element);
public static <T extends GroupedParameterizedElement> StringParameterSelector<T> stringSelector(String bagKey,
String paramKey, String value) {
return new StringParameterSelector<>(bagKey, paramKey, value);
String paramKey, String value, StringMatchMode matchMode) {
return new StringParameterSelector<>(bagKey, paramKey, value, matchMode);
}
public static <T extends GroupedParameterizedElement> IntegerParameterSelector<T> integerSelector(String bagKey,
@ -86,31 +87,17 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
public static class StringParameterSelector<T extends GroupedParameterizedElement> extends ParameterSelector<T> {
private StringMatchMode matchMode;
private String value;
private boolean contains;
private boolean caseInsensitive;
public StringParameterSelector(String bagKey, String paramKey, String value) {
public StringParameterSelector(String bagKey, String paramKey, String value, StringMatchMode matchMode) {
super(bagKey, paramKey);
this.value = value;
this.matchMode = matchMode;
}
public boolean isContains() {
return this.contains;
}
public boolean isCaseInsensitive() {
return this.caseInsensitive;
}
public StringParameterSelector<T> contains(boolean contains) {
this.contains = contains;
return this;
}
public StringParameterSelector<T> caseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
return this;
public StringMatchMode getMatchMode() {
return this.matchMode;
}
@Override
@ -126,16 +113,7 @@ public abstract class ParameterSelector<T extends GroupedParameterizedElement> i
StringParameter param = bag.getParameter(this.paramKey);
String paramValue = param.getValue();
if (this.contains && this.caseInsensitive)
return paramValue.toLowerCase().contains(this.value.toLowerCase());
if (this.caseInsensitive)
return paramValue.toLowerCase().equals(this.value.toLowerCase());
if (this.contains)
return paramValue.contains(this.value);
return paramValue.equals(this.value);
return this.matchMode.matches(paramValue, this.value);
}
}

View File

@ -35,6 +35,8 @@ import li.strolch.persistence.inmemory.InMemoryResourceDao;
import org.junit.Test;
import ch.eitchnet.utils.StringMatchMode;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -107,7 +109,7 @@ public class InMemoryQueryTest {
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
List<Selector<Resource>> andSelectors = new ArrayList<>();
andSelectors.add(new IdSelector<Resource>("@3"));
andSelectors.add(new NameSelector<Resource>("Res 3"));
andSelectors.add(new NameSelector<Resource>("Res 3", StringMatchMode.EQUALS_CASE_SENSITIVE));
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.setSelector(andSelector);
@ -128,7 +130,7 @@ public class InMemoryQueryTest {
resourceQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
List<Selector<Resource>> andSelectors = new ArrayList<>();
andSelectors.add(new IdSelector<Resource>("@3"));
andSelectors.add(new NameSelector<Resource>("Res 4"));
andSelectors.add(new NameSelector<Resource>("Res 4", StringMatchMode.EQUALS_CASE_SENSITIVE));
BooleanSelector<Resource> andSelector = new AndSelector<Resource>(andSelectors);
resourceQuery.setSelector(andSelector);
@ -148,7 +150,8 @@ public class InMemoryQueryTest {
ballQuery.setNavigator(new AnyNavigator<Resource>());
ballQuery.setElementVisitor(new NoStrategyVisitor<Resource>());
AndSelector<Resource> andSelector = new AndSelector<>();
andSelector.with(ParameterSelector.<Resource> stringSelector("parameters", "color", "red"));
andSelector.with(ParameterSelector.<Resource> stringSelector("parameters", "color", "red",
StringMatchMode.EQUALS_CASE_SENSITIVE));
andSelector.with(ParameterSelector.<Resource> booleanSelector("parameters", "forChildren", true));
andSelector.with(ParameterSelector.<Resource> floatSelector("parameters", "diameter", 22.0));
ballQuery.setSelector(andSelector);

View File

@ -43,6 +43,7 @@ import li.strolch.runtime.StrolchConstants;
import org.junit.Test;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.utils.StringMatchMode;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -136,7 +137,9 @@ public class QueryTest {
}
ResourceQuery query = ResourceQuery.query("MyType");
query.and().with(ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "olch").contains(true));
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "olch",
StringMatchMode.CONTAINS_CASE_SENSITIVE));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = tx.doQuery(query);
@ -160,7 +163,9 @@ public class QueryTest {
}
ResourceQuery query = ResourceQuery.query("MyType");
query.and().with(ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "str").contains(true));
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "str",
StringMatchMode.CONTAINS_CASE_SENSITIVE));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = tx.doQuery(query);
@ -183,7 +188,9 @@ public class QueryTest {
}
ResourceQuery query = ResourceQuery.query("MyType");
query.and().with(ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch").caseInsensitive(true));
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch",
StringMatchMode.EQUALS_CASE_INSENSITIVE));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = tx.doQuery(query);
@ -207,7 +214,9 @@ public class QueryTest {
}
ResourceQuery query = ResourceQuery.query("MyType");
query.and().with(ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch"));
query.and().with(
ParameterSelection.stringSelection(BAG_ID, PARAM_STRING_ID, "strolch",
StringMatchMode.EQUALS_CASE_SENSITIVE));
List<Resource> result;
try (StrolchTransaction tx = container.getRealm(StrolchConstants.DEFAULT_REALM).openTx(certificate, "test")) {
result = tx.doQuery(query);