b519af1b99581306e7d5418bd2de820db5659ef1
[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 org.eclipse.jdt.annotation.Nullable;
18
19 public final class HashedWheelTimerCloseable implements AutoCloseable, Timer {
20
21     private final Timer timer;
22
23     private HashedWheelTimerCloseable(final Timer timer) {
24         this.timer = timer;
25     }
26
27     @Override
28     public void close() {
29         stop();
30     }
31
32     @Override
33     public Timeout newTimeout(final TimerTask task, final long delay, final 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(final @Nullable Long duration,
43             final @Nullable Integer ticksPerWheel) {
44         return newInstance(null, duration, ticksPerWheel);
45     }
46
47     public static HashedWheelTimerCloseable newInstance(final @Nullable ThreadFactory threadFactory,
48             final @Nullable Long duration, final @Nullable Integer ticksPerWheel) {
49         TimeUnit unit = TimeUnit.MILLISECONDS;
50         if(!nullOrNonPositive(duration) && threadFactory == null && nullOrNonPositive(ticksPerWheel)) {
51             return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit));
52         }
53
54         if(!nullOrNonPositive(duration) && threadFactory == null && !nullOrNonPositive(ticksPerWheel)) {
55             return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel));
56         }
57
58         if(nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) {
59             return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory));
60         }
61
62         if(!nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) {
63             return new HashedWheelTimerCloseable(
64                     new HashedWheelTimer(threadFactory, duration, unit));
65         }
66
67         if(!nullOrNonPositive(duration) && threadFactory != null && !nullOrNonPositive(ticksPerWheel)) {
68             return new HashedWheelTimerCloseable(
69                     new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel));
70         }
71
72         return new HashedWheelTimerCloseable(new HashedWheelTimer());
73     }
74
75     private static boolean nullOrNonPositive(final Number n) {
76         return n == null || n.longValue() <= 0;
77     }
78 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.