TagFilter { @Nullable public static TagFilter create(@Nullable final Collection<String> tags, @Nullable final FilteringStrategy requestedFilteringStrategy) { final FilteringStrategy filteringStrategy = requestedFilteringStrategy == null ? FilteringStrategy.getDefault() : requestedFilteringStrategy; if (FilteringStrategy.UNTAGGED.equals(filteringStrategy)) { return new TagFilter(null, FilteringStrategy.UNTAGGED); } else { if (tags != null) { final Set<String> cleansedTags = new HashSet<String>(); for (final String tag : tags) { final String cleansedTag = Tag.cleanse(tag); if (cleansedTag.length() > 0) { cleansedTags.add(cleansedTag); } } if (!cleansedTags.isEmpty()) { return new TagFilter(cleansedTags, filteringStrategy); } } } return null; } private  TagFilter(@Nullable final Collection<String> tags, @NotNull final FilteringStrategy filteringStrategy); @Nullable static TagFilter create(@Nullable final Collection<String> tags,
                                   @Nullable final FilteringStrategy requestedFilteringStrategy); @NotNull Set<String> getTags(); @NotNull FilteringStrategy getFilteringStrategy(); @NotNull String getWhereClause(); @Override String toString();  }
@Test public void testCreateMethod() { final List<String> emptyList = new ArrayList<String>(); final List<String> oneTag = Arrays.asList("foo"); final List<String> multipleTags = Arrays.asList("foo", "bar"); final List<String> tagsWithOneEmptyItem = Arrays.asList("foo", ""); final List<String> tagsWithDuplicateItems = Arrays.asList("foo", "Foo", "bar", " bar "); final List<String> tagsWithAllEmptyItems = Arrays.asList("\t", " ", ""); Assert.assertNull(TagFilter.create(null, null)); Assert.assertNull(TagFilter.create(null, TagFilter.FilteringStrategy.ALL)); Assert.assertNull(TagFilter.create(null, TagFilter.FilteringStrategy.ANY)); Assert.assertNull(TagFilter.create(null, TagFilter.FilteringStrategy.NONE)); Assert.assertNull(TagFilter.create(emptyList, null)); Assert.assertNull(TagFilter.create(emptyList, TagFilter.FilteringStrategy.ALL)); Assert.assertNull(TagFilter.create(emptyList, TagFilter.FilteringStrategy.ANY)); Assert.assertNull(TagFilter.create(emptyList, TagFilter.FilteringStrategy.NONE)); Assert.assertNull(TagFilter.create(tagsWithAllEmptyItems, TagFilter.FilteringStrategy.ALL)); Assert.assertNull(TagFilter.create(tagsWithAllEmptyItems, TagFilter.FilteringStrategy.ANY)); Assert.assertNull(TagFilter.create(tagsWithAllEmptyItems, TagFilter.FilteringStrategy.NONE)); testUntagged(TagFilter.create(null, TagFilter.FilteringStrategy.UNTAGGED)); testUntagged(TagFilter.create(oneTag, TagFilter.FilteringStrategy.UNTAGGED)); testUntagged(TagFilter.create(multipleTags, TagFilter.FilteringStrategy.UNTAGGED)); testUntagged(TagFilter.create(tagsWithOneEmptyItem, TagFilter.FilteringStrategy.UNTAGGED)); testUntagged(TagFilter.create(tagsWithDuplicateItems, TagFilter.FilteringStrategy.UNTAGGED)); testUntagged(TagFilter.create(tagsWithAllEmptyItems, TagFilter.FilteringStrategy.UNTAGGED)); testTagFilter(TagFilter.create(oneTag, TagFilter.FilteringStrategy.ALL), TagFilter.FilteringStrategy.ALL, 1, new String[]{"facet.tags like '%,foo,%'"}); testTagFilter(TagFilter.create(multipleTags, TagFilter.FilteringStrategy.ALL), TagFilter.FilteringStrategy.ALL, 2, new String[]{"facet.tags like '%,foo,%'", "AND", "facet.tags like '%,bar,%'"}); testTagFilter(TagFilter.create(tagsWithDuplicateItems, TagFilter.FilteringStrategy.ALL), TagFilter.FilteringStrategy.ALL, 2, new String[]{"facet.tags like '%,foo,%'", "AND", "facet.tags like '%,bar,%'"}); testTagFilter(TagFilter.create(tagsWithOneEmptyItem, TagFilter.FilteringStrategy.ALL), TagFilter.FilteringStrategy.ALL, 1, new String[]{"facet.tags like '%,foo,%'"}); testTagFilter(TagFilter.create(oneTag, TagFilter.FilteringStrategy.ANY), TagFilter.FilteringStrategy.ANY, 1, new String[]{"facet.tags like '%,foo,%'"}); testTagFilter(TagFilter.create(multipleTags, TagFilter.FilteringStrategy.ANY), TagFilter.FilteringStrategy.ANY, 2, new String[]{"facet.tags like '%,foo,%'", "OR", "facet.tags like '%,bar,%'"}); testTagFilter(TagFilter.create(tagsWithDuplicateItems, TagFilter.FilteringStrategy.ANY), TagFilter.FilteringStrategy.ANY, 2, new String[]{"facet.tags like '%,foo,%'", "OR", "facet.tags like '%,bar,%'"}); testTagFilter(TagFilter.create(tagsWithOneEmptyItem, TagFilter.FilteringStrategy.ANY), TagFilter.FilteringStrategy.ANY, 1, new String[]{"facet.tags like '%,foo,%'"}); testTagFilter(TagFilter.create(oneTag, TagFilter.FilteringStrategy.NONE), TagFilter.FilteringStrategy.NONE, 1, new String[]{"facet.tags is NULL OR (facet.tags NOT like '%,foo,%')"}); testTagFilter(TagFilter.create(multipleTags, TagFilter.FilteringStrategy.NONE), TagFilter.FilteringStrategy.NONE, 2, new String[]{"facet.tags is NULL OR (", "facet.tags NOT like '%,foo,%'", "AND", "facet.tags NOT like '%,bar,%'",")"}); testTagFilter(TagFilter.create(tagsWithDuplicateItems, TagFilter.FilteringStrategy.NONE), TagFilter.FilteringStrategy.NONE, 2, new String[]{"facet.tags is NULL OR (", "facet.tags NOT like '%,foo,%'", "AND", "facet.tags NOT like '%,bar,%'", ")"}); testTagFilter(TagFilter.create(tagsWithOneEmptyItem, TagFilter.FilteringStrategy.NONE), TagFilter.FilteringStrategy.NONE, 1, new String[]{"facet.tags is NULL OR (facet.tags NOT like '%,foo,%')"}); }