Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.common.collect;
     16 
     17 import static com.google.common.base.Preconditions.checkArgument;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 import com.google.common.annotations.GwtIncompatible;
     21 
     22 import java.io.IOException;
     23 import java.io.ObjectInputStream;
     24 import java.io.ObjectOutputStream;
     25 import java.util.EnumMap;
     26 import java.util.Iterator;
     27 
     28 /**
     29  * Multiset implementation backed by an {@link EnumMap}.
     30  *
     31  * @author Jared Levy
     32  * @since 2.0 (imported from Google Collections Library)
     33  */
     34 @GwtCompatible(emulated = true)
     35 public final class EnumMultiset<E extends Enum<E>> extends AbstractMapBasedMultiset<E> {
     36   /** Creates an empty {@code EnumMultiset}. */
     37   public static <E extends Enum<E>> EnumMultiset<E> create(Class<E> type) {
     38     return new EnumMultiset<E>(type);
     39   }
     40 
     41   /**
     42    * Creates a new {@code EnumMultiset} containing the specified elements.
     43    *
     44    * <p>This implementation is highly efficient when {@code elements} is itself a {@link
     45    * Multiset}.
     46    *
     47    * @param elements the elements that the multiset should contain
     48    * @throws IllegalArgumentException if {@code elements} is empty
     49    */
     50   public static <E extends Enum<E>> EnumMultiset<E> create(Iterable<E> elements) {
     51     Iterator<E> iterator = elements.iterator();
     52     checkArgument(iterator.hasNext(), "EnumMultiset constructor passed empty Iterable");
     53     EnumMultiset<E> multiset = new EnumMultiset<E>(iterator.next().getDeclaringClass());
     54     Iterables.addAll(multiset, elements);
     55     return multiset;
     56   }
     57 
     58   private transient Class<E> type;
     59 
     60   /** Creates an empty {@code EnumMultiset}. */
     61   private EnumMultiset(Class<E> type) {
     62     super(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
     63     this.type = type;
     64   }
     65 
     66   @GwtIncompatible("java.io.ObjectOutputStream")
     67   private void writeObject(ObjectOutputStream stream) throws IOException {
     68     stream.defaultWriteObject();
     69     stream.writeObject(type);
     70     Serialization.writeMultiset(this, stream);
     71   }
     72 
     73   /**
     74    * @serialData the {@code Class<E>} for the enum type, the number of distinct
     75    *             elements, the first element, its count, the second element, its
     76    *             count, and so on
     77    */
     78   @GwtIncompatible("java.io.ObjectInputStream")
     79   private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
     80     stream.defaultReadObject();
     81     @SuppressWarnings("unchecked") // reading data stored by writeObject
     82     Class<E> localType = (Class<E>) stream.readObject();
     83     type = localType;
     84     setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type)));
     85     Serialization.populateMultiset(this, stream);
     86   }
     87 
     88   @GwtIncompatible("Not needed in emulated source")
     89   private static final long serialVersionUID = 0;
     90 }
     91