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 17 package com.android.quicksearchbox.util 18 19 import kotlin.collections.HashMap 20 21 /** 22 * Uses a separate executor for each task name. 23 * @param executorFactory Used to run the commands. 24 */ 25 class PerNameExecutor(private val mExecutorFactory: Factory<NamedTaskExecutor>) : 26 NamedTaskExecutor { 27 private var mExecutors: HashMap<String, NamedTaskExecutor>? = null 28 29 @Synchronized cancelPendingTasksnull30 override fun cancelPendingTasks() { 31 if (mExecutors == null) return 32 for (executor in mExecutors!!.values) { 33 executor.cancelPendingTasks() 34 } 35 } 36 37 @Synchronized closenull38 override fun close() { 39 if (mExecutors == null) return 40 for (executor in mExecutors!!.values) { 41 executor.close() 42 } 43 } 44 45 @Synchronized executenull46 override fun execute(task: NamedTask?) { 47 if (mExecutors == null) { 48 mExecutors = HashMap<String, NamedTaskExecutor>() 49 } 50 val name: String? = task?.name 51 var executor: NamedTaskExecutor? = mExecutors?.get(name) 52 if (executor == null) { 53 executor = mExecutorFactory.create() 54 mExecutors?.put(name!!, executor) 55 } 56 executor.execute(task) 57 } 58 } 59