package com.atlassian.stash.internal.backup;

import com.google.common.base.Function;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A simple {@link Backup} implementation wrapping a {@code File}.
 *
 * @since 2.1
 */
public class FileBackup implements Backup {

    public static final Function<File, Backup> FILE_TRANSFORM = new Function<File, Backup>() {

        @Override
        public FileBackup apply(File file) {
            return new FileBackup(file);
        }
    };

    private final File file;

    public FileBackup(@Nonnull File file) {
        this.file = checkNotNull(file, "file");
    }

    @Override
    public long getModified() {
        return file.lastModified();
    }

    @Nonnull
    @Override
    public String getName() {
        return file.getName();
    }

    @Override
    public long getSize() {
        return file.length();
    }

    @Nonnull
    @Override
    public InputStream getInput() throws IOException {
        return new FileInputStream(file);
    }
}
