001    /**
002     * Do we want to use this space for anything?  Eg, revision history?
003     */
004    package edu.nrao.sss.astronomy;
005    
006    import java.util.Arrays;
007    
008    import edu.nrao.sss.util.EnumerationUtility;
009    import edu.nrao.sss.util.StringUtil;
010    
011    /**
012     * An enumeration of astronomical epochs.
013     * <p>
014     * An epoch in astronomy is a point in time for which celestial
015     * positions are specified.
016     * See <a href="http://en.wikipedia.org/wiki/Epoch_%28astronomy%29">Wikipedia</a>
017     * for more information.
018     * 
019     * @author David M. Harland
020     * @since  2006-03-02
021     */
022    public enum Epoch
023    {
024      /** Besselian epoch B1950.0.
025       *  See <a href="http://en.wikipedia.org/wiki/B1950.0">Wikipedia</a>
026       *  for more information.
027       */
028      B1950(new String[] {"B1950.0", "1950", "1950.0"}), 
029    
030      /** Julian epoch J2000.0.
031       *  See <a href="http://en.wikipedia.org/wiki/J2000">Wikipedia</a>
032       *  for more information.
033       */
034      J2000(new String[] {"J2000.0", "2000", "2000.0"}), 
035    
036      /** An unknown proposal status.  This element is used to implement the
037       *  <i>Null Object Pattern</i>.  In situations where a method might
038       *  be tempted to return <i>null</i>, this element is returned instead.
039       */
040      UNKNOWN(new String[] {"UNK"});
041      
042      private final String[] aliases;
043    
044      /**
045       * Constructs a new epoch.
046       * 
047       * @param aliases a list of other names by which this epoch is known.
048       */
049      private Epoch(String[] aliases)
050      {
051        this.aliases = aliases;
052        Arrays.sort(this.aliases);
053      }
054      
055      /**
056       * Returns a default epoch.
057       * @return a default epoch.
058       */
059      public static Epoch getDefault()
060      {
061        return J2000;
062      }
063      
064      /**
065       * Returns a text representation of this enumeration constant.
066       * @return a text representation of this enumeration constant.
067       */
068      public String toString()
069      {
070        return EnumerationUtility.getSharedInstance().enumToString(this);
071      }
072      
073      /**
074       * Returns an epoch for the given {@code text}.
075       * <p>
076       * The format of {@code text} is somewhat forgiving, in that
077       * the search is not case-sensitive and will ignore leading
078       * and/or trailing whitespace.  In addition, several forms
079       * of the name are permitted.  For example, both <i>"J2000"</i>
080       * and <i>"J2000.0"</i> will be resolved to {@link #J2000}.</p> 
081       * <p>
082       * If no epoch can be found for {@code text},
083       * {@link #UNKNOWN} is returned.</p>
084       * 
085       * @param text the name of an epoch.
086       * 
087       * @return an epoch for {@code name}.
088       */
089      public static Epoch fromString(String text)
090      {
091        //Try standard approach first
092        Epoch result = EnumerationUtility.getSharedInstance()
093                                         .enumFromString(Epoch.class, text);
094        
095        //If no match found, search aliases
096        if (result == null)
097        {
098          text = StringUtil.getInstance().normalizeString(text);
099          for (Epoch epoch : Epoch.values())
100          {
101            //See if the parameter name is in the list of the epoch's acceptable names.
102            //NOTE: This relies on the fact that the aliases have been sorted.
103            if (Arrays.binarySearch(epoch.aliases, text.toUpperCase()) >= 0)
104            {
105              result = epoch;
106              break;
107            }
108          }
109        }
110        
111        return result;
112      }
113    }