package hep.aida.ref.tree;

import java.io.*;

import hep.aida.*;

/**
 *
 * @author  The AIDA team @ SLAC.
 *
 * @version $Id: TreeFactory.java,v 1.11 2005/02/14 23:29:42 serbo Exp $
 */
public class TreeFactory implements ITreeFactory {
    
    private IAnalysisFactory analysisFactory;

    /**
     * The default constructor.
     *
     */
    public TreeFactory(IAnalysisFactory analysisFactory) {
        this.analysisFactory = analysisFactory;
    }    

    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     *                  in memory and therefore will not be associated with a file.
     *
     */
    public ITree create() {
      return new Tree(analysisFactory);
    }

    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     * @param storeName The name of the store, if empty (""), the tree is created
     *                  in memory and therefore will not be associated with a file.
     * @throws IOException if the store already exists
     * @throws IllegalArgumentException
     *
     */
    public ITree create(String storeName) throws IllegalArgumentException, IOException {
        return create(storeName, null);
    }
    
    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     * @param storeName The name of the store, if empty (""), the tree is created
     *                  in memory and therefore will not be associated with a file.
     * @param storeType Implementation specific string, may control store type
     * @throws IOException if the store already exists
     * @throws IllegalArgumentException
     *
     */
    public ITree create(String storeName, String storeType) throws IllegalArgumentException, IOException {
        return createTree(storeName, storeType, false, false, null, false);
    }

    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     * @param storeName The name of the store, if empty (""), the tree is created
     *                  in memory and therefore will not be associated with a file.
     * @param storeType Implementation specific string, may control store type
     * @param readOnly If true the store is opened readonly, an exception if it does not exist
     * @throws IOException if the store already exists
     * @throws IllegalArgumentException
     *
     */
    public ITree create(String storeName, String storeType, boolean readOnly) throws IllegalArgumentException, IOException {
        return create(storeName, storeType, readOnly, false);
    }
    
    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     * @param storeName The name of the store, if empty (""), the tree is created
     *                  in memory and therefore will not be associated with a file.
     * @param storeType Implementation specific string, may control store type
     * @param readOnly If true the store is opened readonly, an exception if it does not exist
     * @param createNew If false the file must exist, if true the file will be created
     * @throws IOException if the store already exists
     * @throws IllegalArgumentException
     *
     */
    public ITree create(String storeName, String storeType, boolean readOnly, boolean createNew) throws IllegalArgumentException, IOException {
        return create(storeName, storeType, readOnly, createNew, null);
    }

    /**
     * Creates a new tree and associates it with a store.
     * The store is assumed to be read/write.
     * The store will be created if it does not exist.
     * @param storeName The name of the store, if empty (""), the tree is created
     *                  in memory and therefore will not be associated with a file.
     * @param storeType Implementation specific string, may control store type
     * @param readOnly If true the store is opened readonly, an exception if it does not exist
     * @param createNew If false the file must exist, if true the file will be created
     * @param options Other options, currently are not specified
     * @throws IOException if the store already exists
     * @throws IllegalArgumentException
     *
     */
    public ITree create(String storeName, String storeType, boolean readOnly, boolean createNew, String options) throws IllegalArgumentException, IOException {
        return createTree(storeName, storeType, readOnly, createNew, options, true);
    }
    
    protected ITree createTree(String storeName, String storeType, boolean readOnly, boolean createNew, String options, boolean readOnlyUserDefined) throws IllegalArgumentException, IOException {
        Tree tree = new Tree(analysisFactory);
        tree.init(storeName, readOnly, createNew, storeType, options, readOnlyUserDefined);
        return tree;
    }


}
