/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.config.yang.netty.timer; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; import java.util.Set; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.Nullable; public final class HashedWheelTimerCloseable implements AutoCloseable, Timer { private final Timer timer; private HashedWheelTimerCloseable(final Timer timer) { this.timer = timer; } @Override public void close() { stop(); } @Override public Timeout newTimeout(final TimerTask task, final long delay, final TimeUnit unit) { return this.timer.newTimeout(task, delay, unit); } @Override public Set stop() { return this.timer.stop(); } public static HashedWheelTimerCloseable newInstance(final @Nullable Long duration, final @Nullable Integer ticksPerWheel) { return newInstance(null, duration, ticksPerWheel); } public static HashedWheelTimerCloseable newInstance(final @Nullable ThreadFactory threadFactory, final @Nullable Long duration, final @Nullable Integer ticksPerWheel) { TimeUnit unit = TimeUnit.MILLISECONDS; if (!nullOrNonPositive(duration) && threadFactory == null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit)); } if (!nullOrNonPositive(duration) && threadFactory == null && !nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel)); } if (nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory)); } if (!nullOrNonPositive(duration) && threadFactory != null && nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable( new HashedWheelTimer(threadFactory, duration, unit)); } if (!nullOrNonPositive(duration) && threadFactory != null && !nullOrNonPositive(ticksPerWheel)) { return new HashedWheelTimerCloseable( new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel)); } return new HashedWheelTimerCloseable(new HashedWheelTimer()); } private static boolean nullOrNonPositive(final Number num) { return num == null || num.longValue() <= 0; } }