com.google.common.collect.Multiset.Entry Java Examples

The following examples show how to use com.google.common.collect.Multiset.Entry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public UnmodifiableIterator<E> iterator() {
  final Iterator<Entry<E>> entryIterator = entrySet().iterator();
  return new UnmodifiableIterator<E>() {
    int remaining;
    E element;

    @Override
    public boolean hasNext() {
      return (remaining > 0) || entryIterator.hasNext();
    }

    @Override
    public E next() {
      if (remaining <= 0) {
        Entry<E> entry = entryIterator.next();
        element = entry.getElement();
        remaining = entry.getCount();
      }
      remaining--;
      return element;
    }
  };
}
 
Example #2
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public UnmodifiableIterator<E> iterator() {
  final Iterator<Entry<E>> entryIterator = entrySet().iterator();
  return new UnmodifiableIterator<E>() {
    int remaining;

    E element;


    @Override
    public boolean hasNext() {
      return (remaining > 0) || entryIterator.hasNext();
    }

    @Override
    public E next() {
      if (remaining <= 0) {
        Entry<E> entry = entryIterator.next();
        element = entry.getElement();
        remaining = entry.getCount();
      }
      remaining--;
      return element;
    }
  };
}
 
Example #3
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public UnmodifiableIterator<E> iterator() {
  final Iterator<Entry<E>> entryIterator = entrySet().iterator();
  return new UnmodifiableIterator<E>() {
    int remaining;

    E element;


    @Override
    public boolean hasNext() {
      return (remaining > 0) || entryIterator.hasNext();
    }

    @Override
    public E next() {
      if (remaining <= 0) {
        Entry<E> entry = entryIterator.next();
        element = entry.getElement();
        remaining = entry.getCount();
      }
      remaining--;
      return element;
    }
  };
}
 
Example #4
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@SuppressWarnings("cast")
@Override
public boolean remove(Object object) {
  if (object instanceof Multiset.Entry) {
    Entry<?> entry = (Entry<?>) object;
    Object element = entry.getElement();
    int entryCount = entry.getCount();
    if (entryCount != 0) {
      // Safe as long as we never add a new entry, which we won't.
      @SuppressWarnings("unchecked")
      Multiset<Object> multiset = (Multiset) multiset();
      return multiset.setCount(element, entryCount, 0);
    }
  }
  return false;
}
 
Example #5
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public boolean contains(@Nullable Object o) {
  if (o instanceof Entry) {
    /*
     * The GWT compiler wrongly issues a warning here.
     */
    @SuppressWarnings("cast")
    Entry<?> entry = (Entry<?>) o;
    if (entry.getCount() <= 0) {
      return false;
    }
    int count = multiset().count(entry.getElement());
    return count == entry.getCount();
  }
  return false;
}
 
Example #6
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@SuppressWarnings("cast")
@Override
public boolean remove(Object object) {
  if (object instanceof Multiset.Entry) {
    Entry<?> entry = (Entry<?>) object;
    Object element = entry.getElement();
    int entryCount = entry.getCount();
    if (entryCount != 0) {
      // Safe as long as we never add a new entry, which we won't.
      @SuppressWarnings("unchecked")
      Multiset<Object> multiset = (Multiset) multiset();
      return multiset.setCount(element, entryCount, 0);
    }
  }
  return false;
}
 
Example #7
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public boolean contains(@Nullable Object o) {
  if (o instanceof Entry) {
    /*
     * The GWT compiler wrongly issues a warning here.
     */
    @SuppressWarnings("cast")
    Entry<?> entry = (Entry<?>) o;
    if (entry.getCount() <= 0) {
      return false;
    }
    int count = multiset().count(entry.getElement());
    return count == entry.getCount();
  }
  return false;
}
 
Example #8
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Delegate implementation which cares about the element type.
 */

private static <E> boolean retainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) {
  checkNotNull(multisetToModify);
  checkNotNull(occurrencesToRetain);
  // Avoiding ConcurrentModificationExceptions is tricky.
  Iterator<Entry<E>> entryIterator = multisetToModify.entrySet().iterator();
  boolean changed = false;
  while (entryIterator.hasNext()) {
    Entry<E> entry = entryIterator.next();
    int retainCount = occurrencesToRetain.count(entry.getElement());
    if (retainCount == 0) {
      entryIterator.remove();
      changed = true;
    } else if (retainCount < entry.getCount()) {
      multisetToModify.setCount(entry.getElement(), retainCount);
      changed = true;
    }
  }
  return changed;
}
 
Example #9
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * An implementation of {@link Multiset#addAll}.
 */

static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
  if (elements.isEmpty()) {
    return false;
  }
  if (elements instanceof Multiset) {
    Multiset<? extends E> that = cast(elements);
    for (Entry<? extends E> entry : that.entrySet()) {
      self.add(entry.getElement(), entry.getCount());
    }
  } else {
    Iterators.addAll(self, elements.iterator());
  }
  return true;
}
 
Example #10
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * An implementation of {@link Multiset#equals}.
 */

static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object) {
  if (object == multiset) {
    return true;
  }
  if (object instanceof Multiset) {
    Multiset<?> that = (Multiset<?>) object;
    /*
     * We can't simply check whether the entry sets are equal, since that
     * approach fails when a TreeMultiset has a comparator that returns 0
     * when passed unequal elements.
     */
    if (multiset.size() != that.size()
        || multiset.entrySet().size() != that.entrySet().size()) {
      return false;
    }
    for (Entry<?> entry : that.entrySet()) {
      if (multiset.count(entry.getElement()) != entry.getCount()) {
        return false;
      }
    }
    return true;
  }
  return false;
}
 
Example #11
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * An implementation of {@link Multiset#addAll}.
 */

static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) {
  if (elements.isEmpty()) {
    return false;
  }
  if (elements instanceof Multiset) {
    Multiset<? extends E> that = cast(elements);
    for (Entry<? extends E> entry : that.entrySet()) {
      self.add(entry.getElement(), entry.getCount());
    }
  } else {
    Iterators.addAll(self, elements.iterator());
  }
  return true;
}
 
Example #12
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public boolean contains(@Nullable Object o) {
  if (o instanceof Entry) {
    /*
     * The GWT compiler wrongly issues a warning here.
     */
    @SuppressWarnings("cast")
    Entry<?> entry = (Entry<?>) o;
    if (entry.getCount() <= 0) {
      return false;
    }
    int count = multiset().count(entry.getElement());
    return count == entry.getCount();
  }
  return false;
}
 
Example #13
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public UnmodifiableIterator<E> iterator() {
  final Iterator<Entry<E>> entryIterator = entrySet().iterator();
  return new UnmodifiableIterator<E>() {
    int remaining;

    E element;


    @Override
    public boolean hasNext() {
      return (remaining > 0) || entryIterator.hasNext();
    }

    @Override
    public E next() {
      if (remaining <= 0) {
        Entry<E> entry = entryIterator.next();
        element = entry.getElement();
        remaining = entry.getCount();
      }
      remaining--;
      return element;
    }
  };
}
 
Example #14
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * An implementation of {@link Multiset#equals}.
 */

static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object) {
  if (object == multiset) {
    return true;
  }
  if (object instanceof Multiset) {
    Multiset<?> that = (Multiset<?>) object;
    /*
     * We can't simply check whether the entry sets are equal, since that
     * approach fails when a TreeMultiset has a comparator that returns 0
     * when passed unequal elements.
     */
    if (multiset.size() != that.size()
        || multiset.entrySet().size() != that.entrySet().size()) {
      return false;
    }
    for (Entry<?> entry : that.entrySet()) {
      if (multiset.count(entry.getElement()) != entry.getCount()) {
        return false;
      }
    }
    return true;
  }
  return false;
}
 
Example #15
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * An implementation of {@link Multiset#equals}.
 */

static boolean equalsImpl(Multiset<?> multiset, @Nullable Object object) {
  if (object == multiset) {
    return true;
  }
  if (object instanceof Multiset) {
    Multiset<?> that = (Multiset<?>) object;
    /*
     * We can't simply check whether the entry sets are equal, since that
     * approach fails when a TreeMultiset has a comparator that returns 0
     * when passed unequal elements.
     */
    if (multiset.size() != that.size()
        || multiset.entrySet().size() != that.entrySet().size()) {
      return false;
    }
    for (Entry<?> entry : that.entrySet()) {
      if (multiset.count(entry.getElement()) != entry.getCount()) {
        return false;
      }
    }
    return true;
  }
  return false;
}
 
Example #16
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Iterator<E> iterator() {
  return new TransformedIterator<Entry<E>, E>(multiset().entrySet().iterator()) {
    @Override
    E transform(Entry<E> entry) {
      return entry.getElement();
    }
  };
}
 
Example #17
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Adds each element of {@code elements} to the {@code ImmutableMultiset}.
 *
 * @param elements the {@code Iterable} to add to the {@code
 *     ImmutableMultiset}
 * @return this {@code Builder} object
 * @throws NullPointerException if {@code elements} is null or contains a
 *     null element
 */

@CanIgnoreReturnValue
@Override
public Builder<E> addAll(Iterable<? extends E> elements) {
  if (elements instanceof Multiset) {
    Multiset<? extends E> multiset = Multisets.cast(elements);
    for (Entry<? extends E> entry : multiset.entrySet()) {
      addCopies(entry.getElement(), entry.getCount());
    }
  } else {
    super.addAll(elements);
  }
  return this;
}
 
Example #18
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean contains(Object o) {
  if (o instanceof Entry) {
    Entry<?> entry = (Entry<?>) o;
    if (entry.getCount() <= 0) {
      return false;
    }
    int count = count(entry.getElement());
    return count == entry.getCount();
  }
  return false;
}
 
Example #19
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Iterator<E> iterator() {
  return new TransformedIterator<Entry<E>, E>(multiset().entrySet().iterator()) {
    @Override
    E transform(Entry<E> entry) {
      return entry.getElement();
    }
  };
}
 
Example #20
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Adds each element of {@code elements} to the {@code ImmutableMultiset}.
 *
 * @param elements the {@code Iterable} to add to the {@code
 *     ImmutableMultiset}
 * @return this {@code Builder} object
 * @throws NullPointerException if {@code elements} is null or contains a
 *     null element
 */

@CanIgnoreReturnValue
@Override
public Builder<E> addAll(Iterable<? extends E> elements) {
  if (elements instanceof Multiset) {
    Multiset<? extends E> multiset = Multisets.cast(elements);
    for (Entry<? extends E> entry : multiset.entrySet()) {
      addCopies(entry.getElement(), entry.getCount());
    }
  } else {
    super.addAll(elements);
  }
  return this;
}
 
Example #21
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * An implementation of {@link Multiset#size}.
 */

static int sizeImpl(Multiset<?> multiset) {
  long size = 0;
  for (Entry<?> entry : multiset.entrySet()) {
    size += entry.getCount();
  }
  return Ints.saturatedCast(size);
}
 
Example #22
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is
 * highest count first, with ties broken by the iteration order of the original multiset.
 *
 * @since 11.0
 */

@Beta
public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) {
  List<Entry<E>> sortedEntries = Multisets.DECREASING_COUNT_ORDERING.immutableSortedCopy(multiset.entrySet());
  return ImmutableMultiset.copyFromEntries(sortedEntries);
}
 
Example #23
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@GwtIncompatible // not present in emulated superclass
@Override
int copyIntoArray(Object[] dst, int offset) {
  for (Multiset.Entry<E> entry : entrySet()) {
    Arrays.fill(dst, offset, offset + entry.getCount(), entry.getElement());
    offset += entry.getCount();
  }
  return offset;
}
 
Example #24
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@GwtIncompatible // not present in emulated superclass
@Override
int copyIntoArray(Object[] dst, int offset) {
  for (Multiset.Entry<E> entry : entrySet()) {
    Arrays.fill(dst, offset, offset + entry.getCount(), entry.getElement());
    offset += entry.getCount();
  }
  return offset;
}
 
Example #25
Source File: ImmutableMultiset.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
static <E> ImmutableMultiset<E> copyFromEntries(Collection<? extends Entry<? extends E>> entries) {
  if (entries.isEmpty()) {
    return of();
  } else {
    return new RegularImmutableMultiset<E>(entries);
  }
}
 
Example #26
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is
 * highest count first, with ties broken by the iteration order of the original multiset.
 *
 * @since 11.0
 */
@Beta
public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) {
  List<Entry<E>> sortedEntries =
      Multisets.DECREASING_COUNT_ORDERING.immutableSortedCopy(multiset.entrySet());
  return ImmutableMultiset.copyFromEntries(sortedEntries);
}
 
Example #27
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * An implementation of {@link Multiset#size}.
 */

static int sizeImpl(Multiset<?> multiset) {
  long size = 0;
  for (Entry<?> entry : multiset.entrySet()) {
    size += entry.getCount();
  }
  return Ints.saturatedCast(size);
}
 
Example #28
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns {@code true} if {@code subMultiset.count(o) <=
 * superMultiset.count(o)} for all {@code o}.
 *
 * @since 10.0
 */

@CanIgnoreReturnValue
public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) {
  checkNotNull(superMultiset);
  checkNotNull(subMultiset);
  for (Entry<?> entry : subMultiset.entrySet()) {
    int superCount = superMultiset.count(entry.getElement());
    if (superCount < entry.getCount()) {
      return false;
    }
  }
  return true;
}
 
Example #29
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * Indicates whether an object equals this entry, following the behavior
     * specified in {@link Multiset.Entry#equals}.
     */
    @Override
    public boolean equals(@Nullable Object object) {
      if (object instanceof Multiset.Entry) {
        Multiset.Entry<?> that = (Multiset.Entry<?>) object;
        return this.getCount() == that.getCount()
&& Objects.equal(this.getElement(), that.getElement());
      }
      return false;
    }
 
Example #30
Source File: Multisets.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Iterator<E> iterator() {
  return new TransformedIterator<Entry<E>, E>(multiset().entrySet().iterator()) {
    @Override
    E transform(Entry<E> entry) {
      return entry.getElement();
    }
  };
}