Project

General

Profile

« Previous | Next » 

Revision 53020

reimplemented businness logic for PathRetreiver

View differences:

PathRetreiver.java
2 2

  
3 3
import java.io.File;
4 4
import java.io.FileFilter;
5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.List;
5
import java.util.*;
9 6

  
10 7
import org.apache.commons.lang3.StringUtils;
11 8
import org.apache.commons.logging.Log;
12 9
import org.apache.commons.logging.LogFactory;
13 10

  
14
// TODO: Auto-generated Javadoc
15 11
/**
16 12
 * The Class PathRetreiver.
17 13
 */
......
41 37
	private String base_path;
42 38

  
43 39
	/** The values. */
44
	private List<InfoPath> values;
40
	private TreeMap<Integer, InfoPath> values;
45 41

  
46 42
	/**
47 43
	 * Bootstrap.
48 44
	 */
49 45
	private void bootstrap() {
50
		values = new ArrayList<InfoPath>();
46
		values = new TreeMap<>();
51 47
		File basePath = new File(this.base_path);
52
		File[] selectedFiles = basePath.listFiles(new FileFilter() {
48
		File[] selectedFiles = basePath.listFiles(pathname -> pathname.isDirectory());
53 49

  
54
			@Override
55
			public boolean accept(final File pathname) {
56
				return pathname.isDirectory();
57
			}
58
		});
59

  
60 50
		for (File f : selectedFiles) {
61 51
			String lower = StringUtils.substringAfter(StringUtils.substringBefore(f.getName(), "_"), "PMC");
62 52
			String upper = StringUtils.substringAfter(StringUtils.substringAfter(f.getName(), "_"), "PMC");
......
65 55
			i.setLower(Integer.parseInt(lower));
66 56
			i.setUpper(Integer.parseInt(upper));
67 57
			i.setPath(path);
68
			values.add(i);
58
			values.put(i.getLower(), i);
69 59
		}
70 60

  
71
		Collections.sort(values, new Comparator<InfoPath>() {
72 61

  
73
			@Override
74
			public int compare(final InfoPath o1, final InfoPath o2) {
75
				if (o1.getLower() < o2.getLower()) return -1;
76
				else if (o1.getLower() < o2.getLower()) return 0;
77
				else return 1;
78
			}
79
		});
80 62
		if (log.isDebugEnabled()) {
81
			for (InfoPath p : values) {
63
			for (InfoPath p : values.values()) {
82 64
				log.debug(String.format("%s -- %s : %s", p.getLower(), p.getUpper(), p.getPath()));
83 65
			}
84 66
		}
85

  
86 67
	}
87 68

  
88 69
	/**
......
96 77
		if (values == null) {
97 78
			bootstrap();
98 79
		}
99
		for (int i = 0; i < values.size(); i++) {
100
			if (pmcID < values.get(i).getLower()) {
101
				if (i == 0) return null;
102
				String currentPath = values.get(i - 1).getPath() + "/" + pmcID + ".xml";
103
				File f = new File(currentPath);
104
				log.debug(String.format("try to search in path %s", currentPath));
105
				String s = null;
106
				if (f.exists()) {
107
					s = f.getPath();
108
					log.debug(String.format("found in %s", s));
109
				} else {
110
					log.debug(String.format("not found in %s", s));
111
				}
112
				return s;
113 80

  
81
		Map.Entry<Integer, InfoPath> infoPath = values.floorEntry(pmcID);
82
		if (infoPath != null) {
83

  
84
			final String currentPath = infoPath.getValue().getPath() + "/" + pmcID + ".xml";
85
			final File f = new File(currentPath);
86
			log.debug(String.format("try to search in path %s", currentPath));
87
			String s = null;
88
			if (f.exists()) {
89
				s = f.getPath();
90
				log.debug(String.format("found in %s", s));
91
			} else {
92
				log.debug(String.format("not found in %s", s));
114 93
			}
94
			return s;
115 95
		}
116 96
		log.debug(String.format("PMC with ID: %s not found", pmcID));
117 97
		return null;

Also available in: Unified diff