Example usage for org.apache.commons.collections15 Bag getCount

List of usage examples for org.apache.commons.collections15 Bag getCount

Introduction

In this page you can find the example usage for org.apache.commons.collections15 Bag getCount.

Prototype

int getCount(E object);

Source Link

Document

Returns the number of occurrences (cardinality) of the given object currently in the bag.

Usage

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> intersectBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();//  w  w w  . j  ava2 s  .c o m
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        if ((count0 > 0) && (count1 > 0)) {
            targetBag.add(item, Math.min(count0, count1));
        }
    }
    return targetBag;
}

From source file:com.diversityarrays.kdxplore.vistool.VisToolData.java

static private void appendLines(String fmt, Bag<String> bag, List<String> lines) {
    for (String s : bag.uniqueSet()) {
        lines.add("  " + MessageFormat.format(fmt, s, bag.getCount(s))); //$NON-NLS-1$
    }//from   w  w  w.  j  av  a2s.  c  o m
}

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> unionBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();/*w ww .  j  a  va  2s .  co m*/
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        targetBag.add(item, Math.max(count0, count1));
    }
    for (T item : b1) {
        if (!b0.contains(item)) {
            final int count1 = b1.getCount(item);
            targetBag.add(item, count1);
        }
    }
    return targetBag;
}

From source file:mulavito.utils.BagUtil.java

/**
 * Set the counter of the given entry in the given set.
 * //from w  w  w. j a v a 2 s . c  o m
 * @param bag
 *            The bag to modify
 * @param entry
 *            The entry whose counter is set
 * @param count
 *            The new non-negative counter value
 */
public static <T> void setCount(Bag<T> bag, T entry, int count) {
    if (bag == null)
        throw new IllegalArgumentException();
    else if (count < 0)
        throw new IllegalArgumentException();

    int oldCount = bag.getCount(entry);

    if (count > oldCount)
        bag.add(entry, count - oldCount);
    else if (count < oldCount)
        bag.remove(entry, oldCount - count);
}

From source file:com.diversityarrays.kdxplore.curate.TypedSampleMeasurement.java

/**
 * Return null if all the DeviceNames are unique else
 * the shortFormat if it is sufficient to uniquely identify the TypedSampleMeasurements
 * @param coll//from  w  ww . ja v a 2 s .  c o  m
 * @param shortFormat
 * @param longFormat
 * @return
 */
static public DateFormat getDateFormatForUniqueIdent(Collection<TypedSampleMeasurement> coll,
        DateFormat shortFormat, DateFormat longFormat) {
    Map<String, Bag<String>> datesByDeviceName = new HashMap<>();

    boolean foundDuplicateDeviceName = false;
    for (TypedSampleMeasurement tsm : coll) {
        if (DeviceType.KDSMART == tsm.deviceIdentifier.getDeviceType()) {
            String deviceName = tsm.deviceIdentifier.getDeviceName();
            if (datesByDeviceName.containsKey(deviceName)) {
                foundDuplicateDeviceName = true;
            } else {
                datesByDeviceName.put(deviceName, new HashBag<>());
            }
        }
    }
    if (!foundDuplicateDeviceName) {
        return null;
    }

    for (TypedSampleMeasurement tsm : coll) {
        if (DeviceType.KDSMART == tsm.deviceIdentifier.getDeviceType()) {
            String deviceName = tsm.deviceIdentifier.getDeviceName();

            Bag<String> bag = datesByDeviceName.get(deviceName);
            if (tsm.sampleGroupDate != null) {
                String s = longFormat.format(tsm.sampleGroupDate);
                bag.add(s);
            }
        }
    }

    for (Bag<String> bag : datesByDeviceName.values()) {
        for (String key : bag.uniqueSet()) {
            if (bag.getCount(key) > 1) {
                return longFormat;
            }
        }
    }

    return shortFormat;
}

From source file:com.diversityarrays.kdxplore.stats.StatsUtil.java

static public List<String> computeMode(Bag<String> svalues, NumericTraitValidationProcessor tvp) {

    List<String> result;

    Map<Integer, List<String>> valuesByCount = new HashMap<Integer, List<String>>();

    for (String v : svalues.uniqueSet()) {
        Integer count = svalues.getCount(v);
        List<String> list = valuesByCount.get(count);
        if (list == null) {
            list = new ArrayList<String>();
            valuesByCount.put(count, list);
        }/*from  ww  w. j  av  a2 s  .  c  o  m*/
        list.add(v);
    }
    List<Integer> counts = new ArrayList<Integer>(valuesByCount.keySet());
    int nCounts = counts.size();

    if (nCounts < 1) {
        result = new ArrayList<>();
        ;
    } else if (nCounts == 1) {
        result = valuesByCount.get(counts.get(0));
    } else {
        Collections.sort(counts);

        int lastIndex = nCounts - 1;
        int countMax = counts.get(lastIndex);

        result = valuesByCount.get(countMax);
    }

    return result;
}

From source file:de.uniba.wiai.kinf.pw.projects.lillytab.reasoner.ABoxRoleTest.java

@Test
public void dualRoleInheritanceTest() throws EInconsistencyException {
    final IRBox<String, String, String, String> rbox = _abox.getTBox().getRBox();
    rbox.getAssertedRBox().addRole("parent", RoleType.OBJECT_PROPERTY);
    rbox.getAssertedRBox().addRole("r0", RoleType.OBJECT_PROPERTY);
    rbox.getAssertedRBox().addRole("r1", RoleType.OBJECT_PROPERTY);
    rbox.getAssertedRBox().addSubRole("parent", "r0");
    rbox.getAssertedRBox().addSubRole("parent", "r1");

    final IABoxNode<String, String, String, String> aNode = _abox.getOrAddIndividualNode("a");
    final IABoxNode<String, String, String, String> bNode = _abox.getOrAddIndividualNode("b");
    aNode.getRABox().getAssertedSuccessors().put("r0", bNode.getNodeID());
    aNode.getRABox().getAssertedSuccessors().put("r1", bNode.getNodeID());

    final Bag<String> outRoles = new HashBag<>();
    for (String role : aNode.getRABox().getOutgoingRoles()) {
        outRoles.add(role);// w  ww .j  a  va  2 s.c o  m
    }
    assertEquals(1, outRoles.getCount("parent"));
}

From source file:de.dhke.projects.cutil.collections.BagUtilTest.java

/**
 * Test of unionBags method, of class BagUtil.
 *///from  w w w. ja v a2s. c o  m
@Test
public void testUnionBags() {
    System.out.println("unionBags");
    final Bag<String> b0 = new TreeBag<>(Arrays.asList("A", "B", "C"));
    final Bag<String> b1 = new TreeBag<>(Arrays.asList("A", "C"));

    final Bag<String> union = new TreeBag<>();
    BagUtil.unionBags(b0, b1, union);
    assertTrue(union.contains("A"));
    assertTrue(union.contains("B"));
    assertTrue(union.contains("C"));
    assertEquals(1, union.getCount("A"));
    assertEquals(1, union.getCount("B"));
    assertEquals(1, union.getCount("C"));
}

From source file:de.dhke.projects.cutil.collections.BagUtilTest.java

/**
 * Test of intersectBags method, of class BagUtil.
 *//*from   w ww  .j ava2s  .  co m*/
@Test
public void testIntersectBags() {
    System.out.println("intersectBags");
    final Bag<String> b0 = new TreeBag<>(Arrays.asList("A", "B", "C"));
    final Bag<String> b1 = new TreeBag<>(Arrays.asList("A", "C"));

    final Bag<String> intersection = new TreeBag<>();
    BagUtil.intersectBags(b0, b1, intersection);
    assertTrue(intersection.contains("A"));
    assertFalse(intersection.contains("B"));
    assertTrue(intersection.contains("C"));
    assertEquals(1, intersection.getCount("A"));
    assertEquals(0, intersection.getCount("B"));
    assertEquals(1, intersection.getCount("C"));
}

From source file:com.davidsoergel.stats.Multinomial.java

public Multinomial(Bag<T> counts) throws DistributionException {
    this();/*from   w  ww. j a va2  s .  c  om*/
    for (T k : counts.uniqueSet()) {
        put(k, counts.getCount(k));
    }
    normalize();
}