OSDN Git Service

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