001    package edu.nrao.sss.model.resource.vla;
002    
003    import java.util.HashSet;
004    import java.util.Set;
005    
006    /**
007     * Data processing that can be done by the correlator.
008     * <p>
009     * <b>Version Info:</b>
010     * <table style="margin-left:2em">
011     *   <tr><td>$Revision: 1218 $</td></tr>
012     *   <tr><td>$Date: 2008-04-14 14:56:14 -0600 (Mon, 14 Apr 2008) $</td></tr>
013     *   <tr><td>$Author: btruitt $ (last person to modify)</td></tr>
014     * </table></p>
015     * 
016     * @author David M. Harland
017     * @since 2008-03-19
018     */
019    public enum ProcessingType
020    {
021      TRUE_CHANNEL_ZERO('Z', "True Channel Zero"),
022      HANNING     ('H', "Hanning Smoothing"),
023      LAG         ('L', "Lag");
024      
025      private char   code;
026      private String displayText;
027      
028      private ProcessingType(char code, String displayText)
029      {
030        this.code = code;
031        this.displayText = displayText;
032      }
033      
034      /**
035       * Returns a character code for this element.
036       * This code can be turned into a code that is used in execution scripts,
037       * either on its own or in combination with codes from other types,
038       * by using method {@link #getCombinationCode(Set)}.
039       *  
040       * @return a character code for this element.
041       */
042      public char getCode()  { return code; }
043      
044      /**
045       * Returns text for this element that is suitable for user displays.
046       * @return text for this element that is suitable for user displays.
047       */
048      public String toString()  { return displayText; }
049      
050      /**
051       * Returns <i>true</i> if the given set of processing types may
052       * be used simultaneously.
053       * 
054       * @param combination
055       *   a collection of processing types to be tested for validity of
056       *   simultaneous use.  If the list is null, empty, or contains
057       *   one element, the return value will always be <i>true</i>.
058       *   
059       * @return
060       *   <i>true</i> if the given set of processing types may
061       *   be used simultaneously.
062       */
063      public static boolean isValidSelection(Set<ProcessingType> combination)
064      {
065        if (combination == null || combination.size() <= 1)
066          return true;
067        else if (combination.size() > 2)
068          return false;
069        else //have 2 elements; OK as long as neither is LAG 
070          return !combination.contains(LAG);
071      }
072      
073      /**
074       * Returns a code that represents the given combination of processing types.
075       * If the combination is null, empty, or invalid, a blank code will be
076       * returned.
077       * 
078       * @param combination
079       *   a collection of processing types for which a combined code is needed.
080       *   
081       * @return
082       *   a code that represents the given combination of processing types.
083       */
084      public static String getCombinationCode(Set<ProcessingType> combination)
085      {
086        String code;
087        
088        if (!isValidSelection(combination) ||
089            combination == null || combination.size() == 0)
090        {
091          code = "  ";
092        }
093        else
094        {
095          StringBuilder buff = new StringBuilder();
096          for (ProcessingType p : combination)
097            buff.append(p.getCode());
098          if (buff.length() == 1)
099            buff.append(' ');
100          code = buff.toString();
101        }
102        return code;
103      }
104      
105      /**
106       * Returns the collection of processing types represented by the collection
107       * of characters in {@code comboCode}.  This method does <i>not</i> test
108       * the collection for validity (see {@link #isValidSelection(Set)}.
109       * 
110       * @param comboCode
111       *   text representation of zero or more processing types.
112       *   This value may be <i>null</i> or the empty string, in which case
113       *   an empty set will be returned.
114       *   
115       * @return
116       *   the collection of processing types represented by the collection
117       *   of characters in {@code comboCode}.  The returned collection will
118       *   never be <i>null</i>, but could be empty.
119       *   
120       * @throws IllegalArgumentException
121       *   if {@code comboCode} contains any unknown character codes.
122       */
123      public static Set<ProcessingType> getTypesFromComboCode(String comboCode)
124      {
125        Set<ProcessingType> result = new HashSet<ProcessingType>();
126        
127        if (comboCode != null && comboCode.length() > 0)
128        {
129          Set<Character> chars = new HashSet<Character>();
130          
131          for (int i=comboCode.length()-1; i >= 0; i--)
132            chars.add(comboCode.charAt(i));
133          
134          for (ProcessingType pt : ProcessingType.values())
135          {
136            if (chars.contains(pt.getCode()))
137            {
138              result.add(pt);
139              chars.remove(pt.getCode());
140            }
141          }
142          
143          //Extraneous characters in comboCode
144          if (chars.size() > 0)
145            throw new IllegalArgumentException("comboCode " + comboCode +
146              " contained these illegal characters: " + chars);
147        }
148        
149        return result;
150      }
151    }