Example usage for com.google.common.collect Multiset setCount

List of usage examples for com.google.common.collect Multiset setCount

Introduction

In this page you can find the example usage for com.google.common.collect Multiset setCount.

Prototype

int setCount(E element, int count);

Source Link

Document

Adds or removes the necessary occurrences of an element such that the element attains the desired count.

Usage

From source file:org.summer.dsl.xbase.typesystem.references.LightweightTypeReference.java

/**
 * Returns the list of all super types which includes the super class and the 
 * implemented interfaces. The type parameters of the provided super types are resolved.
 * That means, the super types of <code>ArrayList&lt;String&gt;</code> includes
 * <code>List&lt;String&gt;</code> and <code>Collection&lt;String&gt;</code> 
 * rather than <code>Collection&lt;E&gt;</code>.
 * //  www  .j  a  va 2 s .c  o  m
 * @return the list of all super types, can be empty.
 */
public List<LightweightTypeReference> getAllSuperTypes() {
    final List<LightweightTypeReference> result = Lists.newArrayList();
    final Multiset<JvmType> distances = HashMultiset.create(7);
    final Multiset<JvmType> counterPerType = HashMultiset.create(7);
    collectSuperTypes(new SuperTypeAcceptor() {

        int counter = 0;

        public boolean accept(LightweightTypeReference superType, int distance) {
            JvmType type = superType.getType();
            counterPerType.add(type, counter++);
            if (distances.contains(type)) {
                int currentCount = distances.count(type);
                if (currentCount < distance + 1) {
                    distances.setCount(type, distance + 1);
                } else {
                    return false;
                }
            } else {
                result.add(superType);
                distances.add(type, distance + 1);
            }
            return true;
        }

    });
    Collections.sort(result, new Comparator<LightweightTypeReference>() {
        public int compare(@Nullable LightweightTypeReference o1, @Nullable LightweightTypeReference o2) {
            if (o1 == null || o2 == null) {
                throw new IllegalArgumentException();
            }
            JvmType type1 = o1.getType();
            JvmType type2 = o2.getType();
            if (type1 == null)
                return 1;
            if (type2 == null)
                return -1;
            int distanceCompare = Ints.compare(distances.count(type1), distances.count(type2));
            if (distanceCompare != 0)
                return distanceCompare;
            return Ints.compare(counterPerType.count(type1), counterPerType.count(type2));
        }
    });
    return result;
}

From source file:org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference.java

/**
 * Returns the list of all super types which includes the super class and the 
 * implemented interfaces. The type parameters of the provided super types are resolved.
 * That means, the super types of <code>ArrayList&lt;String&gt;</code> includes
 * <code>List&lt;String&gt;</code> and <code>Collection&lt;String&gt;</code> 
 * rather than <code>Collection&lt;E&gt;</code>.
 * /*  www .jav a2s. co m*/
 * @return the list of all super types, can be empty.
 */
public List<LightweightTypeReference> getAllSuperTypes() {
    final List<LightweightTypeReference> result = Lists.newArrayList();
    final Multiset<JvmType> distances = HashMultiset.create(7);
    final Multiset<JvmType> counterPerType = HashMultiset.create(7);
    collectSuperTypes(new SuperTypeAcceptor() {

        int counter = 0;

        @Override
        public boolean accept(LightweightTypeReference superType, int distance) {
            JvmType type = superType.getType();
            counterPerType.add(type, counter++);
            if (distances.contains(type)) {
                int currentCount = distances.count(type);
                if (currentCount < distance + 1) {
                    distances.setCount(type, distance + 1);
                } else {
                    return false;
                }
            } else {
                result.add(superType);
                distances.add(type, distance + 1);
            }
            return true;
        }

    });
    Collections.sort(result, new Comparator<LightweightTypeReference>() {
        @Override
        public int compare(/* @Nullable */ LightweightTypeReference o1,
                /* @Nullable */ LightweightTypeReference o2) {
            if (o1 == null || o2 == null) {
                throw new IllegalArgumentException();
            }
            JvmType type1 = o1.getType();
            JvmType type2 = o2.getType();
            if (type1 == null)
                return 1;
            if (type2 == null)
                return -1;
            int distanceCompare = Ints.compare(distances.count(type1), distances.count(type2));
            if (distanceCompare != 0)
                return distanceCompare;
            return Ints.compare(counterPerType.count(type1), counterPerType.count(type2));
        }
    });
    return result;
}

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));
            }//ww  w  . j  a  v a 2s .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();
    }
}