OSDN Git Service

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