From 4ce8e9365f256b6bb49f0a7757a57ac0454daa1a Mon Sep 17 00:00:00 2001 From: buczekp <buczekp> Date: Wed, 15 Sep 2010 11:47:57 +0000 Subject: [PATCH] [LMS-1683] added ' & ' as conjunction separator SVN: 17842 --- .../basic/AlternativesStringFilter.java | 29 +++++++++++++++++++ .../basic/AlternativesStringFilterTest.java | 21 ++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java b/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java index e36dd81a1b3..73f64973fac 100644 --- a/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java +++ b/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java @@ -248,14 +248,38 @@ public class AlternativesStringFilter } } + static class ConjunctionMatcher implements Matcher + { + private Matcher m1; + + private Matcher m2; + + ConjunctionMatcher(Matcher m1, Matcher m2) + { + this.m1 = m1; + this.m2 = m2; + } + + public boolean matches(String value) + { + return m1.matches(value) && m2.matches(value); + } + } + /** * Sets a new filter <var>value</var>. */ public void setFilterValue(String value) { alternatives.clear(); + boolean conjunct = false; for (String s : StringUtils.tokenize(value)) { + if (s.equals("&") && alternatives.size() > 0) + { + conjunct = true; + continue; + } final boolean negateValue = s.startsWith(PREFIX_NOT); if (negateValue) { @@ -270,6 +294,11 @@ public class AlternativesStringFilter { matcher = new NegationMatcher(matcher); } + if (conjunct) + { + Matcher previousMatcher = alternatives.remove(alternatives.size() - 1); + matcher = new ConjunctionMatcher(previousMatcher, matcher); + } alternatives.add(matcher); } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilterTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilterTest.java index 0d20af51193..a618849bd7a 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilterTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilterTest.java @@ -453,4 +453,25 @@ public class AlternativesStringFilterTest assertTrue(filter.passes("!=10.3")); } + @Test + public void testNumericalConjunction() + { + AlternativesStringFilter filter = prepare(">5 & <10"); + assertFalse(filter.passes("4")); + assertTrue(filter.passes("7")); + assertFalse(filter.passes("12")); + } + + @Test + public void testNonNumericalConjunction() + { + AlternativesStringFilter filter = prepare("ab & ba"); + assertFalse(filter.passes("ab")); + assertFalse(filter.passes("abb")); + assertTrue(filter.passes("aba")); + assertTrue(filter.passes("bab")); + assertTrue(filter.passes("abba")); + assertFalse(filter.passes("ba")); + assertFalse(filter.passes("bba")); + } } -- GitLab