All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.TreeMultiset Maven / Gradle / Ivy

Go to download

Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0

There is a newer version: 1.0
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.base.Nullable;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Multiset implementation backed by a {@code TreeMap}. The multiset elements
 * are ordered by their natural sort ordering or by a comparator.
 *
 * @author Neal Kanodia
 * @author Jared Levy
 */
@SuppressWarnings("serial") // we're overriding default serialization
public final class TreeMultiset extends AbstractMapBasedMultiset {

  /**
   * Creates a new, empty multiset, sorted according to the elements' natural
   * order. All elements inserted into the multiset must implement the
   * {@code Comparable} interface. Furthermore, all such elements must be
   * mutually comparable: {@code e1.compareTo(e2)} must not throw a
   * {@code ClassCastException} for any elements {@code e1} and {@code e2} in
   * the multiset. If the user attempts to add an element to the multiset that
   * violates this constraint (for example, the user attempts to add a string
   * element to a set whose elements are integers), the {@code add(Object)}
   * call will throw a {@code ClassCastException}.
   *
   * 

The type specification is {@code }, instead of the * more specific {@code >}, to support * classes defined without generics. */ @SuppressWarnings("unchecked") // See method Javadoc public static TreeMultiset create() { return new TreeMultiset(); } /** * Creates a new, empty multiset, sorted according to the specified * comparator. All elements inserted into the multiset must be mutually * comparable by the specified comparator: {@code comparator.compare(e1, * e2)} must not throw a {@code ClassCastException} for any elements {@code * e1} and {@code e2} in the multiset. If the user attempts to add an element * to the multiset that violates this constraint, the {@code add(Object)} call * will throw a {@code ClassCastException}. * * @param comparator the comparator that will be used to sort this multiset. A * null value indicates that the elements' natural ordering should * be used. */ public static TreeMultiset create(Comparator comparator) { return new TreeMultiset(comparator); } /** * Creates an empty multiset containing the given initial elements, sorted * according to the elements' natural order. * *

The type specification is {@code }, instead of the * more specific {@code >}, to support * classes defined without generics. */ @SuppressWarnings("unchecked") // See method Javadoc public static TreeMultiset create( Iterable elements) { return new TreeMultiset(elements); } /** * Constructs a new, empty multiset, sorted according to the elements' natural * order. All elements inserted into the multiset must implement the * {@code Comparable} interface. Furthermore, all such elements must be * mutually comparable: {@code e1.compareTo(e2)} must not throw a * {@code ClassCastException} for any elements {@code e1} and {@code e2} in * the multiset. If the user attempts to add an element to the multiset that * violates this constraint (for example, the user attempts to add a string * element to a set whose elements are integers), the {@code add(Object)} * call will throw a {@code ClassCastException}. */ public TreeMultiset() { super(new TreeMap()); } /** * Constructs a new, empty multiset, sorted according to the specified * comparator. All elements inserted into the multiset must be mutually * comparable by the specified comparator: {@code comparator.compare(e1, * e2)} must not throw a {@code ClassCastException} for any elements {@code * e1} and {@code e2} in the multiset. If the user attempts to add an element * to the multiset that violates this constraint, the {@code add(Object)} call * will throw a {@code ClassCastException}. * * @param comparator the comparator that will be used to sort this multiset. A * null value indicates that the elements' natural ordering should * be used. */ private TreeMultiset(Comparator comparator) { super(new TreeMap(comparator)); } /** * Constructs an empty multiset containing the given initial elements, sorted * according to the elements' natural order. */ private TreeMultiset(Iterable elements) { this(); Iterables.addAll(this, elements); // careful if we make this class non-final } /** * {@inheritDoc} * *

In {@code TreeMultiset}, the return type of this method is narrowed * from {@link Set} to {@link SortedSet}. */ @Override public SortedSet elementSet() { return (SortedSet) super.elementSet(); } @Override public int count(@Nullable Object element) { try { return super.count(element); } catch (NullPointerException e) { return 0; } catch (ClassCastException e) { return 0; } } @Override public int removeAllOccurrences(@Nullable Object element) { try { return super.removeAllOccurrences(element); } catch (NullPointerException e) { return 0; } catch (ClassCastException e) { return 0; } } @Override protected Set createElementSet() { return new SortedMapBasedElementSet( (SortedMap) backingMap()); } private class SortedMapBasedElementSet extends MapBasedElementSet implements SortedSet { SortedMapBasedElementSet(SortedMap map) { super(map); } SortedMap sortedMap() { return (SortedMap) getMap(); } public Comparator comparator() { return sortedMap().comparator(); } public E first() { return sortedMap().firstKey(); } public E last() { return sortedMap().lastKey(); } public SortedSet headSet(E toElement) { return new SortedMapBasedElementSet(sortedMap().headMap(toElement)); } public SortedSet subSet(E fromElement, E toElement) { return new SortedMapBasedElementSet( sortedMap().subMap(fromElement, toElement)); } public SortedSet tailSet(E fromElement) { return new SortedMapBasedElementSet(sortedMap().tailMap(fromElement)); } } /* * TODO: Decide whether entrySet() should return entries with an equals() * method that calls the comparator to compare the two keys. If that change * is made, AbstractMultiset.equals() can simply check whether two multisets * have equal entry sets. */ /** * @serialData the comparator, the number of distinct elements, the first * element, its count, the second element, its count, and so on */ private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(elementSet().comparator()); Serialization.writeMultiset(this, stream); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); @SuppressWarnings("unchecked") // reading data stored by writeObject Comparator comparator = (Comparator) stream.readObject(); setBackingMap(new TreeMap(comparator)); Serialization.populateMultiset(this, stream); } private static final long serialVersionUID = 0; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy