Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2009 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 package com.android.quicksearchbox;
     18 
     19 import android.util.Log;
     20 
     21 import java.util.ArrayList;
     22 
     23 /**
     24  * A promoter that promotes one suggestion from each source.
     25  *
     26  */
     27 public class RoundRobinPromoter implements Promoter {
     28 
     29     private static final String TAG = "QSB.RoundRobinPromoter";
     30     private static final boolean DBG = false;
     31 
     32     /**
     33      * Creates a new RoundRobinPromoter.
     34      */
     35     public RoundRobinPromoter() {
     36     }
     37 
     38     public void pickPromoted(SuggestionCursor shortcuts,
     39             ArrayList<CorpusResult> suggestions, int maxPromoted,
     40             ListSuggestionCursor promoted) {
     41         if (DBG) Log.d(TAG, "pickPromoted(maxPromoted = " + maxPromoted + ")");
     42         final int sourceCount = suggestions.size();
     43         if (sourceCount == 0) return;
     44         int sourcePos = 0;
     45         int suggestionPos = 0;
     46         int maxCount = 0;
     47         // TODO: This is inefficient when there are several exhausted sources.
     48         while (promoted.getCount() < maxPromoted) {
     49             SuggestionCursor sourceResult = suggestions.get(sourcePos);
     50             int count = sourceResult.getCount();
     51             if (count > maxCount) maxCount = count;
     52             if (suggestionPos < count) {
     53                 if (DBG) Log.d(TAG, "Promoting " + sourcePos + ":" + suggestionPos);
     54                 promoted.add(new SuggestionPosition(sourceResult, suggestionPos));
     55             }
     56             sourcePos++;
     57             if (sourcePos >= sourceCount) {
     58                 sourcePos = 0;
     59                 suggestionPos++;
     60                 if (suggestionPos >= maxCount) break;
     61             }
     62         }
     63     }
     64 
     65 }
     66