001    package edu.nrao.sss.astronomy;
002    
003    import java.util.Arrays;
004    
005    import edu.nrao.sss.util.EnumerationUtility;
006    import edu.nrao.sss.util.StringUtil;
007    
008    /**
009     * A single sideband of an amplitude modulated signal.
010     * <p>
011     * <b>Version Info:</b>
012     * <table style="margin-left:2em">
013     *   <tr><td>$Revision: 771 $</td></tr>
014     *   <tr><td>$Date: 2007-07-19 09:33:08 -0600 (Thu, 19 Jul 2007) $</td></tr>
015     *   <tr><td>$Author: dharland $ (last person to modify)</td></tr>
016     * </table></p>
017     * 
018     * @author David M. Harland
019     * @since 2007-07-19
020     */
021    public enum SideBand
022    {
023      LOWER("LSB"),
024      
025      UPPER("USB");
026      
027      private final String[] aliases;
028      
029      private SideBand(String... aliases)
030      {
031        this.aliases = aliases;
032        Arrays.sort(this.aliases);
033      }
034      
035      /**
036       * Returns a default side band.
037       * @return a default side band.
038       */
039      public static SideBand getDefault()
040      {
041        return UPPER;
042      }
043      
044      /**
045       * Returns a text representation of this enumeration constant.
046       * @return a text representation of this enumeration constant.
047       */
048      public String toString()
049      {
050        return EnumerationUtility.getSharedInstance().enumToString(this);
051      }
052      
053      /**
054       * Returns the side band represented by {@code text}.
055       * <p>
056       * For details about the transformation, see
057       * {@link EnumerationUtility#enumFromString(Class, String)}.</p>
058       * 
059       * @param text a text representation of a side band.
060       * 
061       * @return the side band represented by {@code text}.
062       */
063      public static SideBand fromString(String text)
064      {
065        //Try standard approach first
066        SideBand result = EnumerationUtility.getSharedInstance()
067                                            .enumFromString(SideBand.class,
068                                                            text);
069        //If no match found, search aliases
070        if (result == null)
071        {
072          text = StringUtil.getInstance().normalizeString(text);
073          for (SideBand elem : SideBand.values())
074          {
075            //NOTE: This relies on the fact that the aliases have been sorted.
076            if (Arrays.binarySearch(elem.aliases, text.toUpperCase()) >= 0)
077            {
078              result = elem;
079              break;
080            }
081          }
082        }
083        
084        return result;
085      }
086    }