1 /*
2  * Copyright (C) 2022 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 package com.android.quicksearchbox.util
17 
18 import java.util.ArrayList
19 import java.util.concurrent.locks.Condition
20 import java.util.concurrent.locks.Lock
21 import java.util.concurrent.locks.ReentrantLock
22 
23 /**
24  * A consumer that consumes a fixed number of values. When the expected number of values has been
25  * consumed, further values are rejected.
26  */
27 class BarrierConsumer<A>(private val mExpectedCount: Int) : Consumer<A> {
28   private val mLock: Lock = ReentrantLock()
29   private val mNotFull: Condition = mLock.newCondition()
30 
31   // Set to null when getValues() returns.
32   private var mValues: ArrayList<A>?
33 
34   /**
35    * Blocks until the expected number of results is available, or until the thread is interrupted.
36    * This method should not be called multiple times.
37    *
38    * @return A list of values, never `null`.
39    */
40   val values: ArrayList<A>?
41     get() {
42       mLock.lock()
43       return try {
44         try {
45           while (!isFull) {
46             mNotFull.await()
47           }
48         } catch (ex: InterruptedException) {
49           // Return the values that we've gotten so far
50         }
51         val values = mValues
52         mValues = null // mark that getValues() has returned
53         values
54       } finally {
55         mLock.unlock()
56       }
57     }
58 
consumenull59   override fun consume(value: A): Boolean {
60     mLock.lock()
61     return try {
62       // Do nothing if getValues() has already returned,
63       // or enough values have already been consumed
64       if (mValues == null || isFull) {
65         return false
66       }
67       mValues?.add(value)
68       if (isFull) {
69         // Wake up any thread waiting in getValues()
70         mNotFull.signal()
71       }
72       true
73     } finally {
74       mLock.unlock()
75     }
76   }
77 
78   private val isFull: Boolean
79     get() = mValues!!.size == mExpectedCount
80 
81   /**
82    * Constructs a new BarrierConsumer.
83    *
84    * @param expectedCount The number of values to consume.
85    */
86   init {
87     mValues = ArrayList<A>(mExpectedCount)
88   }
89 }
90