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

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

Introduction

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

Prototype

public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) 

Source Link

Document

Adds all elements in iterator to collection .

Usage

From source file:org.apache.twill.internal.appmaster.RunningContainers.java

/**
 * Stops all running services. Only called when the AppMaster stops.
 *//*from www.  j av a  2s . co  m*/
void stopAll() {
    containerLock.lock();
    // Stop the runnables one by one in reverse order of start sequence
    List<String> reverseRunnables = new LinkedList<>();
    try {
        Iterators.addAll(reverseRunnables, startSequence.descendingIterator());
    } finally {
        containerLock.unlock();
    }

    List<ListenableFuture<Service.State>> futures = Lists.newLinkedList();
    for (String runnableName : reverseRunnables) {
        LOG.info("Stopping all instances of " + runnableName);

        futures.clear();
        // Parallel stops all running containers of the current runnable.
        containerLock.lock();
        try {
            for (TwillContainerController controller : containers.row(runnableName).values()) {
                futures.add(controller.stop());
            }
        } finally {
            containerLock.unlock();
        }
        // Wait for containers to stop. Assumes the future returned by Futures.successfulAsList won't throw exception.
        // This will block until handleCompleted() is run for the runnables or a timeout occurs.
        Futures.getUnchecked(Futures.successfulAsList(futures));

        LOG.info("Terminated all instances of " + runnableName);
    }

    // When we acquire this lock, all stopped runnables should have been cleaned up by handleCompleted() method
    containerLock.lock();
    try {
        for (Map.Entry<String, Map<String, TwillContainerController>> entry : containers.rowMap().entrySet()) {
            String runnableName = entry.getKey();
            Collection<ContainerInfo> containerInfos = containerStats.get(runnableName);
            for (Map.Entry<String, TwillContainerController> containerControllerEntry : entry.getValue()
                    .entrySet()) {
                for (ContainerInfo containerInfo : containerInfos) {
                    if (containerInfo.getId().equals(containerControllerEntry.getKey())) {
                        // Only call eventHandler.containerStopped if container is not removed by handleCompleted
                        eventHandler.containerStopped(runnableName,
                                containerControllerEntry.getValue().getInstanceId(),
                                containerControllerEntry.getKey(), ContainerExitCodes.ABORTED);
                        break;
                    }
                }
            }
        }
        containers.clear();
        runnableInstances.clear();
        numRetries.clear();
        containerStats.clear();
    } finally {
        containerLock.unlock();
    }
}

From source file:org.obeonetwork.dsl.uml2.design.api.services.ActivityDiagramServices.java

/**
 * Get all the operations available in the semantic resources.
 *
 * @param eObj//  www  . j av a 2  s . c om
 *            Semantic element
 * @return All the operations
 */
private List<EObject> getAllOperations(Element element) {
    final List<EObject> operations = Lists.newArrayList();
    final List<org.eclipse.uml2.uml.Package> rootPkgs = getAllAvailableRootPackages(element);
    final Predicate<EObject> predicate = new Predicate<EObject>() {
        public boolean apply(EObject eObj) {
            return eObj instanceof Operation
                    && (((Operation) eObj).getMethods() == null || ((Operation) eObj).getMethods().size() == 0);
        }
    };
    for (final org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(operations, Iterators.filter(pkg.eAllContents(), predicate));
    }

    return operations;
}

From source file:com.analog.lyric.dimple.model.transform.VariableEliminator.java

/**
 * Computes a variable elimination order by iteratively retrying using one or more cost functions
 * and choosing the best fit according to the specified threshold statistics.
 * <p>//from   w  w w .j ava2s  .c om
 * Same as {@link #generate(FactorGraph, boolean, int, Stats, CostFunction...)} but uses provided
 * eliminator instead of building a new one.
 */
public static Ordering generate(VariableEliminator eliminator, int nAttempts, Stats threshold,
        CostFunction... costFunctions) {
    final boolean deterministic = nAttempts <= 0;

    if (costFunctions.length == 0) {
        costFunctions = VariableCost.toFunctions(VariableCost.values());
    }
    final int nFunctions = costFunctions.length;

    // Cumulative distribution function for choosing cost function. Initially
    // set to uniform weights.
    final double[] functionCDF = new double[nFunctions];
    {
        final double increment = 1.0 / nFunctions;
        double cumProb = increment;
        for (int i = 0; i < nFunctions; ++i) {
            functionCDF[i] = cumProb;
            cumProb += increment;
        }
    }

    final long[] timePerFunction = new long[nFunctions];
    long totalTime = 0;

    if (deterministic) {
        nAttempts = nFunctions;
    }

    ArrayList<Variable> curList = new ArrayList<Variable>(eliminator._nVariables);
    ArrayList<Variable> bestList = new ArrayList<Variable>(eliminator._nVariables);
    Stats bestStats = null;

    Random rand = eliminator.getRandomizer();
    if (rand == null) {
        rand = new Random();
    }

    for (int attempt = 0; attempt < nAttempts; ++attempt) {
        // Pick a cost function
        int costIndex = 0;
        if (nFunctions > 1) {
            if (deterministic) {
                costIndex = attempt;
            } else {
                costIndex = Arrays.binarySearch(functionCDF, rand.nextDouble());
                if (costIndex < 0) {
                    costIndex = -costIndex - 1;
                }
                costIndex = Math.min(costIndex, nFunctions - 1);
            }
        }

        CostFunction cost = costFunctions[costIndex];

        // Run variable elimination
        final long beforeNS = System.nanoTime();
        OrderIterator iterator = eliminator.orderIterator(cost);
        Iterators.addAll(curList, iterator);
        final long elapsedNS = System.nanoTime() - beforeNS;
        timePerFunction[costIndex] += elapsedNS;
        totalTime += elapsedNS;

        // Compare stats
        Stats curStats = iterator.getStats();

        if (curStats.addedEdges() == 0) {
            bestStats = curStats;
            bestList = curList;
            break;
        }

        if (bestStats == null || curStats.compareTo(bestStats, threshold) < 0) {
            ArrayList<Variable> tmp = curList;
            curList = bestList;
            curList.clear();
            bestList = tmp;
            bestStats = curStats;

            if (bestStats.meetsThreshold(threshold)) {
                break;
            }
        }

        // Update functionCDF based on timings to favor cheaper cost function.
        // TODO: give bonus weight to functions that improved the stats.
        if (nFunctions > 1 && !deterministic) {
            final double normalizer = (double) totalTime * (nFunctions - 1);
            double cumProb = 0.0;
            for (int i = 0; i < nFunctions; ++i) {
                functionCDF[i] = cumProb += (totalTime - timePerFunction[i]) / normalizer;
            }
        }
    }

    if (bestStats == null) {
        bestStats = new Stats(null, 0);
    }

    return new Ordering(bestList, bestStats);
}

From source file:org.obeonetwork.dsl.uml2.core.api.services.ActivityDiagramServices.java

/**
 * Get all the behaviors available in the semantic resources.
 *
 * @param eObj/*from   w  w  w .j av a2s  . c o  m*/
 *            Semantic element
 * @return All the behaviors
 */
private List<EObject> getAllBehaviors(Element element) {
    final List<EObject> behaviors = Lists.newArrayList();
    final List<org.eclipse.uml2.uml.Package> rootPkgs = getAllAvailableRootPackages(element);
    for (final org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(behaviors,
                Iterators.filter(pkg.eAllContents(), Predicates.instanceOf(Behavior.class)));
    }

    return behaviors;
}

From source file:com.koloboke.compile.KolobokeMapBackedMultiset.java

@Override
public boolean addAll(Collection<? extends E> c) {
    if (c.isEmpty()) {
        return false;
    }/*from  ww  w .  j a  v  a 2  s.c o  m*/
    if (c instanceof Multiset) {
        Multiset<? extends E> that = cast(c);
        for (Entry<? extends E> entry : that.entrySet()) {
            this.add(entry.getElement(), entry.getCount());
        }
    } else {
        Iterators.addAll(this, c.iterator());
    }
    return true;
}

From source file:org.eclipse.wb.gef.core.EditPart.java

protected void refreshChildren() {
    // prepare map[model, currentPart]
    Map<Object, EditPart> modelToPart = Maps.newHashMap();
    List<EditPart> children = getChildren();
    for (EditPart editPart : children) {
        modelToPart.put(editPart.getModel(), editPart);
    }/* w w  w .j  a  v  a2  s. c om*/
    // add new child EditPart's or reorder current EditPart's
    List<?> modelChildren = getModelChildren();
    int modelCount = modelChildren.size();
    int partCount = children.size();
    int index = 0;
    for (int modelIndex = 0; modelIndex < modelCount; index++, modelIndex++) {
        Object model = modelChildren.get(modelIndex);
        // do a quick check to see if editPart[i] == model[i]
        if (index < partCount) {
            EditPart part = children.get(index);
            if (part.getModel() == model) {
                updateChildVisual(part, index);
                continue;
            }
        }
        //
        EditPart childPart = modelToPart.get(model);
        if (childPart == null) {
            EditPart createEditPart = createEditPart(model);
            if (createEditPart != null) {
                addChild(createEditPart, index);
            } else {
                index--;
            }
        } else {
            if (model != childPart.getModel()) {
                getViewer().unregisterEditPart(childPart);
                childPart.setModel(model);
                getViewer().registerEditPart(childPart);
                childPart.updateModel();
            }
            // reorder child EditPart
            removeChildVisual(childPart);
            children.remove(childPart);
            children.add(index, childPart);
            addChildVisual(childPart, index);
        }
    }
    //
    int newPartCount = children.size();
    if (newPartCount - index > 1) {
        // deselect old child EditPart's
        List<EditPart> deselectList = Lists.newArrayList();
        Iterators.addAll(deselectList, children.listIterator(index));
        getViewer().deselect(deselectList);
    }
    // remove old child EditPart's
    for (int i = index; i < newPartCount; i++) {
        EditPart childPart = children.get(index);
        removeChild(childPart);
    }
    // recurse refresh()
    for (EditPart child : getChildren()) {
        child.refresh();
    }
}

From source file:org.obeonetwork.dsl.uml2.core.api.services.ActivityDiagramServices.java

/**
 * Get all the operations available in the semantic resources.
 *
 * @param eObj//from   w  ww.  j  a v  a 2 s  .com
 *            Semantic element
 * @return All the operations
 */
private List<EObject> getAllOperations(Element element) {
    final List<EObject> operations = Lists.newArrayList();
    final List<org.eclipse.uml2.uml.Package> rootPkgs = getAllAvailableRootPackages(element);
    final Predicate<EObject> predicate = new Predicate<EObject>() {
        @Override
        public boolean apply(EObject eObj) {
            return eObj instanceof Operation
                    && (((Operation) eObj).getMethods() == null || ((Operation) eObj).getMethods().size() == 0);
        }
    };
    for (final org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(operations, Iterators.filter(pkg.eAllContents(), predicate));
    }

    return operations;
}

From source file:org.obeonetwork.dsl.uml2.core.api.services.ActivityDiagramServices.java

/**
 * Get all the signals available in the semantic resources.
 *
 * @param element//  w ww .  j a v  a  2 s .co m
 *            Semantic element
 * @return All the signals
 */
public List<EObject> getAllSignals(Element element) {
    final List<EObject> signals = Lists.newArrayList();
    final List<org.eclipse.uml2.uml.Package> rootPkgs = getAllAvailableRootPackages(element);
    for (final org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(signals, Iterators.filter(pkg.eAllContents(), Predicates.instanceOf(Signal.class)));
    }

    return signals;
}

From source file:io.prestosql.plugin.hive.BackgroundHiveSplitLoader.java

private List<InternalHiveSplit> getBucketedSplits(Path path, FileSystem fileSystem,
        InternalHiveSplitFactory splitFactory, BucketSplitInfo bucketSplitInfo,
        Optional<BucketConversion> bucketConversion) {
    int readBucketCount = bucketSplitInfo.getReadBucketCount();
    int tableBucketCount = bucketSplitInfo.getTableBucketCount();
    int partitionBucketCount = bucketConversion.isPresent() ? bucketConversion.get().getPartitionBucketCount()
            : tableBucketCount;//  w w  w  .  j a  va 2s  .  co m

    // list all files in the partition
    ArrayList<LocatedFileStatus> files = new ArrayList<>(partitionBucketCount);
    try {
        Iterators.addAll(files, new HiveFileIterator(path, fileSystem, directoryLister, namenodeStats, FAIL));
    } catch (NestedDirectoryNotAllowedException e) {
        // Fail here to be on the safe side. This seems to be the same as what Hive does
        throw new PrestoException(HIVE_INVALID_BUCKET_FILES,
                format("Hive table '%s' is corrupt. Found sub-directory in bucket directory for partition: %s",
                        new SchemaTableName(table.getDatabaseName(), table.getTableName()),
                        splitFactory.getPartitionName()));
    }

    // verify we found one file per bucket
    if (files.size() != partitionBucketCount) {
        throw new PrestoException(HIVE_INVALID_BUCKET_FILES, format(
                "Hive table '%s' is corrupt. The number of files in the directory (%s) does not match the declared bucket count (%s) for partition: %s",
                new SchemaTableName(table.getDatabaseName(), table.getTableName()), files.size(),
                partitionBucketCount, splitFactory.getPartitionName()));
    }

    // Sort FileStatus objects (instead of, e.g., fileStatus.getPath().toString). This matches org.apache.hadoop.hive.ql.metadata.Table.getSortedPaths
    files.sort(null);

    // convert files internal splits
    List<InternalHiveSplit> splitList = new ArrayList<>();
    for (int bucketNumber = 0; bucketNumber < Math.max(readBucketCount, partitionBucketCount); bucketNumber++) {
        // Physical bucket #. This determine file name. It also determines the order of splits in the result.
        int partitionBucketNumber = bucketNumber % partitionBucketCount;
        // Logical bucket #. Each logical bucket corresponds to a "bucket" from engine's perspective.
        int readBucketNumber = bucketNumber % readBucketCount;

        boolean containsEligibleTableBucket = false;
        boolean containsIneligibleTableBucket = false;
        for (int tableBucketNumber = bucketNumber
                % tableBucketCount; tableBucketNumber < tableBucketCount; tableBucketNumber += Math
                        .max(readBucketCount, partitionBucketCount)) {
            // table bucket number: this is used for evaluating "$bucket" filters.
            if (bucketSplitInfo.isTableBucketEnabled(tableBucketNumber)) {
                containsEligibleTableBucket = true;
            } else {
                containsIneligibleTableBucket = true;
            }
        }

        if (containsEligibleTableBucket && containsIneligibleTableBucket) {
            throw new PrestoException(NOT_SUPPORTED,
                    "The bucket filter cannot be satisfied. There are restrictions on the bucket filter when all the following is true: "
                            + "1. a table has a different buckets count as at least one of its partitions that is read in this query; "
                            + "2. the table has a different but compatible bucket number with another table in the query; "
                            + "3. some buckets of the table is filtered out from the query, most likely using a filter on \"$bucket\". "
                            + "(table name: " + table.getTableName() + ", table bucket count: "
                            + tableBucketCount + ", " + "partition bucket count: " + partitionBucketCount
                            + ", effective reading bucket count: " + readBucketCount + ")");
        }
        if (containsEligibleTableBucket) {
            LocatedFileStatus file = files.get(partitionBucketNumber);
            splitFactory.createInternalHiveSplit(file, readBucketNumber).ifPresent(splitList::add);
        }
    }
    return splitList;
}

From source file:org.obeonetwork.dsl.uml2.design.services.ActivityServices.java

/**
 * Get all the operations available in the semantic resources.
 * /* w w w  .j  a v  a2s.  c o m*/
 * @param eObj
 *            Semantic element
 * @return All the operations
 */
public List<EObject> getAllOperations(Element element) {
    List<EObject> operations = Lists.newArrayList();
    UMLServices umlServices = new UMLServices();
    List<org.eclipse.uml2.uml.Package> rootPkgs = umlServices.getAllAvailableRootPackages(element);
    final Predicate<EObject> predicate = new Predicate<EObject>() {
        public boolean apply(EObject eObj) {
            return eObj instanceof Operation
                    && (((Operation) eObj).getMethods() == null || ((Operation) eObj).getMethods().size() == 0);
        }
    };
    for (org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(operations, Iterators.filter(pkg.eAllContents(), predicate));
    }

    return operations;
}