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