Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 Matthew Crocco * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ package com.mattc.launcher.util; import java.io.Closeable; import java.io.IOException; import java.util.Collection; import java.util.Iterator; import java.util.List; import com.google.common.base.CharMatcher; import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.collect.Iterators; public final class Utility { /** Splitter for Splitting by Whitespace. */ private static final Splitter wsSplitter = Splitter.on(CharMatcher.WHITESPACE).trimResults().omitEmptyStrings(); /** * Get Index of an Object in a Collection. * * @param list * @param item * @return */ public static int getIndexOf(Collection<?> list, Object item) { final Iterator<?> iter = list.iterator(); return Iterators.indexOf(iter, Predicates.equalTo(item)); } /** * Silently Close a Stream/Writer/Reader. If it is null or an exception is * thrown it is silenced. * * @param closeable */ public static void closeSilently(Closeable closeable) { if (closeable == null) return; try { closeable.close(); } catch (final IOException e) { } } /** * Turn an Iterable of Strings into a single string. * * @param iter * @return */ public static String stringify(Iterable<String> iter, boolean spaces) { final StringBuilder sb = new StringBuilder(); for (final String s : iter) { sb.append(s.trim() + (spaces ? " " : "")); } return sb.toString(); } /** * Turn an Iterable of Strings into a single string. * * @param iter * @return */ public static String stringify(Iterable<String> iter) { return stringify(iter, true); } /** * Return a List of Strings from a single String. The List is created by * splitting the given String at every whitespace. * * @param from * @return */ public static List<String> splitAtWhitespace(String from) { return wsSplitter.splitToList(from); } /** * Returns an Iterable of Strings from a single String. The Iterable is * created by splitting the given String at every whitespace. * * @param from * @return */ public static Iterable<String> splitAtWhitespaceIterable(String from) { return wsSplitter.split(from); } }