DefaultExecutorServiceManager.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core;
19 
20 import griffon.core.ExecutorServiceManager;
21 
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import java.util.Collection;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27 import java.util.concurrent.ExecutorService;
28 
29 import static java.util.Collections.unmodifiableSet;
30 
31 /**
32  @author Andres Almiray
33  @since 2.0.0
34  */
35 public class DefaultExecutorServiceManager implements ExecutorServiceManager {
36     private final Set<ExecutorService> executorServices = new LinkedHashSet<>();
37 
38     @Nonnull
39     @Override
40     public Collection<ExecutorService> getExecutorServices() {
41         return unmodifiableSet(executorServices);
42     }
43 
44     @Override
45     @Nullable
46     public ExecutorService add(@Nullable ExecutorService executorService) {
47         if (executorService != null) {
48             executorServices.add(executorService);
49         }
50         return executorService;
51     }
52 
53     @Override
54     @Nullable
55     public ExecutorService remove(@Nullable ExecutorService executorService) {
56         executorServices.remove(executorService);
57         return executorService;
58     }
59 
60     @Override
61     public void shutdownAll() {
62         for (ExecutorService executorService : executorServices) {
63             if (!executorService.isShutdown()) {
64                 executorService.shutdown();
65             }
66         }
67     }
68 }