OSDN Git Service

update initialization process. make not existing history directory
[stigmata/stigmata-core.git] / src / main / java / jp / naist / se / stigmata / result / history / XmlFileExtractedBirthmarkHistory.java
1 package jp.naist.se.stigmata.result.history;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
8 import java.util.Iterator;
9 import java.util.LinkedHashMap;
10 import java.util.Map;
11
12 import jp.naist.se.stigmata.BirthmarkEnvironment;
13 import jp.naist.se.stigmata.ExtractionResultSet;
14 import jp.naist.se.stigmata.result.XmlFileExtractionResultSet;
15 import jp.naist.se.stigmata.utils.Utility;
16
17 /**
18  * 
19  * @author Haruaki Tamada
20  * @version $Revision$ $Date$
21  */
22 public class XmlFileExtractedBirthmarkHistory implements ExtractedBirthmarkHistory{
23     private File parent;
24     private Map<String, File> files = new LinkedHashMap<String, File>();
25
26     public XmlFileExtractedBirthmarkHistory(){
27         this(new File(BirthmarkEnvironment.getStigmataHome(), "extracted_birthmarks"));
28     }
29
30     public XmlFileExtractedBirthmarkHistory(String path){
31         this(new File(path));
32     }
33
34     public XmlFileExtractedBirthmarkHistory(File parent){
35         this.parent = parent;
36
37         refresh();
38     }
39
40     public void refresh(){
41         files.clear();
42
43         if(parent.exists()){
44             for(File file: parent.listFiles()){
45                 if(isTarget(file)){
46                     files.put(file.getName(), file);
47                 }
48             }
49         }
50     }
51
52     public void deleteAllResultSets(){
53         for(File file: parent.listFiles()){
54             if(isTarget(file)){
55                 Utility.deleteDirectory(file);
56             }
57         }
58     }
59
60     public void deleteResultSet(String id){
61         File file = files.get(id);
62         if(file != null){
63             if(file.isDirectory()){
64                 Utility.deleteDirectory(file);
65             }
66             else{
67                 file.delete();
68             }
69         }
70     }
71
72     public ExtractionResultSet getResultSet(String id){
73         File file = files.get(id);
74         if(file != null){
75             return new XmlFileExtractionResultSet(file);
76         }
77         return null;
78     }
79
80     public synchronized String[] getResultSetIds(){
81         return files.keySet().toArray(new String[files.size()]);
82     }
83
84     public Iterator<String> iterator(){
85         return files.keySet().iterator();
86     }
87
88     private boolean isTarget(File file){
89         return file.isDirectory()
90         && file.getName().matches("\\d\\d\\d\\d\\d\\d\\d\\d-\\d\\d\\d\\d\\d\\d.\\d\\d\\d"); 
91     }
92 }