1 /* 2 * Copyright (C) 2020 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.settings.network.ims; 18 19 import java.util.concurrent.Semaphore; 20 import java.util.concurrent.TimeUnit; 21 import java.util.concurrent.atomic.AtomicBoolean; 22 import java.util.function.Consumer; 23 24 class BooleanConsumer extends Semaphore implements Consumer<Boolean> { 25 26 private static final String TAG = "BooleanConsumer"; 27 BooleanConsumer()28 BooleanConsumer() { 29 super(0); 30 mValue = new AtomicBoolean(); 31 } 32 33 private volatile AtomicBoolean mValue; 34 35 /** 36 * Get boolean value reported from callback 37 * 38 * @param timeout callback waiting time in milliseconds 39 * @return boolean value reported 40 * @throws InterruptedException when thread get interrupted 41 */ get(long timeout)42 boolean get(long timeout) throws InterruptedException { 43 tryAcquire(timeout, TimeUnit.MILLISECONDS); 44 return mValue.get(); 45 } 46 47 /** 48 * Implementation of {@link Consumer#accept(Boolean)} 49 * 50 * @param value boolean reported from {@link Consumer#accept(Boolean)} 51 */ accept(Boolean value)52 public void accept(Boolean value) { 53 if (value != null) { 54 mValue.set(value.booleanValue()); 55 } 56 release(); 57 } 58 } 59