Example usage for com.google.common.collect Iterators forArray

List of usage examples for com.google.common.collect Iterators forArray

Introduction

In this page you can find the example usage for com.google.common.collect Iterators forArray.

Prototype

public static <T> UnmodifiableIterator<T> forArray(final T... array) 

Source Link

Document

Returns an iterator containing the elements of array in order.

Usage

From source file:com.yandex.yoctodb.query.simple.SortingScoredDocumentIterator.java

private void fillChunk() {
    assert !chunk.hasNext();

    final IntToIntArray taggedDocuments = baseIterator.next();
    final int firstValueIndex = taggedDocuments.getKey();
    final int[] ids = taggedDocuments.getValues();
    final int count = taggedDocuments.getCount();

    assert count > 0;

    final LocalScoredDocument[] docs = new LocalScoredDocument[count];

    for (int i = 0; i < count; i++) {
        final int id = ids[i];
        final int[] sortValueIndexes = new int[indexes.length];
        sortValueIndexes[0] = firstValueIndex;
        for (int s = 1; s < sortValueIndexes.length; s++)
            sortValueIndexes[s] = withOrder(orders[s], indexes[s].getSortValueIndex(id));

        docs[i] = new LocalScoredDocument(id, new LocalScore(sortValueIndexes));
    }//from   w  w  w .j  av a  2s. c om

    // Special case for good index selectivity
    if (docs.length == 1) {
        chunk = Iterators.singletonIterator(docs[0]);
    } else {
        Arrays.sort(docs);
        chunk = Iterators.forArray(docs);
    }
}

From source file:de.griffel.confluence.plugins.plantuml.PlantUmlMacroParams.java

public DiagramType getDiagramType() {
    /* final */DiagramType result;
    try {/*from   w  w  w  .  jav  a 2 s  .  c o m*/
        final String type = get(Param.type);
        result = Iterators.find(Iterators.forArray(DiagramType.values()), new Predicate<DiagramType>() {
            public boolean apply(DiagramType diagramType) {
                return diagramType.name().equalsIgnoreCase(type);
            }
        });
    } catch (NoSuchElementException e) {
        result = DiagramType.UML; /* default type */
    }
    return result;
}

From source file:org.apache.drill.exec.compile.sig.SignatureHolder.java

@Override
public Iterator<CodeGeneratorMethod> iterator() {
    return Iterators.forArray(methods);
}

From source file:com.android.ide.common.resources.configuration.FolderConfiguration.java

/**
 * Creates a {@link FolderConfiguration} matching the folder segments.
 * @param folderSegments The segments of the folder name. The first segments should contain
 * the name of the folder/*  w w  w  .j av  a  2 s.  c o m*/
 * @return a FolderConfiguration object, or null if the folder name isn't valid..
 */
@Nullable
public static FolderConfiguration getConfig(@NonNull String[] folderSegments) {
    Iterator<String> iterator = Iterators.forArray(folderSegments);
    if (iterator.hasNext()) {
        // Skip the first segment: it should be just the base folder, such as "values" or
        // "layout"
        iterator.next();
    }

    return getConfigFromQualifiers(iterator);
}

From source file:org.apache.mahout.common.iterator.sequencefile.SequenceFileDirIterator.java

private void init(FileStatus[] statuses, final boolean reuseKeyValueInstances, final Configuration conf) {

    /*/*from ww w  .j av a  2s  . com*/
     * prevent NPEs. Unfortunately, Hadoop would return null for list if nothing
     * was qualified. In this case, which is a corner case, we should assume an
     * empty iterator, not an NPE.
     */
    if (statuses == null) {
        statuses = NO_STATUSES;
    }

    Iterator<FileStatus> fileStatusIterator = Iterators.forArray(statuses);

    Iterator<Iterator<Pair<K, V>>> fsIterators = Iterators.transform(fileStatusIterator,
            new Function<FileStatus, Iterator<Pair<K, V>>>() {
                @Override
                public Iterator<Pair<K, V>> apply(FileStatus from) {
                    try {
                        SequenceFileIterator<K, V> iterator = new SequenceFileIterator<K, V>(from.getPath(),
                                reuseKeyValueInstances, conf);
                        iterators.add(iterator);
                        return iterator;
                    } catch (IOException ioe) {
                        throw new IllegalStateException(from.getPath().toString(), ioe);
                    }
                }
            });

    Collections.reverse(iterators); // close later in reverse order

    delegate = Iterators.concat(fsIterators);
}

From source file:com.linecorp.armeria.common.MediaTypeSet.java

@Override
public Iterator<MediaType> iterator() {
    return Iterators.forArray(mediaTypes);
}

From source file:com.google.caliper.Arguments.java

public static Arguments parse(String[] argsArray) {
    Arguments result = new Arguments();

    Iterator<String> args = Iterators.forArray(argsArray);
    String delimiter = defaultDelimiter;
    Map<String, String> userParameterStrings = Maps.newLinkedHashMap();
    Map<String, String> vmParameterStrings = Maps.newLinkedHashMap();
    String vmString = null;//from  w  ww. j a  va  2s. c o  m
    boolean standardRun = false;
    while (args.hasNext()) {
        String arg = args.next();

        if ("--help".equals(arg)) {
            throw new DisplayUsageException();
        }

        if (arg.startsWith("-D") || arg.startsWith("-J")) {

            /*
             * Handle user parameters (-D) and VM parameters (-J) of these forms:
             *
             * -Dlength=100
             * -Jmemory=-Xmx1024M
             * -Dlength=100,200
             * -Jmemory=-Xmx1024M,-Xmx2048M
             * -Dlength 100
             * -Jmemory -Xmx1024M
             * -Dlength 100,200
             * -Jmemory -Xmx1024M,-Xmx2048M
             */

            String name;
            String value;
            int equalsSign = arg.indexOf('=');
            if (equalsSign == -1) {
                name = arg.substring(2);
                value = args.next();
            } else {
                name = arg.substring(2, equalsSign);
                value = arg.substring(equalsSign + 1);
            }

            String previousValue;
            if (arg.startsWith("-D")) {
                previousValue = userParameterStrings.put(name, value);
            } else {
                previousValue = vmParameterStrings.put(name, value);
            }
            if (previousValue != null) {
                throw new UserException.DuplicateParameterException(arg);
            }
            standardRun = true;
            // TODO: move warmup/run to caliperrc
        } else if ("--captureVmLog".equals(arg)) {
            result.captureVmLog = true;
            standardRun = true;
        } else if ("--warmupMillis".equals(arg)) {
            result.warmupMillis = Long.parseLong(args.next());
            standardRun = true;
        } else if ("--runMillis".equals(arg)) {
            result.runMillis = Long.parseLong(args.next());
            standardRun = true;
        } else if ("--trials".equals(arg)) {
            String value = args.next();
            try {
                result.trials = Integer.parseInt(value);
                if (result.trials < 1) {
                    throw new UserException.InvalidTrialsException(value);
                }
            } catch (NumberFormatException e) {
                throw new UserException.InvalidTrialsException(value);
            }
            standardRun = true;
        } else if ("--vm".equals(arg)) {
            if (vmString != null) {
                throw new UserException.DuplicateParameterException(arg);
            }
            vmString = args.next();
            standardRun = true;
        } else if ("--delimiter".equals(arg)) {
            delimiter = args.next();
            standardRun = true;
        } else if ("--timeUnit".equals(arg)) {
            result.timeUnit = args.next();
            standardRun = true;
        } else if ("--instanceUnit".equals(arg)) {
            result.instanceUnit = args.next();
            standardRun = true;
        } else if ("--memoryUnit".equals(arg)) {
            result.memoryUnit = args.next();
            standardRun = true;
        } else if ("--saveResults".equals(arg) || "--xmlSave".equals(arg)) {
            // TODO: unsupport legacy --xmlSave
            result.saveResultsFile = new File(args.next());
            standardRun = true;
        } else if ("--uploadResults".equals(arg)) {
            result.uploadResultsFile = new File(args.next());
        } else if ("--printScore".equals(arg)) {
            result.printScore = true;
            standardRun = true;
        } else if ("--measureMemory".equals(arg)) {
            result.measureMemory = true;
            standardRun = true;
        } else if ("--debug".equals(arg)) {
            result.debug = true;
        } else if ("--debug-reps".equals(arg)) {
            String value = args.next();
            try {
                result.debugReps = Integer.parseInt(value);
                if (result.debugReps < 1) {
                    throw new UserException.InvalidDebugRepsException(value);
                }
            } catch (NumberFormatException e) {
                throw new UserException.InvalidDebugRepsException(value);
            }
        } else if ("--marker".equals(arg)) {
            result.marker = args.next();
        } else if ("--measurementType".equals(arg)) {
            String measurementType = args.next();
            try {
                result.measurementType = MeasurementType.valueOf(measurementType);
            } catch (Exception e) {
                throw new InvalidParameterValueException(arg, measurementType);
            }
            standardRun = true;
        } else if ("--primaryMeasurementType".equals(arg)) {
            String measurementType = args.next().toUpperCase();
            try {
                result.primaryMeasurementType = MeasurementType.valueOf(measurementType);
            } catch (Exception e) {
                throw new InvalidParameterValueException(arg, measurementType);
            }
            standardRun = true;
        } else if (arg.startsWith("-")) {
            throw new UnrecognizedOptionException(arg);

        } else {
            if (result.suiteClassName != null) {
                throw new MultipleBenchmarkClassesException(result.suiteClassName, arg);
            }
            result.suiteClassName = arg;
        }
    }

    Splitter delimiterSplitter = Splitter.on(delimiter);

    if (vmString != null) {
        Iterables.addAll(result.userVms, delimiterSplitter.split(vmString));
    }

    Set<String> duplicates = Sets.intersection(userParameterStrings.keySet(), vmParameterStrings.keySet());
    if (!duplicates.isEmpty()) {
        throw new UserException.DuplicateParameterException(duplicates);
    }

    for (Map.Entry<String, String> entry : userParameterStrings.entrySet()) {
        result.userParameters.putAll(entry.getKey(), delimiterSplitter.split(entry.getValue()));
    }
    for (Map.Entry<String, String> entry : vmParameterStrings.entrySet()) {
        result.vmParameters.putAll(entry.getKey(), delimiterSplitter.split(entry.getValue()));
    }

    if (standardRun && result.uploadResultsFile != null) {
        throw new IncompatibleArgumentsException("--uploadResults");
    }

    if (result.suiteClassName == null && result.uploadResultsFile == null) {
        throw new NoBenchmarkClassException();
    }

    if (result.primaryMeasurementType != null && result.primaryMeasurementType != MeasurementType.TIME
            && !result.measureMemory) {
        throw new IncompatibleArgumentsException(
                "--primaryMeasurementType " + result.primaryMeasurementType.toString().toLowerCase());
    }

    return result;
}

From source file:org.apache.mahout.common.iterator.sequencefile.SequenceFileDirValueIterator.java

private void init(FileStatus[] statuses, Comparator<FileStatus> ordering, final boolean reuseKeyValueInstances,
        final Configuration conf) throws IOException {

    /*/*from  ww  w .  j  a va 2  s . c  om*/
     * prevent NPEs. Unfortunately, Hadoop would return null for list if nothing
     * was qualified. In this case, which is a corner case, we should assume an
     * empty iterator, not an NPE.
     */
    if (statuses == null) {
        statuses = NO_STATUSES;
    }

    if (ordering != null) {
        Arrays.sort(statuses, ordering);
    }
    Iterator<FileStatus> fileStatusIterator = Iterators.forArray(statuses);

    try {

        Iterator<Iterator<V>> fsIterators = Iterators.transform(fileStatusIterator,
                new Function<FileStatus, Iterator<V>>() {
                    @Override
                    public Iterator<V> apply(FileStatus from) {
                        try {
                            SequenceFileValueIterator<V> iterator = new SequenceFileValueIterator<V>(
                                    from.getPath(), reuseKeyValueInstances, conf);
                            iterators.add(iterator);
                            return iterator;
                        } catch (IOException ioe) {
                            throw new IllegalStateException(from.getPath().toString(), ioe);
                        }
                    }
                });

        Collections.reverse(iterators); // close later in reverse order

        delegate = Iterators.concat(fsIterators);

    } finally {
        /*
         * prevent file handle leaks in case one of handles fails to open. If some
         * of the files fail to open, constructor will fail and close() will never
         * be called. Thus, those handles that did open in constructor, would leak
         * out, unless we specifically handle it here.
         */
        IOUtils.close(iterators);
    }
}

From source file:com.inductiveautomation.xopc.drivers.modbus2.requests.optimizer.constraints.ConstraintProcessor.java

public List<List<T>> process(List<T> items, Constraint<T>... constraints) {
    List<List<T>> processed = new ArrayList<List<T>>();

    if (constraints.length == 0) {
        processed.add(items);//from  w w w .ja  va 2s . com
        return processed;
    }

    Iterator<Constraint<T>> iter = Iterators.forArray(constraints);

    Constraint<T> first = iter.next();
    processed = first.constrain(items);

    while (iter.hasNext()) {
        List<List<T>> constrained = new ArrayList<List<T>>();
        Constraint<T> constraint = iter.next();

        for (List<T> toConstrain : processed) {
            constrained.addAll(constraint.constrain(toConstrain));
        }

        processed = constrained;
    }

    return processed;
}

From source file:org.elasticsearch.search.internal.InternalSearchHits.java

@Override
public Iterator<SearchHit> iterator() {
    return Iterators.forArray(hits());
}