⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 distribution.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Field[] fields = jmt.gui.jmodel.JMODELConstants.class.getFields();
        Vector tmp = new Vector();
         try {
         for (int i=0; i<fields.length; i++)
            if (fields[i].getName().startsWith("DISTRIBUTION_"))
                tmp.add(Class.forName(path + (String)fields[i].get(null)).newInstance());
         } catch (IllegalAccessException ex) {
            System.err.println("A security manager has blocked reflection");
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.err.println("Reflection Error: cannot access to distribution's implementation");
            e.printStackTrace();
        } catch (InstantiationException e) {
             System.err.println("Reflection Error: Cannot instantiate a distribution");
             e.printStackTrace();
         }
        Distribution[] ret = new Distribution[tmp.size()];
        for (int i=0; i<tmp.size(); i++)
            ret[i] = (Distribution)tmp.get(i);
        all = ret;
        return ret;
    }

    /**
     * Return an array with an istance of every Distribution that allows mean setting. Uses internal
     * caching to search for distributions only the first time that this method is called.
     * @return an array with an istance of every Distribution in which mean value can be specified
     */
    public static Distribution[] findAllWithMean() {
        if (allWithMean != null)
            return allWithMean;
        Distribution[] all = findAll();
        Distribution[] tmp = new Distribution[all.length];
        int n = 0;
        for (int i=0;i<all.length; i++)
            if (all[i].hasMean())
                tmp[n++] = all[i];
        // Now removes empty elements into tmp array
        allWithMean = new Distribution[n];
        for (int i=0; i<n;i++)
            allWithMean[i] = tmp[i];
        return allWithMean;
    }

    /**
     * Tells if this distribution is equivalent to an other one
     * @param o other distribution
     * @return true if equals, false otherwise
     */
    public boolean equals(Object o) {
        if (!(o instanceof Distribution))
            return false;
        Distribution d = (Distribution) o;
        // Check if class is the same
        if (!getClass().equals(d.getClass()))
            return false;
        // Check if all parameters are equals
        for (int i=0; i<getNumberOfParameters(); i++)
            if (!getParameter(i).getValue().equals(d.getParameter(i).getValue()))
                return false;
        return true;
    }

// ----- Abastract Methods that must be implemented by all distributions ------------------------------

    /**
     * Used to set parameters of this distribution.
     * @return distribution parameters
     */
    protected abstract Parameter[] setParameters();

    /**
     * Sets explicative image of this distribution used, together with description, to help the
     * user to understand meaning of parameters.
     * @return explicative image
     */
    protected abstract ImageIcon setImage();

    /**
     * Returns this distribution's short description
     * @return distribution's short description
     */
    public abstract String toString();

// ----- Methods that must be implemented ONLY if hasC or hasMean are true ----------------------------
    /**
     * Sets the variation coefficient C for this distribution
     * @param value variation coefficient C value
     */
    public void setC (double value) {
        c = value;
    }

    /**
     * Sets the mean for this distribution
     * @param value mean value
     */
    public void setMean (double value) {
        mean = value;
    }

    /**
     * This method has to be called whenever a parameter changes and <code>hasC</code> or
     * <code>hasMean</code> are true
     */
    public void updateCM() {
    }
// ----- Inner class used to store parameters ---------------------------------------------------------

    /**
     * This class describes a parameter. It can be instantiated only by Distribution and its subclassses
     * but will be accessed with public methods
     */
    public class Parameter implements Cloneable {
        protected Class valueClass;
        protected Object value;
        protected String name;
        protected String description;
        protected ValueChecker checker;

        /**
         * Construct a new Parameter object. If a custom check on input values is needed, a call
         * to <code>setValueChecker(ValueChecker checker)</code> method must be performed.
         * @param name name of this parameter
         * @param description brief description of this parameter usage
         * @param valueClass Class type of value of this parameter
         * @param defaultValue initial value for this parameter
         * <code>getParameterClasspath()</code> for more details.
         */
        public Parameter(String name, String description, Class valueClass,
                         Object defaultValue) {
            this.name = name;
            this.description = description;
            this.valueClass = valueClass;
            this.value = defaultValue;
            checker = null;
        }

        /**
         * Sets a ValueChecker to check if a parameter's value is in the correct range.
         * If no checks are required don't call this method
         * @param checker Instance of ValueChecker Interface
         */
        public void setValueChecker(ValueChecker checker) {
            this.checker = checker;
        }

        /**
         * Sets value for this parameter. Returns if parameter could be assigned correctly
         * @param value value to be assigned to this parameter
         * @return If value.class is different from valueClass, or parameter value is invalid,
         * returns false, otherwise true
         */
        public boolean setValue(Object value) {
            // if instance is not correct returns false
            if (valueClass.isInstance(value)) {
                // If checker exists and its check is not satisfied returns false
                if (checker != null)
                    if (!checker.checkValue(value))
                        return false;
                // Otherwise sets value and returns true
                this.value = value;
                return true;
            }
            return false;
        }

        /**
         * Sets value for this parameter. If parameter class is String sets it, otherwise
         * try to parse string if parameter is of a known numeric value. Returns if parameter
         * could be assigned correctly.
         * @param value to be assigned to this parameter or to be parsed
         * @return If value.class is different from valueClass, or parameter value is invalid,
         * or is not parsable, returns false, otherwise true
         */
        public boolean setValue(String value) {
            // If parameter is of the correct type, sets it and return
            if (setValue((Object)value))
                return true;

            // Otherwise if it's a known number, try to decode it
            Object objval = null;
            try {
                if (valueClass.equals(Integer.class))
                    objval = Integer.decode(value);
                else if (valueClass.equals(Long.class))
                    objval = Long.decode(value);
                else if (valueClass.equals(Short.class))
                    objval = Short.decode(value);
                else if (valueClass.equals(Byte.class))
                    objval = Byte.decode(value);
                else if (valueClass.equals(Float.class))
                    objval = new Float(Float.parseFloat(value));
                else if (valueClass.equals(Double.class))
                    objval = new Double(Double.parseDouble(value));
                else
                    return false;
                return setValue(objval);
            } catch (NumberFormatException e) {
                return false;
            }
        }

        /**
         * Gets this parameter's value
         * @return this parameter's value
         */
        public Object getValue() {
            return value;
        }

        /**
         * Gets this parameter value's class, ie the class of which parameter value is instance of.
         * @return parameter value's class
         */
        public Class getValueClass() {
            return valueClass;
        }

        /**
         * Gets a brief description of this parameter, used on distributionpanel
         * @return parameter's description
         */
        public String getDescription() {
            return description;
        }

        /**
         * Gets the name of this parameter
         * @return this parameter's name
         */
        public String getName() {
            return name;
        }

        /**
         * Returns a shallow copy of this Parameter. Note that as nothing can be infered about
         * all values clonability, that parameters are only referenced and not cloned. This should
         * not be a problem as every value should be an immutable type.
         * @return a copy of this parameter
         */
        public Object clone() {
            Parameter tmp = new Parameter(this.name, this.description,
                    this.valueClass, this.value);
            tmp.setValueChecker(this.checker);
            return tmp;
        }
    }

    /**
     * This interface is used to specify a custom check method on parameter's value (for example
     * value's range).
     */
    protected interface ValueChecker {
        /**
         * Checks if value of this object is correct. Note that when this method is called,
         * parameter's value is granted to be instance of the correct class.
         * @param value value to be checked
         * @return true iff value is correct for this distribution parameter
         */
        public boolean checkValue(Object value);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -