1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.file;
17
18 import static com.google.common.collect.Lists.transform;
19 import static org.kuali.common.util.base.Exceptions.illegalState;
20 import static org.kuali.common.util.base.Precondition.checkIsDir;
21 import static org.kuali.common.util.base.Precondition.checkNotBlank;
22 import static org.kuali.common.util.base.Precondition.checkNotNull;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.List;
27
28 import com.google.common.base.Function;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.Ordering;
31
32
33
34
35 @Deprecated
36 public final class Files {
37
38 public static long sumFileLengths(Iterable<File> files) {
39 long sum = 0;
40 for (File file : files) {
41 sum += file.length();
42 }
43 return sum;
44 }
45
46 public static List<File> canonicalList(File dir) {
47 return transform(listFiles(checkIsDir(dir, "dir")), canonical());
48 }
49
50 public static File currentWorkingDirectory() {
51 return getCanonicalFile(".");
52 }
53
54 public static Function<File, File> canonical() {
55 return FileFunction.CANONICAL;
56 }
57
58 public static Function<File, Long> length() {
59 return LongFunction.LENGTH;
60 }
61
62 public static Function<File, Long> lastModified() {
63 return LongFunction.LAST_MODIFIED;
64 }
65
66 public static File getCanonicalFile(File file) {
67 return canonical().apply(file);
68 }
69
70 public static File getCanonicalFile(String path) {
71 return getCanonicalFile(new File(checkNotBlank(path, "path")));
72 }
73
74 public static File getCanonicalFile(File parent, String child) {
75 return getCanonicalFile(new File(checkNotNull(parent, "parent"), checkNotBlank(child, "child")));
76 }
77
78
79
80
81
82
83
84 public static List<File> listFiles(File dir) {
85 return listFiles(dir, Ordering.<File> natural());
86 }
87
88
89
90
91
92
93
94 public static List<File> listFilesBySize(File dir) {
95 return listFiles(dir, Ordering.natural().onResultOf(length()));
96 }
97
98
99
100
101
102
103
104 public static List<File> listFilesByLastModified(File dir) {
105 return listFiles(dir, Ordering.natural().onResultOf(lastModified()));
106 }
107
108
109
110
111
112
113
114 public static List<File> listFiles(File dir, Ordering<File> ordering) {
115 checkIsDir(dir, "dir");
116 return ordering.immutableSortedCopy(ImmutableList.copyOf(dir.listFiles()));
117 }
118
119 private enum FileFunction implements Function<File, File> {
120 CANONICAL {
121 @Override
122 public File apply(File file) {
123 try {
124 return file.getCanonicalFile();
125 } catch (IOException e) {
126 throw illegalState(e);
127 }
128 }
129 },
130 PARENT {
131 @Override
132 public File apply(File file) {
133 return file.getParentFile();
134 }
135 },
136 ABSOLUTE {
137 @Override
138 public File apply(File file) {
139 return file.getAbsoluteFile();
140 }
141 }
142 }
143
144 private enum LongFunction implements Function<File, Long> {
145 LENGTH {
146 @Override
147 public Long apply(File file) {
148 return file.length();
149 }
150 },
151 LAST_MODIFIED {
152 @Override
153 public Long apply(File file) {
154 return file.lastModified();
155 }
156 },
157 TOTAL_SPACE {
158 @Override
159 public Long apply(File file) {
160 return file.getTotalSpace();
161 }
162 },
163 USABLE_SPACE {
164 @Override
165 public Long apply(File file) {
166 return file.getUsableSpace();
167 }
168 },
169 FREE_SPACE {
170 @Override
171 public Long apply(File file) {
172 return file.getFreeSpace();
173 }
174 }
175 }
176
177 }