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 java.io.File;
019import java.io.IOException;
020import java.net.URI;
021
022/**
023 * A <code>CanonicalFile</code> is always both absolute and unique.
024 */
025public final class CanonicalFile extends File {
026
027        private static final long serialVersionUID = -8366640724070158688L;
028
029        /**
030         * A <code>CanonicalFile</code> is always both absolute and unique.
031         */
032        public CanonicalFile(File parent, String child) {
033                this(new File(parent, child));
034        }
035
036        /**
037         * A <code>CanonicalFile</code> is always both absolute and unique.
038         */
039        public CanonicalFile(String parent, String child) {
040                this(new File(parent, child));
041        }
042
043        /**
044         * A <code>CanonicalFile</code> is always both absolute and unique.
045         */
046        public CanonicalFile(URI uri) {
047                this(new File(uri));
048        }
049
050        /**
051         * A <code>CanonicalFile</code> is always both absolute and unique.
052         */
053        public CanonicalFile(String path) {
054                this(new File(path));
055        }
056
057        /**
058         * A <code>CanonicalFile</code> is always both absolute and unique.
059         */
060        public CanonicalFile(File file) {
061                super(getCanonicalPath(file));
062        }
063
064        /**
065         * Return the current working directory.
066         */
067        public static final CanonicalFile cwd() {
068                return new CanonicalFile(".");
069        }
070
071        protected static String getCanonicalPath(File file) {
072                try {
073                        return file.getCanonicalPath();
074                } catch (IOException e) {
075                        throw new IllegalStateException("unexpected io error", e);
076                }
077        }
078
079}