OSDN Git Service

update command line interface, and introduce command pattern in Main class
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / BirthmarkContext.java
1 package jp.sourceforge.stigmata;
2
3 /*
4  * $Id$
5  */
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13
14 /**
15  * Birthmark runtime context.
16  * 
17  * @author Haruaki Tamada
18  * @version $Revision$ 
19  */
20 public class BirthmarkContext{
21     private BirthmarkEnvironment environment;
22
23     private ComparisonMethod method = ComparisonMethod.ROUND_ROBIN_SAME_PAIR;
24     private ExtractionUnit unit = ExtractionUnit.CLASS;
25     private BirthmarkStoreTarget store = BirthmarkStoreTarget.MEMORY;
26
27     private List<String> birthmarkTypes = new ArrayList<String>();
28     private List<String> filterTypes = new ArrayList<String>();
29     private Map<String, String> nameMappings = new HashMap<String, String>();
30     private String format;
31     /**
32      * properties which available on a session.
33      */
34     private Map<String, Object> properties = new HashMap<String, Object>();
35
36     /**
37      * self constructor.
38      */
39     public BirthmarkContext(BirthmarkContext context){
40         this.environment = context.getEnvironment();
41         this.method = context.getComparisonMethod();
42         this.unit = context.getExtractionUnit();
43         this.birthmarkTypes = new ArrayList<String>(context.birthmarkTypes);
44         this.filterTypes = new ArrayList<String>(context.filterTypes);
45         this.nameMappings = new HashMap<String, String>(context.nameMappings);
46     }
47
48     public BirthmarkContext(BirthmarkEnvironment environment){
49         this.environment = environment;
50     }
51
52     public BirthmarkEnvironment getEnvironment(){
53         return environment;
54     }
55
56     public boolean hasNameMapping(){
57         return getNameMappingCount() > 0;
58     }
59
60     public int getNameMappingCount(){
61         return nameMappings.size();
62     }
63
64     public String getNameMapping(String key){
65         return nameMappings.get(key);
66     }
67
68     public void addNameMapping(String name1, String name2){
69         nameMappings.put(name1, name2);
70     }
71
72     public void removeNameMapping(String name1){
73         nameMappings.remove(name1);
74     }
75
76     public Map<String, String> getNameMappings(){
77         return Collections.unmodifiableMap(nameMappings);
78     }
79
80     public Iterator<Map.Entry<String, String>> nameMappingEntries(){
81         return getNameMappings().entrySet().iterator();
82     }
83
84     public void setNameMappings(Map<String, String> mappings){
85         nameMappings.clear();
86         for(Iterator<Map.Entry<String, String>> i = mappings.entrySet().iterator(); i.hasNext(); ){
87             Map.Entry<String, String> entry = i.next();
88             addNameMapping(entry.getKey(), entry.getValue());
89         }
90     }
91
92     public void setBirthmarkTypes(String[] types){
93         birthmarkTypes.clear();
94         for(int i = 0; i < types.length; i++){
95             addBirthmarkType(types[i]);
96         }
97     }
98
99     public void addBirthmarkType(String type){
100         if(!birthmarkTypes.contains(type)){
101             birthmarkTypes.add(type);
102         }
103     }
104
105     public void removeBirthmarkType(String type){
106         birthmarkTypes.remove(type);
107     }
108
109     public String getFormat(){
110         return format;
111     }
112
113     public void setFormat(String format){
114         this.format = format;
115     }
116
117     public synchronized String[] getBirthmarkTypes(){
118         return birthmarkTypes.toArray(new String[getBirthmarkTypeSize()]);
119     }
120
121     public int getBirthmarkTypeSize(){
122         return birthmarkTypes.size();
123     }
124
125     public ComparisonMethod getComparisonMethod(){
126         return method;
127     }
128
129     public void setComparisonMethod(ComparisonMethod method){
130         this.method = method;
131     }
132
133     public ExtractionUnit getExtractionUnit(){
134         return unit;
135     }
136
137     public void setExtractionUnit(ExtractionUnit unit){
138         this.unit = unit;
139     }
140
141     public BirthmarkStoreTarget getStoreTarget(){
142         return store;
143     }
144
145     public void setStoreTarget(BirthmarkStoreTarget store){
146         this.store = store;
147     }
148
149     public boolean hasFilter(){
150         return filterTypes.size() > 0;
151     }
152
153     public void setFilterTypes(String[] filterTypes){
154         if(filterTypes != null){
155             for(int i = 0; i < filterTypes.length; i++){
156                 addFilterType(filterTypes[i]);
157             }
158         }
159     }
160
161     public void addFilterType(String filterType){
162         if(filterType != null){
163             filterTypes.add(filterType);
164         }
165     }
166
167     public void removeFilterType(String filterType){
168         filterTypes.remove(filterType);
169     }
170
171     public synchronized String[] getFilterTypes(){
172         return filterTypes.toArray(new String[getFilterTypesCount()]);
173     }
174
175     public Iterator<String> filterTypes(){
176         return filterTypes.iterator();
177     }
178
179     public int getFilterTypesCount(){
180         return filterTypes.size();
181     }
182
183     public Object getProperty(String key){
184         return getProperty(key, null);
185     }
186
187     public Object getProperty(String key, Object defaultValue){
188         Object value = properties.get(key);
189         if(value == null){
190             value = defaultValue;
191         }
192         return value;
193     }
194
195     public void removeProperty(String key){
196         properties.remove(key);
197     }
198
199     public void putProperty(String key, Object value){
200         properties.put(key, value);
201     }
202 }