Save This Page
Home » displaytag-1.1.1-src » org » displaytag » properties » [javadoc | source]
    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.properties;
   13   
   14   import java.util.Iterator;
   15   
   16   import org.apache.commons.collections.iterators.ArrayIterator;
   17   import org.apache.commons.lang.builder.HashCodeBuilder;
   18   
   19   
   20   /**
   21    * Enumeration for sort order.
   22    * @author Fabrizio Giustina
   23    * @version $Revision: 1081 $ ($Author: fgiust $)
   24    */
   25   public final class SortOrderEnum
   26   {
   27   
   28       /**
   29        * Sorted in descending order (1, "descending").
   30        */
   31       public static final SortOrderEnum DESCENDING = new SortOrderEnum(1, "descending"); //$NON-NLS-1$
   32   
   33       /**
   34        * Sorted in ascending order (2, "ascending").
   35        */
   36       public static final SortOrderEnum ASCENDING = new SortOrderEnum(2, "ascending"); //$NON-NLS-1$
   37   
   38       /**
   39        * array containing all the export types.
   40        */
   41       static final SortOrderEnum[] ALL = {DESCENDING, ASCENDING};
   42   
   43       /**
   44        * Code; this is the primary key for these objects.
   45        */
   46       private final int enumCode;
   47   
   48       /**
   49        * description.
   50        */
   51       private final String enumName;
   52   
   53       /**
   54        * private constructor. Use only constants.
   55        * @param code int code
   56        * @param name description of enumerated type
   57        */
   58       private SortOrderEnum(int code, String name)
   59       {
   60           this.enumCode = code;
   61           this.enumName = name;
   62       }
   63   
   64       /**
   65        * returns the int code.
   66        * @return int code
   67        */
   68       public int getCode()
   69       {
   70           return this.enumCode;
   71       }
   72   
   73       /**
   74        * returns the description.
   75        * @return String description of the sort order ("ascending" or "descending")
   76        */
   77       public String getName()
   78       {
   79           return this.enumName;
   80       }
   81   
   82       /**
   83        * lookup a SortOrderEnum by key.
   84        * @param key int code
   85        * @return SortOrderEnum or null if no SortOrderEnum is found with the given key
   86        */
   87       public static SortOrderEnum fromCode(int key)
   88       {
   89           for (int i = 0; i < ALL.length; i++)
   90           {
   91               if (key == ALL[i].getCode())
   92               {
   93                   return ALL[i];
   94               }
   95           }
   96           // lookup failed
   97           return null;
   98       }
   99   
  100       /**
  101        * lookup a SortOrderEnum by an Integer key.
  102        * @param key Integer code - null safe: a null key returns a null Enum
  103        * @return SortOrderEnum or null if no SortOrderEnum is found with the given key
  104        */
  105       public static SortOrderEnum fromCode(Integer key)
  106       {
  107           if (key == null)
  108           {
  109               return null;
  110           }
  111   
  112           return fromCode(key.intValue());
  113       }
  114   
  115       /**
  116        * lookup a SortOrderEnum by an Integer key.
  117        * @param key Integer code - null safe: a null key returns a null Enum
  118        * @return SortOrderEnum or null if no SortOrderEnum is found with the given key
  119        * @deprecated use fromCode(Integer)
  120        */
  121       public static SortOrderEnum fromIntegerCode(Integer key)
  122       {
  123           return fromCode(key);
  124       }
  125   
  126       /**
  127        * Lookup a SortOrderEnum by a String key.
  128        * @param code String code - null safe: a null key returns a null Enum
  129        * @return SortOrderEnum or null if no SortOrderEnum is found with the given key
  130        */
  131       public static SortOrderEnum fromName(String code)
  132       {
  133           for (int i = 0; i < ALL.length; i++)
  134           {
  135               if (ALL[i].getName().equals(code))
  136               {
  137                   return ALL[i];
  138               }
  139           }
  140           // lookup failed
  141           return null;
  142       }
  143   
  144       /**
  145        * returns an iterator on all the enumerated instaces.
  146        * @return iterator
  147        */
  148       public static Iterator iterator()
  149       {
  150           return new ArrayIterator(ALL);
  151       }
  152   
  153       /**
  154        * returns the enumeration description.
  155        * @see java.lang.Object#toString()
  156        */
  157       public String toString()
  158       {
  159           return getName();
  160       }
  161   
  162       /**
  163        * Only a single instance of a specific enumeration can be created, so we can check using ==.
  164        * @param o the object to compare to
  165        * @return hashCode
  166        */
  167       public boolean equals(Object o)
  168       {
  169           if (this == o)
  170           {
  171               return true;
  172           }
  173   
  174           return false;
  175       }
  176   
  177       /**
  178        * @see java.lang.Object#hashCode()
  179        */
  180       public int hashCode()
  181       {
  182           return new HashCodeBuilder(1123997057, -1289836553).append(this.enumCode).toHashCode();
  183       }
  184   
  185   }

Save This Page
Home » displaytag-1.1.1-src » org » displaytag » properties » [javadoc | source]