1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package net.sf.statcvs.renderer; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collection; |
27 | |
import java.util.Collections; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.NoSuchElementException; |
31 | |
|
32 | |
import net.sf.statcvs.util.IntegerMap; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class FileCollectionFormatter { |
43 | |
|
44 | |
private final Collection files; |
45 | |
private final IntegerMap filesPerDir; |
46 | |
private final IntegerMap dirDepths; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 64 | public FileCollectionFormatter(final Collection files) { |
54 | 64 | this.files = files; |
55 | 64 | filesPerDir = createFilesPerDirCount(); |
56 | 64 | dirDepths = createDirDepths(); |
57 | 64 | } |
58 | |
|
59 | |
private IntegerMap createFilesPerDirCount() { |
60 | 64 | final IntegerMap result = new IntegerMap(); |
61 | 64 | final Iterator it = files.iterator(); |
62 | 144 | while (it.hasNext()) { |
63 | 80 | final String file = (String) it.next(); |
64 | 80 | result.addInt(getDirectory(file), 1); |
65 | 70 | } |
66 | 64 | return result; |
67 | |
} |
68 | |
|
69 | |
private IntegerMap createDirDepths() { |
70 | 64 | final IntegerMap result = new IntegerMap(); |
71 | 64 | final Iterator it = filesPerDir.iteratorSortedByKey(); |
72 | 128 | while (it.hasNext()) { |
73 | 64 | final String dir = (String) it.next(); |
74 | 64 | result.put(dir, getDepth(dir)); |
75 | 56 | } |
76 | 64 | return result; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public List getDirectories() { |
86 | 48 | final List result = new ArrayList(); |
87 | 48 | final Iterator it = dirDepths.iteratorSortedByKey(); |
88 | 104 | while (it.hasNext()) { |
89 | 56 | final String directory = (String) it.next(); |
90 | 56 | result.add(directory); |
91 | 49 | } |
92 | 48 | return result; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public List getFiles(final String directory) { |
107 | 64 | if (!dirDepths.contains(directory)) { |
108 | 8 | throw new NoSuchElementException("doesn't contain directory '" + directory + "'"); |
109 | |
} |
110 | 56 | final List result = new ArrayList(getFilesInDir(directory)); |
111 | 56 | Collections.sort(result); |
112 | 56 | final List allSubdirFiles = getFilesInSubdirs(directory); |
113 | 56 | Collections.sort(allSubdirFiles); |
114 | 56 | result.addAll(allSubdirFiles); |
115 | 56 | return result; |
116 | |
} |
117 | |
|
118 | |
private List getFilesInSubdirs(final String directory) { |
119 | 56 | final List result = new ArrayList(); |
120 | 56 | final Iterator it = files.iterator(); |
121 | 168 | while (it.hasNext()) { |
122 | 112 | final String filename = (String) it.next(); |
123 | 112 | if (isInDirectory(filename, directory) && !getDirectory(filename).equals(directory) && !isInDeeperDirectory(filename, directory)) { |
124 | 0 | result.add(getRelativeFilename(filename, directory)); |
125 | |
} |
126 | 98 | } |
127 | 56 | return result; |
128 | |
} |
129 | |
|
130 | |
private boolean isInDeeperDirectory(final String filename, final String directory) { |
131 | 24 | String currentDir = getDirectory(filename); |
132 | 24 | int currentDepth = getDepth(currentDir); |
133 | 24 | final int directoryDepth = getDepth(directory); |
134 | 24 | while (currentDepth > directoryDepth) { |
135 | 24 | if (dirDepths.contains(currentDir)) { |
136 | 24 | return true; |
137 | |
} |
138 | 0 | currentDepth--; |
139 | 0 | currentDir = getParent(currentDir); |
140 | |
} |
141 | 0 | return false; |
142 | |
} |
143 | |
|
144 | |
private List getFilesInDir(final String directory) { |
145 | 56 | final List result = new ArrayList(); |
146 | 56 | final Iterator it = files.iterator(); |
147 | 168 | while (it.hasNext()) { |
148 | 112 | final String filename = (String) it.next(); |
149 | 112 | if (getDirectory(filename).equals(directory)) { |
150 | 72 | result.add(getRelativeFilename(filename, directory)); |
151 | |
} |
152 | 98 | } |
153 | 56 | return result; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
protected static boolean isInDirectory(final String filename, final String directory) { |
163 | 112 | return getDirectory(filename).startsWith(directory); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
protected static String getRelativeFilename(final String filename, final String dir) { |
173 | 128 | return filename.substring(dir.length()); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
protected static String getDirectory(final String filename) { |
182 | 448 | return filename.substring(0, filename.lastIndexOf("/") + 1); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected static String getParent(final String directory) { |
191 | 24 | final int lastIndex = directory.lastIndexOf("/"); |
192 | 24 | if (lastIndex == -1) { |
193 | 8 | return ""; |
194 | |
} |
195 | 16 | return directory.substring(0, directory.lastIndexOf("/", lastIndex - 1) + 1); |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
protected static int getDepth(final String directory) { |
204 | 136 | int result = 0; |
205 | 136 | int index = 0; |
206 | 248 | while (directory.indexOf("/", index) != -1) { |
207 | 112 | index = directory.indexOf("/", index) + 1; |
208 | 112 | result++; |
209 | |
} |
210 | 136 | return result; |
211 | |
} |
212 | |
} |