X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fconfig%2Fnetty-timer-config%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fnetty%2Ftimer%2FHashedWheelTimerCloseable.java;fp=opendaylight%2Fconfig%2Fnetty-timer-config%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fnetty%2Ftimer%2FHashedWheelTimerCloseable.java;h=4b23af11a7d2fbe263b3d455b0571cebcd8f1667;hb=9832745f5d61833705269a0004a83b39e81bddce;hp=0000000000000000000000000000000000000000;hpb=44b5dbe52783a3a558b3eae619b01baf613f82e0;p=controller.git diff --git a/opendaylight/config/netty-timer-config/src/main/java/org/opendaylight/controller/config/yang/netty/timer/HashedWheelTimerCloseable.java b/opendaylight/config/netty-timer-config/src/main/java/org/opendaylight/controller/config/yang/netty/timer/HashedWheelTimerCloseable.java new file mode 100644 index 0000000000..4b23af11a7 --- /dev/null +++ b/opendaylight/config/netty-timer-config/src/main/java/org/opendaylight/controller/config/yang/netty/timer/HashedWheelTimerCloseable.java @@ -0,0 +1,73 @@ +/* + * 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 javax.annotation.Nullable; + +public final class HashedWheelTimerCloseable implements AutoCloseable, Timer { + + private final Timer timer; + + private HashedWheelTimerCloseable(Timer timer) { + this.timer = timer; + } + + @Override + public void close() throws Exception { + stop(); + } + + @Override + public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { + return this.timer.newTimeout(task, delay, unit); + } + + @Override + public Set stop() { + return this.timer.stop(); + } + + public static HashedWheelTimerCloseable newInstance(@Nullable ThreadFactory threadFactory, + @Nullable Long duration, @Nullable Integer ticksPerWheel) { + TimeUnit unit = TimeUnit.MILLISECONDS; + if(!nullOrNonZero(duration) && threadFactory == null && nullOrNonZero(ticksPerWheel)) { + return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit)); + } + + if(!nullOrNonZero(duration) && threadFactory == null && !nullOrNonZero(ticksPerWheel)) { + return new HashedWheelTimerCloseable(new HashedWheelTimer(duration, unit, ticksPerWheel)); + } + + if(nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) { + return new HashedWheelTimerCloseable(new HashedWheelTimer(threadFactory)); + } + + if(!nullOrNonZero(duration) && threadFactory != null && nullOrNonZero(ticksPerWheel)) { + return new HashedWheelTimerCloseable( + new HashedWheelTimer(threadFactory, duration, unit)); + } + + if(!nullOrNonZero(duration) && threadFactory != null && !nullOrNonZero(ticksPerWheel)) { + return new HashedWheelTimerCloseable( + new HashedWheelTimer(threadFactory, duration, unit, ticksPerWheel)); + } + + return new HashedWheelTimerCloseable(new HashedWheelTimer()); + } + + private static boolean nullOrNonZero(Number n) { + return n == null || n.longValue() <= 0; + } +} \ No newline at end of file