View Javadoc
1   
2   package eu.simuline.util;
3   
4   import java.util.Set;
5   import java.util.Map;
6   import java.util.HashMap;
7   
8   /**
9    * Represents a set with multiplicities based on a {@link HashMap}. 
10   * Mathematically this is something between a set and a family. 
11   * Note that this kind of set does support <code>null</code> elements. 
12   *
13   * @param <T>
14   *    the class of the elements of this multi-set. 
15   *
16   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
17   * @version 1.0
18   */
19  public final class HashMultiSet<T> 
20      extends AbstractMultiSet<Map<T, MultiSet.Multiplicity>, T> 
21      implements MultiSet<T> {
22  
23  
24      /* -------------------------------------------------------------------- *
25       * constructors.                                                        *
26       * -------------------------------------------------------------------- */
27  
28      private HashMultiSet(Map<T, Multiplicity> t2mult) {
29  	super(t2mult);
30      }
31  
32      /**
33       * Creates a new, empty <code>MultiSet</code>. 
34       */
35      public HashMultiSet() {
36  	this(new HashMap<T, Multiplicity>());
37      }
38  
39      /**
40       * Copy constructor. 
41       *
42       * @param other 
43       *    another <code>MultiSet</code> instance. 
44       */
45      public HashMultiSet(MultiSet<? extends T> other) {
46  	this(new HashMap<T, Multiplicity>(other.getMap()));
47      }
48  
49      /**
50       * Creates a multi set with the elements of <code>sSet</code> 
51       * and all elements with multiplicity <code>1</code>. 
52       *
53       * @param  sSet
54       *    some instance of a sorted set. 
55       */
56      public HashMultiSet(Set<? extends T> sSet) {
57  	this();
58  	addAll(sSet);
59      }
60  
61      /* -------------------------------------------------------------------- *
62       * methods.                                                             *
63       * -------------------------------------------------------------------- */
64  
65      /**
66       * Returns a view of the underlying set of this <code>MultiSet</code>. 
67       * For certain implementations, this set is immutable 
68       * to prevent implicit modification of this <code>MultiSet</code>. 
69       *
70       * @return 
71       *    the <code>Set</code> containing exactly the objects 
72       *    with strictly positive multiplicity in this <code>MultiSet</code>. 
73       */
74      public Set<T> getSet() {
75  	return this.obj2mult.keySet();
76      }
77  
78      /**
79       * Returns a view of the underlying map of this <code>MultiSet</code> 
80       * as a map mapping each entry to its multiplicity. 
81       */
82       public Map<T, Multiplicity> getMap() {
83  	return this.obj2mult;
84      }
85  
86      public String toString() {
87  	return "<MultiSet>" + this.obj2mult + "</MultiSet>";
88      }
89  
90  }