OSDN Git Service

01aedfc2871223dfcc6a8b52e509e632f9d212e2
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / vmd / model / NamedListMap.java
1 /*
2  * string named list map
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.vmd.model;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.LinkedList;
13 import java.util.List;
14 import java.util.Map;
15
16 /**
17  * 名前付けされたリストのマップ。
18  * 登録名の追加順が保持される。
19  * @param <E> リスト要素の型
20  */
21 public class NamedListMap<E> {
22
23     private final List<String> nameList;
24     private final Map<String, List<E>> listMap;
25
26
27     /**
28      * コンストラクタ。
29      */
30     public NamedListMap(){
31         super();
32         this.nameList = new LinkedList<String>();
33         this.listMap = new HashMap<String, List<E>>();
34         return;
35     }
36
37
38     /**
39      * マップをクリアする。
40      */
41     public void clear(){
42         this.nameList.clear();
43         this.listMap.clear();
44         return;
45     }
46
47     /**
48      * マップが空か否か判定する。
49      * @return 空ならtrue
50      */
51     public boolean isEmpty(){
52         if(this.listMap.isEmpty()) return true;
53         return false;
54     }
55
56     /**
57      * 名前一覧を返す。
58      * <p>名前は登録順に並ぶ。
59      * <p>ここで返されるListへの修正操作は不可能。
60      * @return 名前一覧のリスト
61      */
62     public List<String> getNames(){
63         List<String> result = Collections.unmodifiableList(this.nameList);
64         return result;
65     }
66
67     /**
68      * 名前付けされたリストを返す。
69      * @param name 名前
70      * @return 名前付けされたリスト。リストが存在しなければnull。
71      */
72     public List<E> getNamedList(String name){
73         List<E> result = this.listMap.get(name);
74         return result;
75     }
76
77     /**
78      * 名前付けされたリストを削除する。
79      * 存在しない名前が渡された場合、何もしない。
80      * @param name 名前
81      */
82     public void removeNamedList(String name){
83         if(this.listMap.remove(name) == null) return;
84         this.nameList.remove(name);
85         return;
86     }
87
88     /**
89      * 名前付けされたリストに新要素を追加する。
90      * 未登録の名前であれば新たにリストが作成される。
91      * @param name 名前
92      * @param elem 新要素
93      * @throws NullPointerException 引数がnull
94      */
95     public void addNamedElement(String name, E elem)
96             throws NullPointerException{
97         if(name == null || elem == null) throw new NullPointerException();
98
99         List<E> list = this.listMap.get(name);
100         if(list == null){
101             list = new LinkedList<E>();
102             this.listMap.put(name, list);
103             this.nameList.add(name);
104         }
105
106         list.add(elem);
107
108         return;
109     }
110
111 }