Example #1
0
  /**
   * Creates a <code>TagFilter</code> from the given {@link Collection} of tags and {@link
   * FilteringStrategy} using the following rules:
   *
   * <ul>
   *   <li>If the {@link FilteringStrategy} is <code>null</code>, the {@link
   *       FilteringStrategy#getDefault() default} is used instead.
   *   <li>If the given {@link Collection} of tags is non-<code>null</code>, then a cleansed {@link
   *       Collection} of unique tags is created by only adding tags which are non-empty after being
   *       {@link Tag#cleanse(String) cleansed}.
   *   <li>If the {@link FilteringStrategy} is {@link FilteringStrategy#UNTAGGED}, then the given
   *       {@link Collection} of <code>tags</code> is ignored and a <code>TagFilter</code> is
   *       created with an empty {@link Collection} of tags.
   *   <li>If the {@link FilteringStrategy} is <i>not</i> {@link FilteringStrategy#UNTAGGED}, but
   *       the given cleansed {@link Collection} of <code>tags</code> is <code>null</code> or empty,
   *       then this method returns <code>null</code>.
   *   <li>If the {@link FilteringStrategy} is <i>not</i> {@link FilteringStrategy#UNTAGGED}, and
   *       the given {@link Collection} of <code>tags</code> is non-<code>null</code> and non-empty,
   *       then this method creates a <code>TagFilter</code> using the given unique, cleansed <code>
   *       tags</code> and {@link FilteringStrategy}.
   * </ul>
   */
  @Nullable
  public static TagFilter create(
      @Nullable final Collection<String> tags,
      @Nullable final FilteringStrategy requestedFilteringStrategy) {
    // make sure the filtering strategy is non-null, choosing the default if necessary
    final FilteringStrategy filteringStrategy =
        requestedFilteringStrategy == null
            ? FilteringStrategy.getDefault()
            : requestedFilteringStrategy;

    if (FilteringStrategy.UNTAGGED.equals(filteringStrategy)) {
      // filter for untagged items
      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;
  }