OSDN Git Service

指定したキーワードに関連するハッシュタグを取得する機能の基礎部分が完成.GUI部分はまだ作成していない.
[nt-manager/nt-manager.git] / src / twitter / util / MultiSortedMap.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package twitter.util;
7
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.Set;
12 import java.util.SortedMap;
13 import java.util.SortedSet;
14 import java.util.TreeMap;
15 import java.util.TreeSet;
16
17 /**
18  * sort済みマルチマップ
19  * @author nishio
20  * @param <T>
21  * @param <V>
22  */
23 public class MultiSortedMap<T, V> {
24
25         private SortedMap<T,SortedSet<V>> mmap;
26
27     public MultiSortedMap() {
28         mmap = Collections.synchronizedSortedMap( new TreeMap<T,SortedSet<V>>() );
29     }
30
31     public MultiSortedMap(Comparator<? super T> cmprtr) {
32         mmap = Collections.synchronizedSortedMap( new TreeMap<T,SortedSet<V>>(cmprtr) );
33     }
34
35         /**
36          *
37          * @param key
38          * @param value
39          */
40         public void add(T key, V value){
41                 SortedSet<V> mapValue = null;
42
43                 if(mmap.containsKey(key)) {
44                         mapValue = mmap.get(key);
45                 } else {
46                         mapValue = Collections.synchronizedSortedSet( new TreeSet<V>() );
47                 }
48
49                 mapValue.add(value);
50                 mmap.put(key, mapValue);
51     }
52
53         /**
54          *
55          * @param key
56          * @return
57          */
58         public SortedSet<V> get(T key){
59                 if( mmap == null ) {
60                         return null;
61                 }
62                 SortedSet<V> s = new TreeSet<V>();
63                 s = mmap.get(key);
64
65                 return s;
66         }
67
68         /**
69          *
70          * @return
71          */
72         public Set<T> getKeys() {
73                 if( mmap == null ) {
74                         return null;
75                 }
76                 return mmap.keySet();
77         }
78
79         /**
80          *
81          * @return
82          */
83         public Collection<SortedSet<V>> getValues() {
84                 if( mmap == null ) {
85                         return null;
86                 }
87                 return mmap.values();
88         }
89
90         /**
91          * 登録されているキーの数を返す
92          * @return
93          */
94         public int size() {
95                 return mmap.size();
96         }
97
98         /**
99          *
100          * @param key
101          * @param value
102          * @return
103          */
104         public boolean contains(T key, V value) {
105                 Set<V> t = get( key );
106                 if( t != null ) {
107                         return t.contains(value);
108                 }
109                 return false;
110         }
111
112         /**
113          *
114          * @param key
115          * @return
116          */
117         public boolean containsKey(T key) {
118                 return mmap.containsKey(key);
119         }
120
121         /**
122          *
123          * @param key
124          */
125         public SortedSet<V> remove(T key) {
126                 return mmap.remove(key);
127         }
128
129         /**
130          *
131          * @param key
132          * @param value
133          * @return
134          */
135         public boolean removeValue(T key, V value) {
136                 Set<V> t = get( key );
137                 if( t != null ) {
138                         return t.remove(value);
139                 }
140                 return false;
141         }
142 }
143