001    package edu.nrao.sss.model.project;
002    
003    import java.util.EnumSet;
004    
005    import edu.nrao.sss.model.resource.TelescopeType;
006    import edu.nrao.sss.util.EnumerationUtility;
007    
008    /**
009     * An enumeration of scientific priorities.
010     * <p>
011     * A lower number represents a higher priority.</p>
012     * 
013     * @version 1.1
014     * @since   2006-03-09
015     */
016    public enum ScientificPriority
017    {
018      /**
019       * The highest priority.
020       *  <p>
021       *  The acceptable names of this element (for use with
022       *  {@code getInstanceFromName(String)}) are
023       *  <i>ZERO</i>, <i>0</i>, and <i>A</i>.</p>
024       */
025      ZERO('A', 0),
026      
027      /**
028       * An intermediate priority that is lower than {@link #ZERO} but higher
029       * than {@link #TWO}.
030       *  <p>
031       *  The acceptable names of this element (for use with
032       *  {@code getInstanceFromName(String)}) are
033       *  <i>ONE</i>, <i>1</i>, and <i>B</i>.</p>
034       */
035      ONE('B', 1),
036      
037      /**
038       * An intermediate priority that is lower than {@link #ONE} but higher
039       * than {@link #THREE}.
040       *  <p>
041       *  The acceptable names of this element (for use with
042       *  {@code getInstanceFromName(String)}) are
043       *  <i>TWO</i>, <i>2</i>, and <i>C</i>.</p>
044       */
045      TWO('C', 2),
046      
047      /**
048       * An intermediate priority that is lower than {@link #TWO} but higher
049       * than {@link #FOUR}.
050       *  <p>
051       *  The acceptable names of this element (for use with
052       *  {@code getInstanceFromName(String)}) are
053       *  <i>THREE</i>, <i>3</i>, and <i>D</i>.</p>
054       */
055      THREE('D', 3),
056      
057      /**
058       * The lowest priority (except for that of the {@link #UNKNOWN} priority).
059       *  <p>
060       *  The acceptable names of this element (for use with
061       *  {@code getInstanceFromName(String)}) are
062       *  <i>FOUR</i>, <i>4</i>, and <i>E</i>.</p>
063       */
064      FOUR('E', 4),
065      
066      /** Represents a scientific priority of unknown type.
067       *  <p>
068       *  This priority is lower than all others.</p>
069       *  <p>
070       *  This element is used to implement the
071       *  <i>Null Object Pattern</i>.  In situations where a method might
072       *  be tempted to return <i>null</i>, this element is returned instead.</p>
073       */
074      UNKNOWN('?', Integer.MAX_VALUE);
075    
076      private final char character;
077      private final int  number;
078    
079      /** Creates a new priority. */
080      private ScientificPriority(char asChar, int asNum)
081      {
082        this.character = asChar;
083        this.number    = asNum;
084      }
085      
086      /**
087       * Returns a numeric representation of this priority.
088       * 
089       * @return a numeric representation of this priority.
090       */
091      public int toInt()
092      {
093        return this.number;
094      }
095      
096      /**
097       * Returns a single character that represents this priority.
098       * 
099       * @return a single character that represents this priority.
100       */
101      public char toChar()
102      {
103        return this.character;
104      }
105      
106      /**
107       * Returns a text representation of this priority that is
108       * appropriate for the given telescope.  For the
109       * {@link TelescopeType#GBT}, this is a single alphabetic
110       * character.  Otherwise, this is an integer.
111       * 
112       * @param telescope the telescope for which a text representation
113       *                  of this priority is desired.
114       *                  
115       * @return a text representation of this priority.
116       */
117      public String toString(TelescopeType telescope)
118      {
119        switch(telescope)
120        {
121          case GBT:  return Character.toString(toChar());
122          default:   return Integer.toString(toInt());
123        }
124      }
125      
126      public String toString()
127      {
128        return toString(TelescopeType.getDefault());
129      }
130      
131      /**
132       * Returns <i>true</i> if this priority is higher (more important)
133       * than {@code otherSciPri}.
134       * 
135       * @param otherSciPri the other priority to which this one is compared.
136       * 
137       * @return <i>true</i> if this priority is higher {@code otherSciPri}.
138       */
139      public boolean isHigherPriorityThan(ScientificPriority otherSciPri)
140      {
141        return this.toInt() < otherSciPri.toInt();
142      }
143      
144      /**
145       * Returns a set of enumerations that is appropriate for the given telescope.
146       * <p>
147       * Since GBT uses only the priorities A, B, and C, only the priorities
148       * with these {@code toChar()} properties are contained in the set.
149       * Otherwise, all priorities <i>except for the {@code UNKNOWN} priority</i>
150       * are in the returned set.</p>
151       * 
152       * @param telescope the telescope for which a set of priorities is needed.
153       * 
154       * @return a set of priorities for the given telescope.
155       */
156      public static EnumSet<ScientificPriority> getPrioritiesFor(TelescopeType telescope)
157      {
158        ScientificPriority from = ZERO;
159        ScientificPriority thru = telescope.equals(TelescopeType.GBT) ? TWO : FOUR;
160        
161        return EnumSet.range(from, thru);
162      }
163    
164      /**
165       * Returns a default scientific priority.
166       * @return a default scientific priority.
167       */
168      public static ScientificPriority getDefault()
169      {
170        return UNKNOWN;
171      }
172      
173      /**
174       * Returns the scientific priority represented by {@code text}.
175       * <p>
176       * For details about the transformation, see
177       * {@link EnumerationUtility#enumFromString(Class, String)}.</p>
178       * 
179       * @param text a text representation of a scientific priority.
180       * 
181       * @return the scientific priority represented by {@code text}.
182       */
183      public static ScientificPriority fromString(String text)
184      {
185        return EnumerationUtility.getSharedInstance()
186                                 .enumFromString(ScientificPriority.class, text);
187      }
188    }