2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.netty.timer;
10 import io.netty.util.HashedWheelTimer;
11 import io.netty.util.Timeout;
12 import io.netty.util.Timer;
13 import io.netty.util.TimerTask;
15 import java.util.concurrent.ThreadFactory;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nullable;
19 public final class HashedWheelTimerCloseable implements AutoCloseable, Timer {
21 private final Timer timer;
23 private HashedWheelTimerCloseable(Timer timer) {
28 public void close() throws Exception {
33 public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
34 return this.timer.newTimeout(task, delay, unit);
38 public Set<Timeout> stop() {
39 return this.timer.stop();
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));
49 if(!nullOrNonZero(duration) && threadFactory == null && !nullOrNonZero(ticksPerWheel)) {
50 return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel));
53 if(nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) {
54 return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory));
57 if(!nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) {
58 return new HashedWheelTimerCloseable(
59 new HashedWheelTimer(threadFactory, duration, unit));
62 if(!nullOrNonZero(duration) && threadFactory != null && !nullOrNonZero(ticksPerWheel)) {
63 return new HashedWheelTimerCloseable(
64 new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel));
67 return new HashedWheelTimerCloseable(new HashedWheelTimer());
70 private static boolean nullOrNonZero(Number n) {
71 return n == null || n.longValue() <= 0;