Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 package android.filterfw.core;
     19 
     20 import java.util.Set;
     21 
     22 import android.filterfw.core.Filter;
     23 import android.filterfw.core.Scheduler;
     24 
     25 /**
     26  * @hide
     27  */
     28 public class RoundRobinScheduler extends Scheduler {
     29 
     30     private int mLastPos = -1;
     31 
     32     public RoundRobinScheduler(FilterGraph graph) {
     33         super(graph);
     34     }
     35 
     36     @Override
     37     public void reset() {
     38         mLastPos = -1;
     39     }
     40 
     41     @Override
     42     public Filter scheduleNextNode() {
     43         Set<Filter> all_filters = getGraph().getFilters();
     44         if (mLastPos >= all_filters.size()) mLastPos = -1;
     45         int pos = 0;
     46         Filter first = null;
     47         int firstNdx = -1;
     48         for (Filter filter : all_filters) {
     49             if (filter.canProcess()) {
     50                 if (pos <= mLastPos) {
     51                     if (first == null) {
     52                         // store the first available filter
     53                         first = filter;
     54                         firstNdx = pos;
     55                     }
     56                 } else {
     57                     // return the next available filter since last
     58                     mLastPos = pos;
     59                     return filter;
     60                 }
     61             }
     62             pos ++;
     63         }
     64         // going around from the beginning
     65         if (first != null ) {
     66             mLastPos = firstNdx;
     67             return first;
     68         }
     69         // if there is nothing to be scheduled, still keep the previous
     70         // position.
     71         return null;
     72     }
     73 }
     74