/*
 * Copyright (c) 2002-2009 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 *  the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util.io;

import java.io.IOException;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
@SuppressWarnings("unchecked")
public class ComponentFile<T>
        implements DataFile<T> {

    private final ComponentReadableFile<T> readableFile;
    private final ComponentWritableFile<T> writableFile;

    public static <T> ComponentFile<T> fileFor(DataSource source, DataCodec<T> codec) {
        return new ComponentFile<T>(source, codec);
    }

    public ComponentFile(ComponentReadableFile<T> readableFile, ComponentWritableFile<T> writableFile) {
        assertNotNull(readableFile);
        this.readableFile = readableFile;

        assertNotNull(writableFile);
        this.writableFile = writableFile;
    }

    public ComponentFile(DataSource source, DataCodec<T> codec) {
        this(new ComponentReadableFile<T>(source, codec), new ComponentWritableFile<T>(source, codec));
    }

    public boolean writeAllowed() {
        return this.writableFile.writeAllowed();
    }

    public boolean readAllowed() {
        return this.readableFile.readAllowed();
    }

    public void write(T data)
            throws IOException {
        this.writableFile.write(data);
    }

    public T read()
            throws IOException {
        return this.readableFile.read();
    }

}