001    package edu.nrao.sss.model.project;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.Collection;
006    import java.util.EnumSet;
007    
008    //import org.apache.log4j.Logger;
009    
010    import edu.nrao.sss.util.EventSetStatus;
011    import edu.nrao.sss.util.Filter;
012    
013    /**
014     * A filter that operates on {@link SchedulingBlock scheduling blocks}.
015     * <p>
016     * <b>Version Info:</b>
017     * <table style="margin-left:2em">
018     *   <tr><td>$Revision: 1709 $</td></tr>
019     *   <tr><td>$Date: 2008-11-14 11:22:37 -0700 (Fri, 14 Nov 2008) $</td></tr>
020     *   <tr><td>$Author: dharland $ (last person to modify)</td></tr>
021     * </table></p>
022     * 
023     * @author David M. Harland
024     * @since 2008-06-11
025     */
026    public class SchedulingBlockFilter
027      implements Filter<SchedulingBlock>
028    {
029    //private static final Logger log = Logger.getLogger(SchedulingBlockFilter.class);
030    
031      private EnumSet<EventSetStatus> execStatuses;
032      
033      //TODO create individual criteria variables
034    
035      /**
036       * Creates a new wide-open filter that allows all scheduling blocks to pass.
037       */
038      public SchedulingBlockFilter()
039      {
040        execStatuses = EnumSet.noneOf(EventSetStatus.class);
041        
042        clearAll();
043      }
044    
045      //============================================================================
046      // CLEARING THE FILTERING CRITERIA
047      //============================================================================
048    
049      /**
050       * Sets this filter to a wide-open state.
051       */
052      public void clearAll()
053      {
054        clearExecStatuses();
055        //TODO clear individual criteria
056      }
057    
058      /**
059       * Sets the execution status criterion into its wide-open state.
060       * This means that no scheduling block will be blocked by this filter due
061       * to its event status.
062       */
063      public void clearExecStatuses()
064      {
065        execStatuses.addAll(Arrays.asList(EventSetStatus.values())); 
066      }
067      
068      //TODO clear-methods for individual criteria
069    
070      //============================================================================
071      // SETTING THE FILTER CRITERIA
072      //============================================================================
073    
074      /**
075       * Sets the execution status criterion.
076       * Only those scheduling blocks that have a status equal to one of those
077       * in the parameter list may pass through this filter.
078       * 
079       * @param goodStatuses
080       *   a list of execution statuses.  Any scheduling block passing through
081       *   this filter must have one of these statuses.
082       *   
083       * @see #clearExecStatuses()
084       */
085      public void setExecStatuses(EventSetStatus... goodStatuses)
086      {
087        execStatuses.clear();
088        
089        for (EventSetStatus ess : goodStatuses)
090          execStatuses.add(ess);
091      }
092      
093      //TODO set-methods for individual criteria
094      //  Some possibilities: pref LST start, status, is-ready-for-scheduling,
095      //                      total duration, sched type
096      
097      //============================================================================
098      // APPLYING THIS FILTER
099      //============================================================================
100    
101      public boolean blocks(SchedulingBlock sb)
102      {
103        //Filter blocks all null SBs
104        if (sb == null)
105          return true;
106        
107        //Event status
108        if (!execStatuses.contains(sb.getExecutionStatus()))
109          return true;
110        
111        //TODO use individual criteria
112    
113        return false;
114      }
115    
116      public boolean allows(SchedulingBlock sb)
117      {
118        return !blocks(sb);
119      }
120      
121      /**
122       * Selects those objects in {@code bag} that are scheduling blocks and that
123       * can pass through this filter.  The selections are added to a new collection
124       * and returned.  If the bag holds no such objects, the returned collection
125       * will be empty.
126       * <p>
127       * The original collection ({@code bag}) is not altered.</p>
128       * 
129       * @param bag
130       *   a collection of objects.
131       * 
132       * @return
133       *   a collection of scheduling blocks from {@code bag} that were able to
134       *   pass through this filter.
135       */
136      public Collection<SchedulingBlock> selectFrom(Collection<?> bag)
137      {
138        Collection<SchedulingBlock> selection = new ArrayList<SchedulingBlock>();
139        
140        for (Object candidate : bag)
141        {
142          if (candidate instanceof SchedulingBlock)
143          {
144            SchedulingBlock sb = (SchedulingBlock)candidate;
145            
146            if (this.allows(sb))
147              selection.add(sb);
148          }
149        }
150        
151        return selection;
152      }
153    }