OSDN Git Service

b2ddf0767cd052e94327a625e2a9b00d2fd0276a
[stigmata/stigmata-core.git] / src / main / java / jp / sourceforge / stigmata / BirthmarkSet.java
1 package jp.sourceforge.stigmata;
2
3 /*
4  * $Id$
5  */
6
7 import java.net.URL;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.Map;
11
12 /**
13  * This class manages a set of birthmarks which extracted from a target.
14  *
15  * @author  Haruaki TAMADA
16  * @version  $Revision$ 
17  */
18 public class BirthmarkSet implements Iterable<Birthmark>{
19     /**
20      * this object name.
21      */
22     private String name;
23
24     /**
25      * location of target is loaded from.
26      */
27     private URL location;
28
29     /**
30      * map for birthmarks.
31      */
32     private Map<String, Birthmark> birthmarks = new HashMap<String, Birthmark>();
33
34     /**
35      * constructor.
36      */
37     public BirthmarkSet(String name, URL location){
38         this.name = name;
39         this.location = location;
40     }
41
42     /**
43      * return the sum of all element count of birthmarks this instance has.
44      */
45     public int getSumOfElementCount(){
46         int count = 0;
47         for(Iterator<String> i = birthmarkTypes(); i.hasNext();){
48             Birthmark birthmark = getBirthmark(i.next());
49             count += birthmark.getElementCount();
50         }
51         return count;
52     }
53
54     /**
55      * return the number of birthmarks.
56      */
57     public int getBirthmarksCount(){
58         return birthmarks.size();
59     }
60
61     /**
62      * return the name.
63      */
64     public String getName(){
65         return name;
66     }
67
68     /**
69      * return the location.
70      */
71     public URL getLocation(){
72         return location;
73     }
74
75     /**
76      * add given birthmark to this instance.
77      * @throws NullPointerException given birthmark is null.
78      */
79     public void addBirthmark(Birthmark birthmark){
80         if(birthmark == null){
81             throw new NullPointerException("given birthmark is null");
82         }
83         birthmarks.put(birthmark.getType(), birthmark);
84     }
85
86     /**
87      * return the given type of birthmark.
88      */
89     public Birthmark getBirthmark(String type){
90         return birthmarks.get(type);
91     }
92
93     /**
94      * does this object have the given birthmark type.
95      */
96     public boolean hasBirthmark(String type){
97         return birthmarks.get(type) != null;
98     }
99
100     /**
101      * returns an array containing all of the birthmarks in this object.
102      */
103     public Birthmark[] getBirthmarks(){
104         Birthmark[] b = new Birthmark[getBirthmarksCount()];
105         int index = 0;
106         for(Iterator<String> i = birthmarkTypes(); i.hasNext();){
107             b[index] = getBirthmark(i.next());
108             index++;
109         }
110         return b;
111     }
112
113     /**
114      * returns an iterator over the birthmarks in this birthmark-set.
115      */
116     @Override
117     public Iterator<Birthmark> iterator(){
118         return birthmarks.values().iterator();
119     }
120
121     /**
122      * returns an iterator over the birthmark types.
123      */
124     public Iterator<String> birthmarkTypes(){
125         return birthmarks.keySet().iterator();
126     }
127
128     /**
129      * returns an array of birthmark types.
130      */
131     public synchronized String[] getBirthmarkTypes(){
132         return birthmarks.keySet().toArray(new String[birthmarks.size()]);
133     }
134 }