Example usage for com.google.common.collect ImmutableMultiset copyOf

List of usage examples for com.google.common.collect ImmutableMultiset copyOf

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMultiset copyOf.

Prototype

public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements) 

Source Link

Document

Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.

Usage

From source file:cpw.mods.inventorysorter.SortingHandler.java

private void distributeInventory(Action.ActionContext context, Multiset<ItemStackHolder> itemcounts) {
    InventoryCrafting ic = (InventoryCrafting) context.slot.inventory;
    Multiset<ItemStackHolder> slotCounts = TreeMultiset.create(new InventoryHandler.ItemStackComparator());
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                slotCounts.add(new ItemStackHolder(is));
            }/*from   ww  w. j  a  v  a  2 s  .c o  m*/
        }
    }

    final ImmutableMultiset<ItemStackHolder> staticcounts = ImmutableMultiset.copyOf(itemcounts);
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                ItemStackHolder ish = new ItemStackHolder(is);
                int count = staticcounts.count(ish);
                int slotNum = slotCounts.count(ish);
                final int occurrences = count / slotNum;
                itemcounts.remove(ish, occurrences);
                is.stackSize = occurrences;
            }
        }
    }
    for (int x = 0; x < ic.getWidth(); x++) {
        for (int y = 0; y < ic.getHeight(); y++) {
            ItemStack is = ic.getStackInRowAndColumn(x, y);
            if (is != null) {
                ItemStackHolder ish = new ItemStackHolder(is);
                if (itemcounts.count(ish) > 0) {
                    is.stackSize += itemcounts.setCount(ish, 0);
                }
            }
        }
    }
    for (int slot = context.slotMapping.begin; slot < context.slotMapping.end + 1; slot++) {
        context.player.openContainer.getSlot(slot).onSlotChanged();
    }
}

From source file:org.tensorics.core.tensor.Position.java

/**
 * Retrieves a position instance, representing the given coordinates. This is a convenience method to a call to
 * {@link #of(Iterable)}.//from   w  w  w. jav  a2s.c o m
 * 
 * @param coordinates the coordinates for which to retrieve a position instance
 * @return a position instance
 * @throws IllegalArgumentException in case the coordinates are not a valid set for creating a position.
 * @throws NullPointerException if the given coordinates array is {@code null}
 * @see #of(Iterable)
 */
@SafeVarargs
public static Position of(Object... coordinates) {
    requireNonNull(coordinates, "coordinates must not be null");
    return of(ImmutableMultiset.copyOf(coordinates));
}

From source file:com.github.rinde.rinsim.central.rt.ScheduleUtil.java

static List<List<Parcel>> fixSchedule(ImmutableList<ImmutableList<Parcel>> schedule, GlobalStateObject state) {

    checkArgument(schedule.size() == state.getVehicles().size(),
            "The number of routes (%s) and the number of vehicles (%s) must " + "be equal.", schedule.size(),
            state.getVehicles().size());
    checkArgument(!state.getVehicles().get(0).getRoute().isPresent(),
            "A state object without routes is expected.");

    // only parcels in this set may occur in the schedule
    final Set<Parcel> undeliveredParcels = new HashSet<>();
    undeliveredParcels.addAll(state.getAvailableParcels());

    // for each vehicle, we create a multiset that is a representation of the
    // number of times the occurrence of a parcel is REQUIRED to be in the
    // route of the vehicle
    final List<Multiset<Parcel>> expectedRoutes = new ArrayList<>();
    for (int i = 0; i < state.getVehicles().size(); i++) {
        expectedRoutes.add(HashMultiset.<Parcel>create());
        final VehicleStateObject vehicle = state.getVehicles().get(i);
        expectedRoutes.get(i).addAll(vehicle.getContents());
        if (vehicle.getDestination().isPresent()
                && !vehicle.getContents().contains(vehicle.getDestination().get())) {
            expectedRoutes.get(i).add(vehicle.getDestination().get(), 2);
        }/*from  w w w . j a  va  2 s  .co m*/
        undeliveredParcels.addAll(vehicle.getContents());
    }

    // create map of parcel -> vehicle index
    final Multimap<Parcel, Integer> parcelOwner = LinkedHashMultimap.create();
    for (int i = 0; i < schedule.size(); i++) {
        final List<Parcel> route = schedule.get(i);
        final Set<Parcel> routeSet = ImmutableSet.copyOf(route);
        for (final Parcel p : routeSet) {
            parcelOwner.put(p, i);
        }
    }

    // copy schedule into a modifiable structure
    final List<List<Parcel>> newSchedule = new ArrayList<>();
    for (final ImmutableList<Parcel> route : schedule) {
        newSchedule.add(new ArrayList<>(route));
    }
    // compare with current vehicle cargo
    for (int i = 0; i < state.getVehicles().size(); i++) {
        final VehicleStateObject vehicle = state.getVehicles().get(i);
        final Multiset<Parcel> routeSet = ImmutableMultiset.copyOf(schedule.get(i));

        final Set<Parcel> test = Sets.union(routeSet.elementSet(), expectedRoutes.get(i).elementSet());

        for (final Parcel p : test) {
            final int actualOccurences = routeSet.count(p);
            checkState(actualOccurences <= 2);
            final int expectedOccurrences = expectedRoutes.get(i).count(p);

            if (!undeliveredParcels.contains(p)) {
                // it is already delivered, remove all occurrences
                newSchedule.get(i).removeAll(Collections.singleton(p));
            } else if (actualOccurences != expectedOccurrences && expectedOccurrences > 0) {
                if (expectedOccurrences == 1 && actualOccurences == 2) {
                    newSchedule.get(i).remove(p);
                } else {
                    // expected occurr = 1 or 2
                    final boolean destinationIsCurrent = vehicle.getDestination().asSet().contains(p);

                    int toAdd = expectedOccurrences - actualOccurences;

                    // add it once at the front of the route
                    if (destinationIsCurrent) {
                        newSchedule.get(i).add(0, p);
                        toAdd--;
                    }

                    // add it once to the end of the route
                    if (toAdd > 0) {
                        newSchedule.get(i).add(p);
                    }
                }
            }

            // if the parcel is expected in the current vehicle, but it also appears
            // in (an) other vehicle(s), we have to remove it there
            if (expectedOccurrences > 0 && parcelOwner.containsKey(p)) {
                for (final Integer v : parcelOwner.get(p)) {
                    if (!v.equals(i)) {
                        newSchedule.get(v).removeAll(Collections.singleton(p));
                    }
                }
            }
        }

        if (vehicle.getDestination().isPresent()
                && !newSchedule.get(i).get(0).equals(vehicle.getDestination().get())) {
            newSchedule.get(i).remove(vehicle.getDestination().get());
            newSchedule.get(i).add(0, vehicle.getDestination().get());
        }
    }
    return newSchedule;
}

From source file:com.facebook.presto.util.IterableTransformer.java

public Multiset<E> bag() {
    return ImmutableMultiset.copyOf(iterable);
}

From source file:org.lightjason.agentspeak.language.execution.action.CProxyAction.java

/**
 * ctor/*from  ww  w. ja  va  2  s  .c o  m*/
 *
 * @param p_actions actions definition
 * @param p_literal literal
 */
public CProxyAction(final Map<IPath, IAction> p_actions, final ILiteral p_literal) {
    // create cache for scoring action and define action
    final Multiset<IAction> l_scoringcache = HashMultiset.create();
    m_execution = new CActionWrapper(p_literal, p_actions, l_scoringcache);

    // scoring set is created so build-up to an unmodifieable set
    m_scoringcache = ImmutableMultiset.copyOf(l_scoringcache);
}

From source file:ai.grakn.graql.internal.query.match.MatchQueryInternal.java

@Override
default InsertQuery insert(Collection<? extends Var> vars) {
    ImmutableMultiset<VarAdmin> varAdmins = ImmutableMultiset.copyOf(AdminConverter.getVarAdmins(vars));
    return Queries.insert(varAdmins, admin());
}

From source file:ai.grakn.graql.internal.query.match.AbstractMatch.java

@Override
public final InsertQuery insert(Collection<? extends VarPattern> vars) {
    ImmutableMultiset<VarPatternAdmin> varAdmins = ImmutableMultiset.copyOf(AdminConverter.getVarAdmins(vars));
    return Queries.insert(varAdmins, admin());
}

From source file:ai.grakn.graql.internal.query.match.AbstractMatchQuery.java

@Override
public final InsertQuery insert(Collection<? extends VarPatternBuilder> vars) {
    ImmutableMultiset<VarPatternAdmin> varAdmins = ImmutableMultiset.copyOf(AdminConverter.getVarAdmins(vars));
    return Queries.insert(varAdmins, admin());
}

From source file:org.eclipse.sirius.tests.sample.component.util.PayloadMarkerAdapter.java

public Multiset<String> getUniqueContexts() {
    return ImmutableMultiset.copyOf(uniqueContexts);
}

From source file:ai.grakn.test.engine.tasks.BackgroundTaskTestUtils.java

public static Multiset<TaskId> completableTasks(List<TaskState> tasks) {
    Map<TaskId, Long> tasksById = tasks.stream().collect(groupingBy(TaskState::getId, counting()));
    Set<TaskId> retriedTasks = Maps.filterValues(tasksById, count -> count != null && count > 1).keySet();

    Set<TaskId> completableTasks = Sets.newHashSet();
    Set<TaskId> visitedTasks = Sets.newHashSet();

    Set<TaskId> appearedTasks = Sets.newHashSet();

    tasks.forEach(task -> {/*from w w w .j a  v a 2 s  . co m*/
        // A task is expected to complete only if:
        // 1. It has not already executed and failed
        // 2. it is not a failing task
        // 3. it is RUNNING or not being retried
        TaskId id = task.getId();
        boolean visited = visitedTasks.contains(id);
        boolean willFail = task.taskClass().equals(FailingMockTask.class);
        boolean isRunning = appearedTasks.contains(id);
        boolean isRetried = retriedTasks.contains(id);
        if (!visited && (isRunning || !isRetried)) {
            if (!willFail) {
                completableTasks.add(id);
            }
            visitedTasks.add(id);
        }
        appearedTasks.add(id);
    });

    return ImmutableMultiset.copyOf(completableTasks);
}