Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / TestingFixedThreadPoolModule.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.checkState;
11
12 import java.io.Closeable;
13 import javax.annotation.Nullable;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.config.api.ModuleIdentifier;
16 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
17 import org.opendaylight.controller.config.spi.Module;
18
19 @NotThreadSafe
20 public class TestingFixedThreadPoolModule implements
21         TestingFixedThreadPoolConfigMXBean, Module,
22         TestingThreadPoolConfigMXBean, ModifiableThreadPoolServiceInterface {
23     private final AutoCloseable oldCloseable;
24     private final TestingFixedThreadPool oldInstance;
25     private final ModuleIdentifier name;
26     private TestingFixedThreadPool instance;
27     private int threadCount = 0;
28     private boolean triggerNewInstanceCreation;
29
30     TestingFixedThreadPoolModule(final ModuleIdentifier name,
31             @Nullable final AutoCloseable oldCloseable,
32             @Nullable final TestingFixedThreadPool oldInstance) {
33         this.name = name;
34         this.oldCloseable = oldCloseable;
35         this.oldInstance = oldInstance;
36     }
37
38     // attributes
39     @Override
40     public void setThreadCount(final int threadCount) {
41         this.threadCount = threadCount;
42     }
43
44     @Override
45     public int getThreadCount() {
46         return threadCount;
47     }
48
49     @Override
50     public boolean isTriggerNewInstanceCreation() {
51         return triggerNewInstanceCreation;
52     }
53
54     @Override
55     public void setTriggerNewInstanceCreation(final boolean triggerNewInstanceCreation) {
56         this.triggerNewInstanceCreation = triggerNewInstanceCreation;
57     }
58
59     // operations
60
61     private boolean isReusable() {
62         return oldInstance != null;
63     }
64
65     @Override
66     public void validate() {
67         checkState(threadCount > 0,
68                 "Parameter 'threadCount' must be greater than 0");
69     }
70
71     @Override
72     public boolean canReuse(final Module oldModule) {
73         return isReusable() && triggerNewInstanceCreation == false;
74     }
75
76     @Override
77     @SuppressWarnings("IllegalCatch")
78     public Closeable getInstance() {
79         if (instance == null) {
80             if (isReusable() && triggerNewInstanceCreation == false) { // simulate
81                                                                        // big
82                                                                        // change
83                                                                        // using
84                                                                        // triggerNewInstanceCreation
85                 oldInstance.setMaximumNumberOfThreads(threadCount);
86                 instance = oldInstance;
87             } else {
88                 if (oldCloseable != null) {
89                     try {
90                         oldCloseable.close();
91                     } catch (final Exception e) {
92                         throw new RuntimeException(e);
93                     }
94                 }
95                 instance = new TestingFixedThreadPool(threadCount,
96                         name.toString());
97             }
98         }
99         return instance;
100     }
101
102     @Override
103     public ModuleIdentifier getIdentifier() {
104         return name;
105     }
106
107 }