package source;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Ini{
private static final String JAVAEXTC = ".java";
private static String pathname = "F:\\source";
private static HashMap<String,HashMap<String ,String >> map = new HashMap<String, HashMap<String,String>>();
private static ThreadPoolExecutor exec = new ThreadPoolExecutor(5, 10, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) throws IOException, InterruptedException {
init();
System.out.println(getByFullName("Set"));
}
public static Result getByName(String name) {
String key = name+JAVAEXTC;
if (map.containsKey(key)){
HashMap<String,String> hashMap = map.get(key);
if (hashMap.size() != 1){
Set<String> keySet = hashMap.keySet();
return new Result(keySet);
}
return new Result(new ArrayList<String>(hashMap.values()).get(0));
}
return null;
}
public static Result getByFullName(String className) {
String key = className.substring(className.lastIndexOf(".")+1);
if (map.containsKey(key+JAVAEXTC)){
HashMap<String,String> hashMap = map.get(key+JAVAEXTC);
if (hashMap.containsKey(className+JAVAEXTC)){
return new Result(hashMap.get(className+JAVAEXTC));
}
}
return null;
}
public static void init() throws IOException{
/**
* 加载
*/
File sourceDir = new File(pathname);
if (sourceDir.exists() && sourceDir.isDirectory()){
searchJava(sourceDir);
}
exec.shutdown();
}
private static void searchJava(File sourceDir) throws IOException {
File[] files = sourceDir.listFiles();
for (File file : files) {
if (file.isFile()){
String name = file.getName();
if (name.endsWith(JAVAEXTC)){
String className = file.getAbsolutePath().substring(pathname.length()+1).replaceAll("\\\\", ".");
exec.execute(new LoadJava(file, name, className));
}
} else {
searchJava(file);
}
}
}
static class Result{
private boolean single ;
private Set<String> set;
private String content;
public boolean isSingle() {
return single;
}
public void setSingle(boolean single) {
this.single = single;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Result() {
super();
// TODO Auto-generated constructor stub
}
public Result(Set<String> set) {
super();
this.set = set;
}
public Result(String content) {
super();
this.content = content;
this.single = true;
}
@Override
public String toString() {
return "Result [single=" + single + ", set=" + set + ", content=" + content + "]";
}
}
static class LoadJava implements Runnable{
private File file;
private String name;
private String className;
public LoadJava(File file, String name, String className) {
super();
this.file = file;
this.name = name;
this.className = className;
}
@Override
public void run() {
String content = this.getJAVAContent();
HashMap<String,String> hashMap = null;
synchronized (name) {
if(map.containsKey(name)){
hashMap = map.get(name);
} else {
hashMap = new HashMap<String, String>();
}
hashMap.put(className, content);
map.put(name, hashMap);
}
}
private String getJAVAContent() {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
String line;
StringBuffer sb = new StringBuffer();
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\r\n");
}
br.close();
return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}