Add blueprint wiring for netty configs
[controller.git] / opendaylight / config / netty-timer-config / src / main / java / org / opendaylight / controller / config / yang / netty / timer / HashedWheelTimerCloseable.java
1 /*
2  * Copyright (c) 2013 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.yang.netty.timer;
9
10 import io.netty.util.HashedWheelTimer;
11 import io.netty.util.Timeout;
12 import io.netty.util.Timer;
13 import io.netty.util.TimerTask;
14 import java.util.Set;
15 import java.util.concurrent.ThreadFactory;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nullable;
18
19 public final class HashedWheelTimerCloseable implements AutoCloseable, Timer {
20
21     private final Timer timer;
22
23     private HashedWheelTimerCloseable(Timer timer) {
24         this.timer = timer;
25     }
26
27     @Override
28     public void close() throws Exception {
29         stop();
30     }
31
32     @Override
33     public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
34         return this.timer.newTimeout(task, delay, unit);
35     }
36
37     @Override
38     public Set<Timeout> stop() {
39         return this.timer.stop();
40     }
41
42     public static HashedWheelTimerCloseable newInstance(@Nullable ThreadFactory threadFactory,
43             @Nullable Long duration, @Nullable Integer ticksPerWheel) {
44         TimeUnit unit = TimeUnit.MILLISECONDS;
45         if(!nullOrNonZero(duration) && threadFactory == null && nullOrNonZero(ticksPerWheel)) {
46             return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit));
47         }
48
49         if(!nullOrNonZero(duration) && threadFactory == null && !nullOrNonZero(ticksPerWheel)) {
50             return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel));
51         }
52
53         if(nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) {
54             return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory));
55         }
56
57         if(!nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) {
58             return new HashedWheelTimerCloseable(
59                     new HashedWheelTimer(threadFactory, duration, unit));
60         }
61
62         if(!nullOrNonZero(duration) && threadFactory != null && !nullOrNonZero(ticksPerWheel)) {
63             return new HashedWheelTimerCloseable(
64                     new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel));
65         }
66
67         return new HashedWheelTimerCloseable(new HashedWheelTimer());
68     }
69
70     private static boolean nullOrNonZero(Number n) {
71         return n == null || n.longValue() <= 0;
72     }
73 }