View Javadoc

1   /*
2    * Copyright (C) 2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21   */
22  package de.fu_berlin.ties.util;
23  
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.TreeMap;
28  import java.util.TreeSet;
29  
30  /***
31   * A {@link de.fu_berlin.ties.util.MultiValueMap} that sorts the values
32   * stored for each key, discarding duplicates. For this purpose,
33   * {@link java.util.TreeSet}s are used for inner collections.
34   *
35   * By default, the keys are sorted as well, by wrapping a
36   * {@link java.util.TreeMap}. This can be changed by specifying a different kind
37   * of map to wrap in the constructor.
38   *
39   * @author Christian Siefkes
40   * @version $Revision: 1.1 $, $Date: 2004/11/05 12:16:16 $, $Author: siefkes $
41   */
42  public class SortedMultiValueMap<K, V> extends MultiValueMap<K, V> {
43  
44      /***
45       * Creates a new instance, wrapping a {@link TreeMap}.
46       */
47      public SortedMultiValueMap() {
48          this(new TreeMap<K, Collection<V>>());
49      }
50  
51      /***
52       * Creates a new instance.
53       *
54       * @param wrappedMap wrapped map used as storage, e.g. a {@link HashMap} or
55       * a {@link java.util.TreeMap}
56       */
57      public SortedMultiValueMap(Map<K, Collection<V>> wrappedMap) {
58          super(wrappedMap);
59      }
60  
61      /***
62       * {@inheritDoc}
63       * This implementation returns a {@link TreeSet}.
64       */
65      protected Collection<V> createCollection(
66              final Collection<? extends V> coll) {
67          if (coll == null) {
68              return new TreeSet<V>();
69          } else {
70              return new TreeSet<V>(coll);
71          }
72      }
73  
74  }