package com.srbenoit.filter;

import java.io.File;

/**
 * Information on a file used to persist part of a pipe item.
 */
public class PipeItemFileInfo {

    /** the file */
    private File file;

    /** true if persisted; false if not */
    private boolean persisted;

    /** the file size */
    private long size;

    /** the file CRC */
    private String crc;

    /**
     * Constructs a new <code>PipeItemFileInfo</code> representing an unpersisted file.
     *
     * @param  theFile  the file
     */
    public PipeItemFileInfo(final File theFile) {

        this.file = theFile;
        this.persisted = false;
        this.size = 0;
        this.crc = null;
    }

    /**
     * Gets the file.
     *
     * @return  the file
     */
    public File getFile() {

        return this.file;
    }

    /**
     * Sets the file.
     *
     * @param  newFile  the new file
     */
    public void setFile(final File newFile) {

        this.file = newFile;
    }

    /**
     * Tests whether the file has been persisted.
     *
     * @return  <code>true</code> if the file is persisted; <code>false</code> if not
     */
    public boolean isPersisted() {

        return this.persisted;
    }

    /**
     * Gets the size of the persisted file.
     *
     * @return  the file size
     */
    public long getSize() {

        return this.size;
    }

    /**
     * Gets the CRC of the persisted file, expressed as a <code>String</code> (this is to
     * facilitate its use in XML files, where comparisons can be done without parsing the numerical
     * value from the XML).
     *
     * @return  the file CRC
     */
    public String getCrc() {

        return this.crc;
    }

    /**
     * Called after the file is actually persisted, this computes the size and CRC and sets the
     * persisted flag.
     */
    public void wasPersisted() {

        if (this.file.exists()) {
            this.size = this.file.length();
            this.crc = Pipe.crcFile(this.file);
            this.persisted = true;
        }
    }

    /**
     * Indicates that the file is not persisted.
     */
    public void notPersisted() {

        this.size = 0;
        this.crc = null;
        this.persisted = false;
    }
}
