NumberMetaData.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:12k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: NumberMetaData.java,v 1.3 2005/10/10 17:01:09 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.binding.metadata;
  22. import java.util.Locale;
  23. /**
  24.  * <p>
  25.  * Class for representing meta-data for a numerical data field which is
  26.  * one of the following types:
  27.  * <ul>
  28.  * <li>java.lang.Integer</li>
  29.  * <li>java.lang.Long</li>
  30.  * <li>java.lang.Short</li>
  31.  * <li>java.lang.Float</li>
  32.  * <li>java.lang.Double</li>
  33.  * </ul>
  34.  * This meta-data class defines additional properties and edit constraints
  35.  * which are applicable to numerical values, such as minimum, maximum,
  36.  * whether or not the value is a currency, etc.  Example usage:
  37.  * <pre><code>
  38.  *     NumberMetaData metaData = new NumberMetaData(&quot;interestrate&quot;,
  39.  *                                                  Float.class, &quot;Interest Rate&quot;);
  40.  *     metaData.setMinimum(new Float(4.5));
  41.  *     metaData.setMaximum(new Float(6.8));
  42.  * </code></pre>
  43.  * Setting a minimum and/or maximum constraint will implicitly cause a range
  44.  * validator to be added to the meta-data object.
  45.  * </p>
  46.  *
  47.  * @author Amy Fowler
  48.  * @version 1.0
  49.  */
  50. public class NumberMetaData extends MetaData {
  51.     protected Number minimum = null;
  52.     protected Number maximum = null;
  53.     protected boolean currency = false;
  54.     private Validator rangeValidator = null;
  55.     /**
  56.       * Instantiates a meta-data object with a default name &quot;numbervalue&quot; and
  57.       * a default field class equal to <code>java.lang.Integer</code>.
  58.       * This provides the no-argument constructor required for JavaBeans.
  59.       * It is recommended that the program explicitly set a meaningful
  60.       * &quot;name&quot; property.
  61.       */
  62.      public NumberMetaData() {
  63.          this("numbervalue");
  64.      }
  65.     /**
  66.      * Instantiates a meta-data object with the specified name and
  67.      * a default field class equal to <code>java.lang.Integer</code>.
  68.      * @param name String containing the name of the data field
  69.      */
  70.     public NumberMetaData(String name) {
  71.         super(name);
  72.         this.klass = Integer.class;
  73.     }
  74.     /**
  75.      * Instantiates a meta-data object with the specified name and
  76.      * field class.
  77.      * @param name String containing the name of the data field
  78.      * @param klass Class indicating type of data field
  79.      */
  80.     public NumberMetaData(String name, Class klass) {
  81.         this(name);
  82.         this.klass = klass;
  83.     }
  84.     /**
  85.      * Instantiates a meta-data object with the specified name,
  86.      * field class, and label.
  87.      * @param name String containing the name of the data field
  88.      * @param klass Class indicating type of data field
  89.      * @param label String containing the user-displayable label for the
  90.      *        data field
  91.      */
  92.     public NumberMetaData(String name, Class klass, String label) {
  93.         this(name, klass);
  94.         this.label = label;
  95.     }
  96.     /**
  97.      * Gets the meta-data &quot;minimum&quot; property which indicates
  98.      * the minimum value of the associated data field.
  99.      * The default is <code>null</code>, which indicates no minimum.
  100.      * @see #setMinimum
  101.      * @return Number containing the minimum value of the data field.
  102.      */
  103.     public Number getMinimum() {
  104.         return minimum;
  105.     }
  106.     /**
  107.      * Sets the meta-data &quot;minimum&quot; property.
  108.      * Setting a minimum and/or maximum will cause an appropriate
  109.      * range validator object to be added to this meta-data object.
  110.      * @see #getMinimum
  111.      * @param minimum Number containing the minimum value of the data field.
  112.      * @throws IllegalArgumentException if the minimum object's class does
  113.      *         not equal the meta-data's field class
  114.      */
  115.     public void setMinimum(Number minimum) {
  116.         if (klass != minimum.getClass()) {
  117.             throw new IllegalArgumentException(getName() + ": minimum value is class "+
  118.                                                minimum.getClass().getName() +
  119.                                                " but should be "+ getClass().getName());
  120.         }
  121.         Number oldMinimum = this.minimum;
  122.         this.minimum = minimum;
  123.         setupRangeValidator();
  124.         firePropertyChange("minimum", oldMinimum, minimum);
  125.     }
  126.     /**
  127.      * Gets the meta-data &quot;maximum&quot; property which indicates
  128.      * the maximum value of the associated data field.
  129.      * The default is <code>null</code>, which indicates no maximum.
  130.      * @see #setMaximum
  131.      * @return Number containing the maximum value of the data field.
  132.      */
  133.     public Number getMaximum() {
  134.         return maximum;
  135.     }
  136.     /**
  137.      * Sets the meta-data &quot;maximum&quot; property.
  138.      * Setting a minimum and/or maximum will cause an appropriate
  139.      * range validator object to be added to this meta-data object.
  140.      * @see #getMaximum
  141.      * @param maximum Number containing the maximum value of the data field.
  142.      * @throws IllegalArgumentException if the maximum object's class does
  143.      *         not equal the meta-data's field class
  144.      */
  145.     public void setMaximum(Number maximum) {
  146.         if (getElementClass() != maximum.getClass()) {
  147.             throw new IllegalArgumentException(getName() +
  148.                                                ": maximum value is class " +
  149.                                                maximum.getClass().getName() +
  150.                                                " but should be " +
  151.                                                getClass().getName());
  152.         }
  153.         Number oldMaximum = this.maximum;
  154.         this.maximum = maximum;
  155.         setupRangeValidator();
  156.         firePropertyChange("maximum", oldMaximum, maximum);
  157.     }
  158.     /**
  159.      * Gets the meta-data &quot;currency&quot; property which indicates
  160.      * whether this data field represents a currency value.
  161.      * The default is <code>false</code>.
  162.      * @see #setCurrency
  163.      * @return boolean indicating whether the data field represents a currency
  164.      */
  165.     public boolean isCurrency() {
  166.         return currency;
  167.     }
  168.     /**
  169.      * Sets the meta-data &quot;currency&quot; property.
  170.      * @see #isCurrency
  171.      * @param currency boolean indicating whether the data field represents a currency
  172.      */
  173.     public void setCurrency(boolean currency) {
  174.         boolean oldCurrency = this.currency;
  175.         this.currency = currency;
  176.         firePropertyChange("currency", oldCurrency, currency);
  177.     }
  178.     private void setupRangeValidator() {
  179.         if (maximum != null || minimum != null) {
  180.             if (rangeValidator == null) {
  181.                 rangeValidator = getRangeValidator();
  182.                 addValidator(rangeValidator);
  183.             }
  184.         } else if (rangeValidator != null) {
  185.             removeValidator(rangeValidator);
  186.             rangeValidator = null;
  187.         }
  188.     }
  189.     private Validator getRangeValidator() {
  190.         if (klass.equals(Integer.class)) {
  191.             return new IntegerRangeValidator();
  192.         } else if (klass.equals(Long.class)) {
  193.             return new LongRangeValidator();
  194.         } else if (klass.equals(Float.class)) {
  195.             return new FloatRangeValidator();
  196.         } else if (klass.equals(Short.class)) {
  197.             return new ShortRangeValidator();
  198.         } else if (klass.equals(Double.class)) {
  199.             return new DoubleRangeValidator();
  200.         }
  201.         return null;
  202.     }
  203.     /**@todo aim: these could be exposed as public static Validator classes,
  204.      *  but they would then have to become stateful.
  205.      */
  206.     private class IntegerRangeValidator implements Validator {
  207.         public boolean validate(Object value, Locale locale, String[] error) {
  208.             int intValue = ((Integer)value).intValue();
  209.             if (maximum != null &&
  210.                 intValue > ((Integer)maximum).intValue()) {
  211.                 error[0] = getName()+ ": value " + intValue + " exceeds maximum "+
  212.                         ((Integer)maximum).intValue();
  213.                 return false;
  214.             }
  215.             if (minimum != null &&
  216.                 intValue < ((Integer)minimum).intValue()) {
  217.                 error[0] = getName() + ": value " + intValue + " is less than the minimum" +
  218.                         ((Integer)minimum).intValue();
  219.                 return false;
  220.             }
  221.             return true;
  222.         }
  223.     }
  224.     private class LongRangeValidator implements Validator {
  225.         public boolean validate(Object value, Locale locale, String[] error) {
  226.             long longValue = ((Long)value).longValue();
  227.             if (maximum != null &&
  228.                 longValue > ((Long)maximum).longValue()) {
  229.                 error[0] = getName()+ ": value " + longValue + " exceeds maximum "+
  230.                         ((Long)maximum).longValue();
  231.                 return false;
  232.             }
  233.             if (minimum != null &&
  234.                 longValue < ((Long)minimum).longValue()) {
  235.                 error[0] = getName() + ": value " + longValue + " is less than the minimum" +
  236.                         ((Long)minimum).longValue();
  237.                 return false;
  238.             }
  239.             return true;
  240.         }
  241.     }
  242.     private class ShortRangeValidator implements Validator {
  243.         public boolean validate(Object value, Locale locale, String[] error) {
  244.             short shortValue = ((Short)value).shortValue();
  245.             if (maximum != null &&
  246.                 shortValue > ((Short)maximum).shortValue()) {
  247.                 error[0] = getName()+ ": value " + shortValue + " exceeds maximum "+
  248.                         ((Short)maximum).shortValue();
  249.                 return false;
  250.             }
  251.             if (minimum != null &&
  252.                 shortValue < ((Short)minimum).shortValue()) {
  253.                 error[0] = getName() + ": value " + shortValue + " is less than the minimum" +
  254.                         ((Short)minimum).shortValue();
  255.                 return false;
  256.             }
  257.             return true;
  258.         }
  259.     }
  260.     private class FloatRangeValidator implements Validator {
  261.         public boolean validate(Object value, Locale locale, String[] error) {
  262.             float floatValue = ((Float)value).floatValue();
  263.             if (maximum != null &&
  264.                 floatValue > ((Float)maximum).floatValue()) {
  265.                 error[0] = getName()+ ": value " + floatValue + " exceeds maximum "+
  266.                         ((Float)maximum).floatValue();
  267.                 return false;
  268.             }
  269.             if (minimum != null &&
  270.                 floatValue < ((Float)minimum).floatValue()) {
  271.                 error[0] = getName() + ": value " + floatValue + " is less than the minimum" +
  272.                         ((Float)minimum).floatValue();
  273.                 return false;
  274.             }
  275.             return true;
  276.         }
  277.     }
  278.     private class DoubleRangeValidator implements Validator {
  279.         public boolean validate(Object value, Locale locale, String[] error) {
  280.             double doubleValue = ((Double)value).doubleValue();
  281.             if (maximum != null &&
  282.                 doubleValue > ((Double)maximum).doubleValue()) {
  283.                 error[0] = getName()+ ": value " + doubleValue + " exceeds maximum "+
  284.                         ((Double)maximum).doubleValue();
  285.                 return false;
  286.             }
  287.             if (minimum != null &&
  288.                 doubleValue < ((Double)minimum).doubleValue()) {
  289.                 error[0] = getName() + ": value " + doubleValue + " is less than the minimum" +
  290.                         ((Double)minimum).doubleValue();
  291.                 return false;
  292.             }
  293.             return true;
  294.         }
  295.     }
  296. }