// $Id: ModuleState.java,v 1.3 2004/03/08 14:22:03 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.module;



/**
 * Represents a module and its state.
 *
 * @version $Revision: 1.3 $
 * @author      Michael Meyling
 */
public final class ModuleState {

    public static final ModuleState STATE_UNDIFINED
        = new ModuleState(ModuleConstants.STATE_STRING_UNDEFINED,
            false, ModuleConstants.STATE_CODE_UNDEFINED);

    public static final ModuleState STATE_LOADING_FROM_WEB
        = new ModuleState(ModuleConstants.STATE_STRING_LOADING_FROM_WEB,
            false, ModuleConstants.STATE_CODE_LOADING_FROM_WEB);

    public static final ModuleState STATE_LOADING_FROM_WEB_FAILED
        =  new ModuleState(ModuleConstants.STATE_STRING_LOADING_FROM_WEB_FAILED,
            true, ModuleConstants.STATE_CODE_LOADING_FROM_WEB_FAILED);

    public static final ModuleState STATE_LOADING_FROM_BUFFER
        = new ModuleState(ModuleConstants.STATE_STRING_LOADING_FROM_BUFFER,
            false, ModuleConstants.STATE_CODE_LOADING_FROM_BUFFER);

    public static final ModuleState STATE_LOADING_FROM_BUFFER_FAILED
        =  new ModuleState(ModuleConstants.STATE_STRING_LOADING_FROM_BUFFER_FAILED,
            true, ModuleConstants.STATE_CODE_LOADING_FROM_BUFFER_FAILED);

    public static final ModuleState STATE_LOADING_INTO_MEMORY
        = new ModuleState(ModuleConstants.STATE_STRING_LOADING_INTO_MEMORY,
            false, ModuleConstants.STATE_CODE_LOADING_INTO_MEMORY);

    public static final ModuleState STATE_CHECKING
        = new ModuleState(ModuleConstants.STATE_STRING_CHECKING,
            false, ModuleConstants.STATE_CODE_CHECKING);

    public static final ModuleState STATE_LOADING_INTO_MEMORY_FAILED
        =  new ModuleState(ModuleConstants.STATE_STRING_LOADING_INTO_MEMORY_FAILED,
            true, ModuleConstants.STATE_CODE_LOADING_INTO_MEMORY_FAILED);

    public static final ModuleState STATE_LOADED_AND_VERIFIED
        = new ModuleState(ModuleConstants.STATE_STRING_LOADED_AND_VERIFIED,
            false, ModuleConstants.STATE_CODE_LOADED_AND_VERIFIED);


    /** meaning of this state. */
    private final String text;

    /** is this state a failure? */
    private final boolean failure;

    /** Code for state. */
    private final int code;

    /** detailed description of this state. */
    private final String description;


    /**
     * Creates new module state.
     *
     * @param   text    meaning of this state, <code>null</code> is not permitted.
     * @param   failure is this a failure state?
     * @param   code    code of this state.
     * @throws  IllegalArgumentException    text == <code>null</code>
     */
    private ModuleState(final String text, final boolean failure, final int code) {
        this.text = text;
        if (this.text == null) {
            throw new IllegalArgumentException("text==null");
        }
        this.failure = failure;
        this.code = code;
        this.description = "";  // TODO
    }


    /**
     * Get meaning of module state.
     *
     * @return  meaning of module state.
     */
    public final String getText() {
        return this.text;
    }


    /**
     * Is this a failure state?
     *
     * @return  is this a failure state?
     */
    public final boolean isFailure() {
        return this.failure;
    }


    /**
     * Get detailed description of module state.
     *
     * @return  detailed description of module state.
     */
    public final String getDescription() {
        return this.description;
    }


    /**
     * Get module state code.
     *
     * @return  module state.
     */
    public final int getCode() {
        return this.code;
    }


    /*  (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public final String toString() {
        return this.text;
    }


    /*  (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public final int hashCode() {
        return this.text.hashCode();
    }

    /*  (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public final boolean equals(final Object obj) {
        if (obj == null || !(obj instanceof ModuleState)) {
            return false;
        }
        return text.equals(((ModuleState) obj).text);
    }



    /**
     * Interface to implement for each state.
     *
     * @version $Revision: 1.3 $
     * @author  Michael Meyling
     */
    private static interface ModuleStateAdapter {

        /**
         * Get module state description.
         *
         * @return  module state.
         */
        String getStateDescription();

        boolean isFailure();

        int getStateCode();
    }

}
