001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.archive.bitarchive;
025
026import java.io.File;
027
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029
030/**
031 * The representation of an ARC file in the bit archive. This class keeps the connection between the name that was used
032 * for lookup and the file that was found.
033 */
034public class BitarchiveARCFile {
035    /** The ARC file name (with no path). */
036    private String fileName;
037    /** The path of the file in the archive. */
038    private File filePath;
039
040    /**
041     * Create a new representation of a file in the archive. Note that <code>fn</code> is not necessarily, though
042     * probably, the same as <code>fp.getName()</code>.
043     * <p>
044     * Failed lookups should be represented by null references rather than an object representing something that doesn't
045     * exist.
046     *
047     * @param fn The ARC name of the file, as used in lookup in the archive.
048     * @param fp The actual path of the file in the archive.
049     * @throws ArgumentNotValid if either argument is null, or any of the file name representaitons is the empty string.
050     */
051    public BitarchiveARCFile(String fn, File fp) throws ArgumentNotValid {
052        ArgumentNotValid.checkNotNull(fp, "File fp");
053        if (fp.getName().isEmpty()) {
054            throw new ArgumentNotValid("fp denotes an empty filename");
055        }
056        ArgumentNotValid.checkNotNullOrEmpty(fn, "String fn");
057        fileName = fn;
058        filePath = fp;
059    }
060
061    /**
062     * Return true if the file exists, false otherwise. Note that failure to exist indicates a severe error in the bit
063     * archive, not just that the lookup failed.
064     *
065     * @return Whether the file exists
066     */
067    public boolean exists() {
068        return filePath.exists();
069    }
070
071    /**
072     * Get the ARC name of this file. This is the name that the file can be found under when looking up in the bit
073     * archive.
074     *
075     * @return A String representing the ARC name of this file.
076     */
077    public String getName() {
078        return fileName;
079    }
080
081    /**
082     * Get the full file path of this file.
083     *
084     * @return A path where this file can be found in the bit archive.
085     */
086    public File getFilePath() {
087        return filePath;
088    }
089
090    /**
091     * Get the size of this file. If the file does not exist, the size is 0L.
092     *
093     * @return The size of this file.
094     */
095    public long getSize() {
096        return (filePath.length());
097    }
098}