pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / MapOfSets.java
1 package org.apache.lucene.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20
21 import java.util.Set;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.Map;
25
26 /**
27  * Helper class for keeping Lists of Objects associated with keys. <b>WARNING: THIS CLASS IS NOT THREAD SAFE</b>
28  * @lucene.internal
29  */
30 public class MapOfSets<K, V> {
31
32   private final Map<K, Set<V>> theMap;
33
34   /**
35    * @param m the backing store for this object
36    */
37   public MapOfSets(Map<K, Set<V>> m) {
38     theMap = m;
39   }
40
41   /**
42    * @return direct access to the map backing this object.
43    */
44   public Map<K, Set<V>> getMap() {
45     return theMap;
46   }
47
48   /**
49    * Adds val to the Set associated with key in the Map.  If key is not 
50    * already in the map, a new Set will first be created.
51    * @return the size of the Set associated with key once val is added to it.
52    */
53   public int put(K key, V val) {
54     final Set<V> theSet;
55     if (theMap.containsKey(key)) {
56       theSet = theMap.get(key);
57     } else {
58       theSet = new HashSet<V>(23);
59       theMap.put(key, theSet);
60     }
61     theSet.add(val);
62     return theSet.size();
63   }
64    /**
65    * Adds multiple vals to the Set associated with key in the Map.  
66    * If key is not 
67    * already in the map, a new Set will first be created.
68    * @return the size of the Set associated with key once val is added to it.
69    */
70   public int putAll(K key, Collection<? extends V> vals) {
71     final Set<V> theSet;
72     if (theMap.containsKey(key)) {
73       theSet = theMap.get(key);
74     } else {
75       theSet = new HashSet<V>(23);
76       theMap.put(key, theSet);
77     }
78     theSet.addAll(vals);
79     return theSet.size();
80   }
81  
82 }