Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / TestingFixedThreadPool.java
1 /*
2  * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.testingservices.threadpool;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.collect.Lists;
14 import java.io.Closeable;
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ThreadPoolExecutor;
22
23 public class TestingFixedThreadPool implements TestingThreadPoolIfc, Closeable,
24         TestingModifiableThreadPoolIfc {
25     private final ThreadPoolExecutor executorService;
26     private final String uniqueName;
27
28     public static void cleanUp() {
29         for (ExecutorService executorService : ALL_EXECUTORS) {
30             executorService.shutdown();
31         }
32         ALL_EXECUTORS.clear();
33     }
34
35     // for verification purposes:
36     public static final List<ThreadPoolExecutor> ALL_EXECUTORS = Collections
37             .synchronizedList(Lists.<ThreadPoolExecutor>newLinkedList());
38
39     public TestingFixedThreadPool(final int threadCount, final String uniqueName) {
40         checkNotNull(uniqueName);
41         this.uniqueName = uniqueName;
42         executorService = (ThreadPoolExecutor) Executors
43                 .newFixedThreadPool(threadCount);
44         ALL_EXECUTORS.add(executorService);
45     }
46
47     @Override
48     public Executor getExecutor() {
49         return executorService;
50     }
51
52     @Override
53     public void close() throws IOException {
54         executorService.shutdown();
55         ALL_EXECUTORS.remove(executorService);
56
57     }
58
59     @Override
60     public int getMaxNumberOfThreads() {
61         return executorService.getMaximumPoolSize();
62     }
63
64     public String getUniqueName() {
65         return uniqueName;
66     }
67
68     @Override
69     public void setMaximumNumberOfThreads(final int activeCount) {
70         checkArgument(activeCount > 0);
71         executorService.setMaximumPoolSize(activeCount);
72     }
73 }