Example usage for com.google.common.collect Lists newArrayList

List of usage examples for com.google.common.collect Lists newArrayList

Introduction

In this page you can find the example usage for com.google.common.collect Lists newArrayList.

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayList() 

Source Link

Document

Creates a mutable, empty ArrayList instance (for Java 6 and earlier).

Usage

From source file:org.apache.mahout.math.OrthonormalityVerifier.java

public static VectorIterable pairwiseInnerProducts(Iterable<MatrixSlice> basis) {
    DenseMatrix out = null;/*w  w w .j  a v a 2s  . co  m*/
    for (MatrixSlice slice1 : basis) {
        List<Double> dots = Lists.newArrayList();
        for (MatrixSlice slice2 : basis) {
            dots.add(slice1.vector().dot(slice2.vector()));
        }
        if (out == null) {
            out = new DenseMatrix(dots.size(), dots.size());
        }
        for (int i = 0; i < dots.size(); i++) {
            out.set(slice1.index(), i, dots.get(i));
        }
    }
    return out;
}

From source file:com.yahoo.ycsb.CommandLine.java

public static void main(String[] args) {
    int argindex = 0;

    Properties props = new Properties();
    Properties fileprops = new Properties();
    String table = CoreWorkload.TABLENAME_PROPERTY_DEFAULT;

    while ((argindex < args.length) && (args[argindex].startsWith("-"))) {
        if ((args[argindex].compareTo("-help") == 0) || (args[argindex].compareTo("--help") == 0)
                || (args[argindex].compareTo("-?") == 0) || (args[argindex].compareTo("--?") == 0)) {
            usageMessage();//from   w  w w . ja  v a2  s  .c  o  m
            System.exit(0);
        }

        if (args[argindex].compareTo("-db") == 0) {
            argindex++;
            if (argindex >= args.length) {
                usageMessage();
                System.exit(0);
            }
            props.setProperty("db", args[argindex]);
            argindex++;
        } else if (args[argindex].compareTo("-P") == 0) {
            argindex++;
            if (argindex >= args.length) {
                usageMessage();
                System.exit(0);
            }
            String propfile = args[argindex];
            argindex++;

            Properties myfileprops = new Properties();
            try {
                myfileprops.load(new FileInputStream(propfile));
            } catch (IOException e) {
                System.out.println(e.getMessage());
                System.exit(0);
            }

            for (Enumeration e = myfileprops.propertyNames(); e.hasMoreElements();) {
                String prop = (String) e.nextElement();

                fileprops.setProperty(prop, myfileprops.getProperty(prop));
            }

        } else if (args[argindex].compareTo("-p") == 0) {
            argindex++;
            if (argindex >= args.length) {
                usageMessage();
                System.exit(0);
            }
            int eq = args[argindex].indexOf('=');
            if (eq < 0) {
                usageMessage();
                System.exit(0);
            }

            String name = args[argindex].substring(0, eq);
            String value = args[argindex].substring(eq + 1);
            props.put(name, value);
            //System.out.println("["+name+"]=["+value+"]");
            argindex++;
        } else if (args[argindex].compareTo("-table") == 0) {
            argindex++;
            if (argindex >= args.length) {
                usageMessage();
                System.exit(0);
            }
            table = args[argindex];
            argindex++;
        } else {
            System.out.println("Unknown option " + args[argindex]);
            usageMessage();
            System.exit(0);
        }

        if (argindex >= args.length) {
            break;
        }
    }

    if (argindex != args.length) {
        usageMessage();
        System.exit(0);
    }

    for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
        String prop = (String) e.nextElement();

        fileprops.setProperty(prop, props.getProperty(prop));
    }

    props = fileprops;

    System.out.println("YCSB Command Line client");
    System.out.println("Type \"help\" for command line help");
    System.out.println("Start with \"-help\" for usage info");

    //create a DB
    String dbname = props.getProperty("db", DEFAULT_DB);

    ClassLoader classLoader = CommandLine.class.getClassLoader();

    DB db = null;

    try {
        Class dbclass = classLoader.loadClass(dbname);
        db = (DB) dbclass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }

    db.setProperties(props);
    try {
        db.init();
    } catch (DBException e) {
        e.printStackTrace();
        System.exit(0);
    }

    System.out.println("Connected.");

    //main loop
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    for (;;) {
        //get user input
        System.out.print("> ");

        String input = null;

        try {
            input = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        if (input.compareTo("") == 0) {
            continue;
        }

        if (input.compareTo("help") == 0) {
            help();
            continue;
        }

        if (input.compareTo("quit") == 0) {
            break;
        }

        String[] tokens = input.split(" ");

        long st = System.currentTimeMillis();
        //handle commands
        if (tokens[0].compareTo("table") == 0) {
            if (tokens.length == 1) {
                System.out.println("Using table \"" + table + "\"");
            } else if (tokens.length == 2) {
                table = tokens[1];
                System.out.println("Using table \"" + table + "\"");
            } else {
                System.out.println("Error: syntax is \"table tablename\"");
            }
        } else if (tokens[0].compareTo("read") == 0) {
            if (tokens.length == 1) {
                System.out.println("Error: syntax is \"read keyname [field1 field2 ...]\"");
            } else {
                Set<String> fields = null;

                if (tokens.length > 2) {
                    fields = new HashSet<String>();

                    for (int i = 2; i < tokens.length; i++) {
                        fields.add(tokens[i]);
                    }
                }

                HashMap<String, String> result = new HashMap<String, String>();
                int ret = db.read(table, tokens[1], fields, result);
                System.out.println("Return code: " + ret);
                for (Map.Entry<String, String> ent : result.entrySet()) {
                    System.out.println(ent.getKey() + "=" + ent.getValue());
                }
            }
        } else if (tokens[0].compareTo("scan") == 0) {
            if (tokens.length < 3) {
                System.out.println("Error: syntax is \"scan keyname scanlength [field1 field2 ...]\"");
            } else {
                Set<String> fields = null;

                if (tokens.length > 3) {
                    fields = new HashSet<String>();

                    for (int i = 3; i < tokens.length; i++) {
                        fields.add(tokens[i]);
                    }
                }

                List<Map<String, String>> results = Lists.newArrayList();
                int ret = db.scan(table, tokens[1], Integer.parseInt(tokens[2]), fields, results);
                System.out.println("Return code: " + ret);
                int record = 0;
                if (results.size() == 0) {
                    System.out.println("0 records");
                } else {
                    System.out.println("--------------------------------");
                }
                for (Map<String, String> result : results) {
                    System.out.println("Record " + (record++));
                    for (Map.Entry<String, String> ent : result.entrySet()) {
                        System.out.println(ent.getKey() + "=" + ent.getValue());
                    }
                    System.out.println("--------------------------------");
                }
            }
        } else if (tokens[0].compareTo("update") == 0) {
            if (tokens.length < 3) {
                System.out.println("Error: syntax is \"update keyname name1=value1 [name2=value2 ...]\"");
            } else {
                HashMap<String, String> values = new HashMap<String, String>();

                for (int i = 2; i < tokens.length; i++) {
                    String[] nv = tokens[i].split("=");
                    values.put(nv[0], nv[1]);
                }

                int ret = db.update(table, tokens[1], values);
                System.out.println("Return code: " + ret);
            }
        } else if (tokens[0].compareTo("insert") == 0) {
            if (tokens.length < 3) {
                System.out.println("Error: syntax is \"insert keyname name1=value1 [name2=value2 ...]\"");
            } else {
                HashMap<String, String> values = new HashMap<String, String>();

                for (int i = 2; i < tokens.length; i++) {
                    String[] nv = tokens[i].split("=");
                    values.put(nv[0], nv[1]);
                }

                int ret = db.insert(table, tokens[1], values);
                System.out.println("Return code: " + ret);
            }
        } else if (tokens[0].compareTo("delete") == 0) {
            if (tokens.length != 2) {
                System.out.println("Error: syntax is \"delete keyname\"");
            } else {
                int ret = db.delete(table, tokens[1]);
                System.out.println("Return code: " + ret);
            }
        } else {
            System.out.println("Error: unknown command \"" + tokens[0] + "\"");
        }

        System.out.println((System.currentTimeMillis() - st) + " ms");

    }
}

From source file:com.kegare.bedrocklayer.client.config.BedrockConfigGui.java

private static List<IConfigElement> getConfigElements() {
    List<IConfigElement> list = Lists.newArrayList();
    Configuration config = BedrockLayerAPI.getConfig();

    for (String category : config.getCategoryNames()) {
        list.addAll(new ConfigElement(config.getCategory(category)).getChildElements());
    }/*from  ww  w .jav  a  2 s .co m*/

    return list;
}

From source file:com.eincs.decanter.container.simple.route.MethodExtractor.java

/**
 * /*from   w  w w.j a v  a 2  s.  c  om*/
 * @param serviceObj
 * @return
 * @throws RouteReflectException
 */
public static List<MethodRouteInfo> extract(Object serviceObj) throws RouteReflectException {
    List<MethodRouteInfo> result = Lists.newArrayList();
    for (Method method : serviceObj.getClass().getMethods()) {
        Route route = method.getAnnotation(Route.class);
        if (route != null) {
            result.add(MethodRouteInfo.create(method));
        }
    }
    return result;
}

From source file:com.netflix.governator.configuration.KeyParser.java

/**
 * Parse a key into parts/* w w  w  .jav a  2 s . c  o  m*/
 *
 * @param raw the key
 * @return parts
 */
public static List<ConfigurationKeyPart> parse(String raw, Map<String, String> contextOverrides) {
    List<ConfigurationKeyPart> parts = Lists.newArrayList();

    int caret = 0;
    for (;;) {
        int startIndex = raw.indexOf("${", caret);
        if (startIndex < 0) {
            break;
        }
        int endIndex = raw.indexOf("}", startIndex);
        if (endIndex < 0) {
            break;
        }

        if (startIndex > caret) {
            parts.add(new ConfigurationKeyPart(raw.substring(caret, startIndex), false));
        }
        startIndex += 2;
        if (startIndex < endIndex) {
            String name = raw.substring(startIndex, endIndex);
            if (contextOverrides != null && contextOverrides.containsKey(name)) {
                parts.add(new ConfigurationKeyPart(contextOverrides.get(name), false));
            } else {
                parts.add(new ConfigurationKeyPart(name, true));
            }
        }
        caret = endIndex + 1;
    }

    if (caret < raw.length()) {
        parts.add(new ConfigurationKeyPart(raw.substring(caret), false));
    }

    if (parts.size() == 0) {
        parts.add(new ConfigurationKeyPart("", false));
    }

    return parts;
}

From source file:cn.hxh.springside.mapper.BeanMapper.java

/**
 * Dozer?Collection.//from  w w w  .  j  a va  2 s.co m
 */
public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
    List<T> destinationList = Lists.newArrayList();
    for (Object sourceObject : sourceList) {
        T destinationObject = dozer.map(sourceObject, destinationClass);
        destinationList.add(destinationObject);
    }
    return destinationList;
}

From source file:org.zht.framework.util.RequestUtil.java

public static String getRequestHeaders(HttpServletRequest request) {
    Map<String, List<String>> headers = Maps.newHashMap();
    Enumeration<String> namesEnumeration = request.getHeaderNames();
    while (namesEnumeration != null && namesEnumeration.hasMoreElements()) {
        String name = namesEnumeration.nextElement();
        Enumeration<String> valueEnumeration = request.getHeaders(name);
        List<String> values = Lists.newArrayList();
        while (valueEnumeration.hasMoreElements()) {
            values.add(valueEnumeration.nextElement());
        }//from  w ww .  jav  a2  s .  com
        headers.put(name, values);
    }
    return JSON.toJSONString(headers);
}

From source file:com.wrmsr.nativity.util.Maps.java

public static <K, E> Map<K, List<E>> buildMapToList(Iterator<E> items, Function<E, K> fn) {
    Map<K, List<E>> map = newHashMap();
    while (items.hasNext()) {
        E item = items.next();// w w  w  . jav  a2 s .co m
        K key = fn.apply(item);
        List<E> list = map.get(key);
        if (list == null) {
            list = Lists.newArrayList();
            map.put(key, list);
        }
        list.add(item);
    }
    return map;
}

From source file:com.android.tools.idea.gradle.util.AndroidStudioPreferences.java

public static void cleanUpPreferences(@NotNull ExtensionPoint<ConfigurableEP<Configurable>> preferences,
        @NotNull List<String> bundlesToRemove) {
    List<ConfigurableEP<Configurable>> nonStudioExtensions = Lists.newArrayList();

    ConfigurableEP<Configurable>[] extensions = preferences.getExtensions();
    for (ConfigurableEP<Configurable> extension : extensions) {
        if (bundlesToRemove.contains(extension.instanceClass)) {
            nonStudioExtensions.add(extension);
        }/* ww w  . j  a v  a  2s. co m*/
    }

    for (ConfigurableEP<Configurable> toRemove : nonStudioExtensions) {
        preferences.unregisterExtension(toRemove);
    }
}

From source file:com.nesscomputing.mojo.numbers.PropertyField.java

public static List<PropertyElement> createProperties(final Map<String, String> props,
        final PropertyGroup propertyGroup) throws IOException {
    final List<PropertyElement> result = Lists.newArrayList();

    for (Iterator<String> it = propertyGroup.getPropertyNames(); it.hasNext();) {
        final String name = it.next();
        final String value = propertyGroup.getPropertyValue(name, props);
        result.add(new PropertyField(name, value));
    }//from w  w w.  ja  v a2  s.c o m
    return result;
}