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 public FileCollectionFormatter(final Collection files) {
54 this.files = files;
55 filesPerDir = createFilesPerDirCount();
56 dirDepths = createDirDepths();
57 }
58
59 private IntegerMap createFilesPerDirCount() {
60 final IntegerMap result = new IntegerMap();
61 final Iterator it = files.iterator();
62 while (it.hasNext()) {
63 final String file = (String) it.next();
64 result.addInt(getDirectory(file), 1);
65 }
66 return result;
67 }
68
69 private IntegerMap createDirDepths() {
70 final IntegerMap result = new IntegerMap();
71 final Iterator it = filesPerDir.iteratorSortedByKey();
72 while (it.hasNext()) {
73 final String dir = (String) it.next();
74 result.put(dir, getDepth(dir));
75 }
76 return result;
77 }
78
79
80
81
82
83
84
85 public List getDirectories() {
86 final List result = new ArrayList();
87 final Iterator it = dirDepths.iteratorSortedByKey();
88 while (it.hasNext()) {
89 final String directory = (String) it.next();
90 result.add(directory);
91 }
92 return result;
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106 public List getFiles(final String directory) {
107 if (!dirDepths.contains(directory)) {
108 throw new NoSuchElementException("doesn't contain directory '" + directory + "'");
109 }
110 final List result = new ArrayList(getFilesInDir(directory));
111 Collections.sort(result);
112 final List allSubdirFiles = getFilesInSubdirs(directory);
113 Collections.sort(allSubdirFiles);
114 result.addAll(allSubdirFiles);
115 return result;
116 }
117
118 private List getFilesInSubdirs(final String directory) {
119 final List result = new ArrayList();
120 final Iterator it = files.iterator();
121 while (it.hasNext()) {
122 final String filename = (String) it.next();
123 if (isInDirectory(filename, directory) && !getDirectory(filename).equals(directory) && !isInDeeperDirectory(filename, directory)) {
124 result.add(getRelativeFilename(filename, directory));
125 }
126 }
127 return result;
128 }
129
130 private boolean isInDeeperDirectory(final String filename, final String directory) {
131 String currentDir = getDirectory(filename);
132 int currentDepth = getDepth(currentDir);
133 final int directoryDepth = getDepth(directory);
134 while (currentDepth > directoryDepth) {
135 if (dirDepths.contains(currentDir)) {
136 return true;
137 }
138 currentDepth--;
139 currentDir = getParent(currentDir);
140 }
141 return false;
142 }
143
144 private List getFilesInDir(final String directory) {
145 final List result = new ArrayList();
146 final Iterator it = files.iterator();
147 while (it.hasNext()) {
148 final String filename = (String) it.next();
149 if (getDirectory(filename).equals(directory)) {
150 result.add(getRelativeFilename(filename, directory));
151 }
152 }
153 return result;
154 }
155
156
157
158
159
160
161
162 protected static boolean isInDirectory(final String filename, final String directory) {
163 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 return filename.substring(dir.length());
174 }
175
176
177
178
179
180
181 protected static String getDirectory(final String filename) {
182 return filename.substring(0, filename.lastIndexOf("/") + 1);
183 }
184
185
186
187
188
189
190 protected static String getParent(final String directory) {
191 final int lastIndex = directory.lastIndexOf("/");
192 if (lastIndex == -1) {
193 return "";
194 }
195 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 int result = 0;
205 int index = 0;
206 while (directory.indexOf("/", index) != -1) {
207 index = directory.indexOf("/", index) + 1;
208 result++;
209 }
210 return result;
211 }
212 }