View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @deprecated
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  	 * Return an immutable list of files from <code>dir</code>, sorted naturally
80  	 * 
81  	 * @throws IllegalArgumentException
82  	 *             if dir is null, does not exist, or exists but is not a directory
83  	 */
84  	public static List<File> listFiles(File dir) {
85  		return listFiles(dir, Ordering.<File> natural());
86  	}
87  
88  	/**
89  	 * Return an immutable list of files from <code>dir</code>, sorted by size
90  	 * 
91  	 * @throws IllegalArgumentException
92  	 *             if dir is null, does not exist, or exists but is not a directory
93  	 */
94  	public static List<File> listFilesBySize(File dir) {
95  		return listFiles(dir, Ordering.natural().onResultOf(length()));
96  	}
97  
98  	/**
99  	 * Return an immutable list of files from <code>dir</code>, sorted by last modified timestamp
100 	 * 
101 	 * @throws IllegalArgumentException
102 	 *             if dir is null, does not exist, or exists but is not a directory
103 	 */
104 	public static List<File> listFilesByLastModified(File dir) {
105 		return listFiles(dir, Ordering.natural().onResultOf(lastModified()));
106 	}
107 
108 	/**
109 	 * Return an immutable list of files from <code>dir</code>, sorted by <code>ordering</code>
110 	 * 
111 	 * @throws IllegalArgumentException
112 	 *             if dir is null, does not exist, or exists but is not a directory
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 }