com.brightcove.zartan.common.catalog.TagSet.java Source code

Java tutorial

Introduction

Here is the source code for com.brightcove.zartan.common.catalog.TagSet.java

Source

/**
 * Copyright (C) 2012 Brightcove Inc. All Rights Reserved. No use, copying or distribution of this
 * work may be made except in accordance with a valid license agreement from Brightcove Inc. This
 * notice must be included on all copies, modifications and derivatives of this work.
 * 
 * Brightcove Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * "Brightcove" is a registered trademark of Brightcove Inc.
 */
package com.brightcove.zartan.common.catalog;

import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

/**
 * A TagSet is a text-normalized HashSet<String>. Its API may be used in 
 * exactly the same way as that for HashSet, but all params passed into its 
 * methods will be converted to lower case before forwarding to its HashSet 
 * parent, and TagSet construction will similarly perform text normalization 
 * on any passed in Strings before returning. 
 * <p>
 * As a result, the following calls to </tt>contains</tt>, 
 * </tt>containsAll</tt>, and </tt>remove</tt> will all return true, for 
 * example, since their params are treated case-insensitively: 
 * <p>
 * <pre>
 *     TagSet tagSet = new TagSet(Arrays.asList("tAg_OnE", "TaG_tWo");
 *     
 *     tagSet.add("TAG_three");        
 *     tagSet.contains("tag_ONE"); // returns true
 *     tagSet.containsAll(Arrays.asList("TAG_TWO", "tag_three")); // returns true
 *     
 *     tagSet = new TagSet();
 *     tagSet.addAll(Arrays.asList("tagliatelle", "TAGALONG"));
 *     tagSet.remove("Tagliatelle"); // returns true
 *     tagSet.remove("TagAlong"); // returns true
 * </pre>
 * <p>
 * TagSets are useful for testing the set of tags associated with a video   
 * title without regard to case sensitivity.
 */
public class TagSet extends HashSet<String> {
    private static final long serialVersionUID = 1846971865260316407L;

    public TagSet() {
        super();
    }

    public TagSet(Collection<? extends String> c) {
        super(c);
    }

    public TagSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public TagSet(int initialCapacity) {
        super(initialCapacity);
    }

    private String normalize(String string) {
        return string.toLowerCase();
    }

    /**
     * If <tt>obj</tt> is a <tt>String</tt>, converts it to lower case. If 
     * <tt>obj</tt> is a <tt>Collection</tt>, recursively calls 
     * <tt>normalize</tt> on each of its elements. 
     * @param obj the <tt>Object</tt> on which to perform text normalization.
     * @return the normalized input Object.
     */
    public Object normalize(Object obj) {
        if (obj instanceof String) {
            obj = normalize((String) obj);
        } else if (obj instanceof Collection<?>) {
            Collection<?> collection = (Collection<?>) (obj);

            // convert all strings in the collection to lower case
            CollectionUtils.transform(collection, new Transformer() {
                public Object transform(Object input) {
                    if (input instanceof Collection<?>) {
                        for (Object element : ((Collection<?>) input)) {
                            return transform(element);
                        }

                        // should be unreachable, but compiler error without it
                        return null;
                    } else if (input instanceof String) {
                        return normalize((String) input);
                    } else {
                        System.out.println("Found an element that's not an " + "instance of String");
                        return input;
                    }
                }
            });
        }

        return obj;
    }

    @Override
    public boolean add(String s) {
        return super.add((String) normalize(s));
    }

    @Override
    public boolean contains(Object o) {
        return super.contains(normalize(o));
    }

    @Override
    public boolean remove(Object o) {
        return super.remove(normalize(o));
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(normalize(o));
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return super.removeAll((Collection<?>) normalize(c));
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean addAll(Collection<? extends String> c) {
        return super.addAll((Collection<? extends String>) (normalize(c)));
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return super.containsAll((Collection<?>) normalize(c));
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return super.retainAll((Collection<?>) normalize(c));
    }
}