001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.file;
017
018import static com.google.common.collect.Lists.transform;
019import static org.kuali.common.util.base.Exceptions.illegalState;
020import static org.kuali.common.util.base.Precondition.checkIsDir;
021import static org.kuali.common.util.base.Precondition.checkNotBlank;
022import static org.kuali.common.util.base.Precondition.checkNotNull;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.List;
027
028import com.google.common.base.Function;
029import com.google.common.collect.ImmutableList;
030import com.google.common.collect.Ordering;
031
032/**
033 * @deprecated
034 */
035@Deprecated
036public final class Files {
037
038        public static long sumFileLengths(Iterable<File> files) {
039                long sum = 0;
040                for (File file : files) {
041                        sum += file.length();
042                }
043                return sum;
044        }
045
046        public static List<File> canonicalList(File dir) {
047                return transform(listFiles(checkIsDir(dir, "dir")), canonical());
048        }
049
050        public static File currentWorkingDirectory() {
051                return getCanonicalFile(".");
052        }
053
054        public static Function<File, File> canonical() {
055                return FileFunction.CANONICAL;
056        }
057
058        public static Function<File, Long> length() {
059                return LongFunction.LENGTH;
060        }
061
062        public static Function<File, Long> lastModified() {
063                return LongFunction.LAST_MODIFIED;
064        }
065
066        public static File getCanonicalFile(File file) {
067                return canonical().apply(file);
068        }
069
070        public static File getCanonicalFile(String path) {
071                return getCanonicalFile(new File(checkNotBlank(path, "path")));
072        }
073
074        public static File getCanonicalFile(File parent, String child) {
075                return getCanonicalFile(new File(checkNotNull(parent, "parent"), checkNotBlank(child, "child")));
076        }
077
078        /**
079         * Return an immutable list of files from <code>dir</code>, sorted naturally
080         * 
081         * @throws IllegalArgumentException
082         *             if dir is null, does not exist, or exists but is not a directory
083         */
084        public static List<File> listFiles(File dir) {
085                return listFiles(dir, Ordering.<File> natural());
086        }
087
088        /**
089         * Return an immutable list of files from <code>dir</code>, sorted by size
090         * 
091         * @throws IllegalArgumentException
092         *             if dir is null, does not exist, or exists but is not a directory
093         */
094        public static List<File> listFilesBySize(File dir) {
095                return listFiles(dir, Ordering.natural().onResultOf(length()));
096        }
097
098        /**
099         * 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}