View Javadoc

1   /*
2    * Copyright (C) 2004-2006 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 program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program 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
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties.util;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  import java.util.TreeMap;
27  import java.util.TreeSet;
28  
29  /***
30   * A {@link de.fu_berlin.ties.util.MultiValueMap} that sorts the values
31   * stored for each key, discarding duplicates. For this purpose,
32   * {@link java.util.TreeSet}s are used for inner collections.
33   *
34   * By default, the keys are sorted as well, by wrapping a
35   * {@link java.util.TreeMap}. This can be changed by specifying a different kind
36   * of map to wrap in the constructor.
37   *
38   * @author Christian Siefkes
39   * @version $Revision: 1.5 $, $Date: 2006/10/21 16:04:27 $, $Author: siefkes $
40   * @param <K> the type of keys
41   * @param <V> the type of values
42   */
43  public class SortedMultiValueMap<K, V> extends MultiValueMap<K, V> {
44  
45      /***
46       * Creates a new instance, wrapping a {@link TreeMap}.
47       */
48      public SortedMultiValueMap() {
49          this(new TreeMap<K, Collection<V>>());
50      }
51  
52      /***
53       * Creates a new instance.
54       *
55       * @param wrappedMap wrapped map used as storage, e.g. a
56       * {@link java.util.HashMap} or a {@link java.util.TreeMap}
57       */
58      public SortedMultiValueMap(final Map<K, Collection<V>> wrappedMap) {
59          super(wrappedMap);
60      }
61  
62      /***
63       * {@inheritDoc}
64       * This implementation returns a {@link TreeSet}.
65       */
66      protected Collection<V> createCollection(
67              final Collection<? extends V> coll) {
68          if (coll == null) {
69              return new TreeSet<V>();
70          } else {
71              return new TreeSet<V>(coll);
72          }
73      }
74  
75  }