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 org.eclipse.jdt.annotation.Nullable;
19 public final class HashedWheelTimerCloseable implements AutoCloseable, Timer {
21 private final Timer timer;
23 private HashedWheelTimerCloseable(final Timer timer) {
33 public Timeout newTimeout(final TimerTask task, final long delay, final 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(final @Nullable Long duration,
43 final @Nullable Integer ticksPerWheel) {
44 return newInstance(null, duration, ticksPerWheel);
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));
54 if(!nullOrNonPositive(duration) && threadFactory == null && !nullOrNonPositive(ticksPerWheel)) {
55 return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel));
58 if(nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) {
59 return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory));
62 if(!nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) {
63 return new HashedWheelTimerCloseable(
64 new HashedWheelTimer(threadFactory, duration, unit));
67 if(!nullOrNonPositive(duration) && threadFactory != null && !nullOrNonPositive(ticksPerWheel)) {
68 return new HashedWheelTimerCloseable(
69 new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel));
72 return new HashedWheelTimerCloseable(new HashedWheelTimer());
75 private static boolean nullOrNonPositive(final Number n) {
76 return n == null || n.longValue() <= 0;