Example usage for java.util Collections singleton

List of usage examples for java.util Collections singleton

Introduction

In this page you can find the example usage for java.util Collections singleton.

Prototype

public static <T> Set<T> singleton(T o) 

Source Link

Document

Returns an immutable set containing only the specified object.

Usage

From source file:com.tamingtext.tagging.LuceneTagExtractor.java

public static void main(String[] args) throws IOException {
    DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
    ArgumentBuilder abuilder = new ArgumentBuilder();
    GroupBuilder gbuilder = new GroupBuilder();

    Option inputOpt = obuilder.withLongName("dir").withRequired(true)
            .withArgument(abuilder.withName("dir").withMinimum(1).withMaximum(1).create())
            .withDescription("The Lucene directory").withShortName("d").create();

    Option outputOpt = obuilder.withLongName("output").withRequired(false)
            .withArgument(abuilder.withName("output").withMinimum(1).withMaximum(1).create())
            .withDescription("The output directory").withShortName("o").create();

    Option maxOpt = obuilder.withLongName("max").withRequired(false)
            .withArgument(abuilder.withName("max").withMinimum(1).withMaximum(1).create())
            .withDescription(/* w  ww  .j a v  a 2 s. c  o m*/
                    "The maximum number of vectors to output.  If not specified, then it will loop over all docs")
            .withShortName("m").create();

    Option fieldOpt = obuilder.withLongName("field").withRequired(true)
            .withArgument(abuilder.withName("field").withMinimum(1).withMaximum(1).create())
            .withDescription("The field in the index").withShortName("f").create();

    Option helpOpt = obuilder.withLongName("help").withDescription("Print out help").withShortName("h")
            .create();

    Group group = gbuilder.withName("Options").withOption(inputOpt).withOption(outputOpt).withOption(maxOpt)
            .withOption(fieldOpt).create();

    try {
        Parser parser = new Parser();
        parser.setGroup(group);
        CommandLine cmdLine = parser.parse(args);

        if (cmdLine.hasOption(helpOpt)) {
            CommandLineUtil.printHelp(group);
            return;
        }

        File file = new File(cmdLine.getValue(inputOpt).toString());

        if (!file.isDirectory()) {
            throw new IllegalArgumentException(file + " does not exist or is not a directory");
        }

        long maxDocs = Long.MAX_VALUE;
        if (cmdLine.hasOption(maxOpt)) {
            maxDocs = Long.parseLong(cmdLine.getValue(maxOpt).toString());
        }

        if (maxDocs < 0) {
            throw new IllegalArgumentException("maxDocs must be >= 0");
        }

        String field = cmdLine.getValue(fieldOpt).toString();

        PrintWriter out = null;
        if (cmdLine.hasOption(outputOpt)) {
            out = new PrintWriter(new FileWriter(cmdLine.getValue(outputOpt).toString()));
        } else {
            out = new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"));
        }

        File output = new File("/home/drew/taming-text/delicious/training");
        output.mkdirs();

        emitTextForTags(file, output);

        IOUtils.close(Collections.singleton(out));
    } catch (OptionException e) {
        log.error("Exception", e);
        CommandLineUtil.printHelp(group);
    }

}

From source file:Main.java

public static <K> Set<K> createSingletonSet(K item) {
    return Collections.singleton(item);
}

From source file:Main.java

public static Set<Object> singletonSet(Object object) {
    return Collections.singleton(object);
}

From source file:Main.java

/**
 * Removes null values from the given map
 *///from   w  w w  .j  ava2 s . co  m
public static void removeNullValues(Map<String, ?> map) {
    if (map == null)
        return;
    map.values().removeAll(Collections.singleton(null));
}

From source file:Main.java

public static void filterNull(Collection<?> collection) {
    if (isNotEmpty(collection)) {
        collection.removeAll(Collections.singleton(null));
    }/*w ww. j av  a 2 s. c o m*/
}

From source file:Main.java

public static <A> Set<A> makeSet(Collection<A> xs) {
    if (xs.size() == 0)
        return Collections.<A>emptySet();
    else if (xs.size() == 1)
        return Collections.singleton(xs.iterator().next());
    else {//from   w  w  w .ja  v  a 2  s  . c  o  m
        Set<A> set = new HashSet<A>(xs.size());
        set.addAll(xs);
        return set;
    }
}

From source file:Main.java

public static <T> Set<T> toImmutableSet(Set<T> set) {
    switch (set.size()) {
    case 0:/* ww w .  j  a  v a  2s .  co m*/
        return Collections.emptySet();
    case 1:
        return Collections.singleton(set.iterator().next());
    default:
        return Collections.unmodifiableSet(set);
    }
}

From source file:Main.java

/**
 * Convert a String to a unmodifiable set of characters.
 * @param str The string to convert/*from  w w  w .  j  a va2  s  .co  m*/
 * @return A set containing the characters in str. A empty set
 *    is returned if str is null.
 */
public static Set<Character> strToUnmodifiableSet(String str) {
    if (str == null)
        return Collections.emptySet();
    if (str.length() == 1)
        return Collections.singleton(str.charAt(0));
    return Collections.unmodifiableSet(strToSet(str));
}

From source file:Main.java

/**
 * Creates an unmodifiable set filled with the given values.
 * //from ww w.j  a v a2  s.  c  o  m
 * @param <T>
 *            the set's element type.
 * @param values
 *            the values.
 * @return a newly created set containing the given values.
 */
@SafeVarargs
public static <T> Set<T> createUnmodifiableSet(final T... values) {
    switch (values.length) {
    case 0:
        return Collections.emptySet();
    case 1:
        return Collections.singleton(values[0]);
    default:
        return Collections.unmodifiableSet(createHashSet(values));
    }
}

From source file:Main.java

@SafeVarargs
public static <A> Set<A> makeSet(A... xs) {
    if (xs.length == 0)
        return Collections.<A>emptySet();
    else if (xs.length == 1)
        return Collections.singleton(xs[0]);
    else {//www. ja v a  2  s.  c o  m
        Set<A> set = new HashSet<A>(xs.length);
        Collections.addAll(set, xs);
        return set;
    }
}