Example #1
0
 @Override
 public boolean add(T item) {
   Counter counter = this.items.getIfAbsentPut(item, NEW_COUNTER_BLOCK);
   counter.increment();
   this.size++;
   return true;
 }
Example #2
0
  public boolean removeOccurrences(Object item, int occurrences) {
    if (occurrences < 0) {
      throw new IllegalArgumentException("Cannot remove a negative number of occurrences");
    }

    if (occurrences == 0) {
      return false;
    }

    Counter counter = this.items.get(item);
    if (counter == null) {
      return false;
    }
    int start = counter.getCount();

    if (occurrences >= start) {
      this.items.remove(item);
      this.size -= start;
      return true;
    }

    counter.add(occurrences * -1);
    this.size -= occurrences;
    return true;
  }
 @Test
 public void iterator() {
   Counter counter = new Counter();
   for (Object each : this.unmodifiableCollection) {
     counter.increment();
   }
   Assert.assertEquals(4, counter.getCount());
 }
 @Test
 public void forEachWithIndex() {
   final Counter counter = new Counter();
   this.unmodifiableCollection.forEachWithIndex(
       new ObjectIntProcedure<String>() {
         public void value(String band, int index) {
           counter.add(index);
         }
       });
   Assert.assertEquals(6, counter.getCount());
 }
 @Test
 public void forEach() {
   final Counter counter = new Counter();
   this.unmodifiableCollection.forEach(
       new Procedure<String>() {
         public void value(String band) {
           counter.increment();
         }
       });
   Assert.assertEquals(4, counter.getCount());
 }
Example #6
0
 @Override
 public boolean removeAllIterable(Iterable<?> iterable) {
   int oldSize = this.size;
   for (Object each : iterable) {
     Counter removed = this.items.remove(each);
     if (removed != null) {
       this.size -= removed.getCount();
     }
   }
   return this.size != oldSize;
 }
Example #7
0
 @Override
 public int hashCode() {
   final Counter counter = new Counter();
   this.forEachWithOccurrences(
       new ObjectIntProcedure<T>() {
         public void value(T each, int count) {
           counter.add((each == null ? 0 : each.hashCode()) ^ count);
         }
       });
   return counter.getCount();
 }
Example #8
0
 @Override
 public <P> int countWith(final Predicate2<? super T, ? super P> predicate, final P parameter) {
   final Counter result = new Counter();
   this.forEachWithOccurrences(
       new ObjectIntProcedure<T>() {
         public void value(T each, int occurrences) {
           if (predicate.accept(each, parameter)) {
             result.add(occurrences);
           }
         }
       });
   return result.getCount();
 }
Example #9
0
 @Override
 public boolean remove(Object item) {
   Counter counter = this.items.get(item);
   if (counter != null) {
     if (counter.getCount() > 1) {
       counter.decrement();
     } else {
       this.items.remove(item);
     }
     this.size--;
     return true;
   }
   return false;
 }
Example #10
0
 public int occurrencesOf(Object item) {
   Counter counter = this.items.get(item);
   return counter == null ? 0 : counter.getCount();
 }