Fixed sonar issues in o.o.y.util package. 83/19283/2
authorTony Tkacik <ttkacik@cisco.com>
Wed, 29 Apr 2015 09:25:26 +0000 (11:25 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 3 Jul 2015 16:36:22 +0000 (16:36 +0000)
Change-Id: I577d3b3795340c33e2407bbecd821e5f1ff6cae7
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
common/util/src/main/java/org/opendaylight/yangtools/util/DurationStatisticsTracker.java
common/util/src/main/java/org/opendaylight/yangtools/util/HashCodeBuilder.java
common/util/src/main/java/org/opendaylight/yangtools/util/Identifiables.java
common/util/src/main/java/org/opendaylight/yangtools/util/Immutables.java
common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java
common/util/src/main/java/org/opendaylight/yangtools/util/concurrent/AsyncNotifyingListenableFutureTask.java
common/util/src/main/java/org/opendaylight/yangtools/util/concurrent/MappingCheckedFuture.java
common/util/src/main/java/org/opendaylight/yangtools/util/concurrent/NotificationManager.java

index 10cb39ce7571f6b629c80944d593795a9540a27c..810111d86bfa20d71242271bdb6097c07d0e83c0 100644 (file)
@@ -11,6 +11,7 @@ import static java.util.concurrent.TimeUnit.MICROSECONDS;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 import static java.util.concurrent.TimeUnit.SECONDS;
+
 import com.google.common.annotations.Beta;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -218,9 +219,9 @@ public abstract class DurationStatisticsTracker {
             return "h";
         case DAYS:
             return "d";
+        default:
+            LOG.warn("Unhandled time unit {}", unit);
+            return "";
         }
-
-        LOG.warn("Unhandled time unit {}", unit);
-        return "";
     }
 }
index 1b6b881556e0c8fab32154028436ade18ec64410..fc8da01f1ae01b9da077533f1b9abf9fbfeb2ec6 100644 (file)
@@ -16,6 +16,18 @@ import org.opendaylight.yangtools.concepts.Builder;
  * @param <T> Component objec type
  */
 public final class HashCodeBuilder<T> implements Builder<Integer> {
+    /**
+     *
+     * The value 31 was chosen because it is an odd prime. If it were even and the multiplication
+     * overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The
+     * advantage of using a prime is less clear, but it is traditional. A nice property of 31 is
+     * that the multiplication can be replaced by a shift and a subtraction for better performance:
+     * 31 * i == (i << 5) - i. Modern VMs do this sort of optimization automatically.
+     *
+     * (from Joshua Bloch's Effective Java, Chapter 3, Item 9: Always override hashcode when you
+     * override equals, page 48)
+     */
+    private static final int PRIME = 31;
     private int currentHash;
 
     /**
@@ -44,7 +56,7 @@ public final class HashCodeBuilder<T> implements Builder<Integer> {
      * @return Combined hash code
      */
     public static int nextHashCode(final int hashCode, final Object obj) {
-        return 31 * hashCode + obj.hashCode();
+        return PRIME * hashCode + obj.hashCode();
     }
 
     /**
index 2e5ca558b71487a567066e4833304cea4379857a..313cffb0d4c6e956cb942936a4e5e999504f18bd 100644 (file)
@@ -7,16 +7,15 @@
  */
 package org.opendaylight.yangtools.util;
 
+import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.concepts.Identifiable;
 
-import com.google.common.base.Function;
-
 public final class Identifiables {
     private static final Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
         @Override
-        public Object apply(final @Nonnull Identifiable<Object> input) {
+        public Object apply(@Nonnull final Identifiable<Object> input) {
             Preconditions.checkNotNull(input);
             return input.getIdentifier();
         }
index 4ec7c5f917b46813dea363f314fa6af7078e2ded..3d3f02b4b0fd196310f58dcc753fcd312e0c014a 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.util;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -35,9 +36,7 @@ public final class Immutables {
      * @return true if object is known to be immutable false otherwise.
      */
     public static boolean isImmutable(final Object o) {
-        if (o == null) {
-            throw new IllegalArgumentException("Object should not be null");
-        }
+        Preconditions.checkArgument(o != null,"Object should not be null");
         if (o instanceof Mutable) {
             return false;
         } else if (o instanceof Immutable) {
index e8b1a3dca975adbfbba831ace82a1045557c9af5..38868cbc0fd5855899ee82abb021fa1c6cba197c 100644 (file)
@@ -12,13 +12,14 @@ import java.util.Collections;
 import java.util.EventListener;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
 
 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
 
-    private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
+    private final ConcurrentMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
     final Set<ListenerRegistration<T>> unmodifiableView;
 
     @SuppressWarnings("unchecked")
@@ -34,36 +35,35 @@ public class ListenerRegistry<T extends EventListener> implements Iterable<Liste
         return unmodifiableView;
     }
 
-    public ListenerRegistration<T> register(T listener) {
+    public ListenerRegistration<T> register(final T listener) {
         if (listener == null) {
             throw new IllegalArgumentException("Listener should not be null.");
         }
-        ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
+        final ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
         listeners.put(ret,ret);
         return ret;
     }
-    
-    public <L extends T> ListenerRegistration<L> registerWithType(L listener) {
-        ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
+
+    public <L extends T> ListenerRegistration<L> registerWithType(final L listener) {
+        final ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
         listeners.put(ret,ret);
         return ret;
     }
-    
+
     @Override
     public java.util.Iterator<ListenerRegistration<T>> iterator() {
         return unmodifiableView.iterator();
     }
 
     @SuppressWarnings("rawtypes")
-    private void remove(ListenerRegistrationImpl registration) {
+    private void remove(final ListenerRegistrationImpl registration) {
         listeners.remove(registration);
     }
 
-    private class ListenerRegistrationImpl<P extends EventListener> //
-            extends AbstractObjectRegistration<P> //
-            implements ListenerRegistration<P> {
+    private class ListenerRegistrationImpl<P extends EventListener> extends AbstractObjectRegistration<P> implements
+            ListenerRegistration<P> {
 
-        public ListenerRegistrationImpl(P instance) {
+        public ListenerRegistrationImpl(final P instance) {
             super(instance);
         }
 
index 2ba16931f18a8f687836415c8da213e2a6cc3bab..f77a99895ecd035f5cdd23b57eb086b53458d18d 100644 (file)
@@ -12,14 +12,11 @@ import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.ExecutionList;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListenableFutureTask;
-
 import java.util.concurrent.Callable;
 import java.util.concurrent.Executor;
 import java.util.concurrent.FutureTask;
-
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
index 48f69e2da3ba29832d82141d338c3c3233dd44fd..62385fdbc555a6c84e3c25601022783c2cffcb73 100644 (file)
@@ -8,15 +8,14 @@
 
 package org.opendaylight.yangtools.util.concurrent;
 
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.AbstractCheckedFuture;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 /**
  * An implementation of CheckedFuture that provides similar behavior for the <code>get</code> methods
@@ -33,11 +32,11 @@ import com.google.common.util.concurrent.ListenableFuture;
  * @param <V> The result type returned by this Future's get method
  * @param <X> The checked exception type
  */
-public class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
+public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
 
     private final Function<Exception, X> mapper;
 
-    private MappingCheckedFuture( ListenableFuture<V> delegate, Function<Exception, X> mapper ) {
+    private MappingCheckedFuture( final ListenableFuture<V> delegate, final Function<Exception, X> mapper ) {
         super( delegate );
         this.mapper = Preconditions.checkNotNull( mapper );
     }
@@ -51,16 +50,16 @@ public class MappingCheckedFuture<V, X extends Exception> extends AbstractChecke
      * @return a new <code>MappingCheckedFuture</code>
      */
     public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
-            ListenableFuture<V> delegate, Function<Exception, X> mapper ) {
+            final ListenableFuture<V> delegate, final Function<Exception, X> mapper ) {
         return new MappingCheckedFuture<V, X>( delegate, mapper );
     }
 
     @Override
-    protected X mapException( Exception e ) {
+    protected X mapException( final Exception e ) {
         return mapper.apply( e );
     }
 
-    private ExecutionException wrapInExecutionException( String message, final Exception e ) {
+    private ExecutionException wrapInExecutionException( final String message, final Exception e ) {
         return new ExecutionException( message, mapException( e ) );
     }
 
@@ -68,27 +67,27 @@ public class MappingCheckedFuture<V, X extends Exception> extends AbstractChecke
     public V get() throws InterruptedException, ExecutionException {
         try {
             return super.get();
-        } catch( InterruptedException e ) {
+        } catch( final InterruptedException e ) {
             Thread.currentThread().interrupt();
             throw wrapInExecutionException( "Operation was interrupted", e );
-        } catch( CancellationException e ) {
+        } catch( final CancellationException e ) {
             throw wrapInExecutionException( "Operation was cancelled", e );
-        } catch( ExecutionException e ) {
+        } catch( final ExecutionException e ) {
             throw wrapInExecutionException( e.getMessage(), e );
         }
     }
 
     @Override
-    public V get( long timeout, TimeUnit unit )
+    public V get( final long timeout, final TimeUnit unit )
             throws InterruptedException, ExecutionException, TimeoutException {
         try {
             return super.get( timeout, unit );
-        } catch( InterruptedException e ) {
+        } catch( final InterruptedException e ) {
             Thread.currentThread().interrupt();
             throw wrapInExecutionException( "Operation was interrupted", e );
-        } catch( CancellationException e ) {
+        } catch( final CancellationException e ) {
             throw wrapInExecutionException( "Operation was cancelled", e );
-        } catch( ExecutionException e ) {
+        } catch( final ExecutionException e ) {
             throw wrapInExecutionException( e.getMessage(), e );
         }
     }
index 41cc7dcc0edccbad853d8b66a8595de95d0e8b69..aee3c9967639bfff299acea8074a2d75c30a9fc6 100644 (file)
@@ -29,8 +29,7 @@ public interface NotificationManager<L, N> {
      * @param notification the notification to dispatch
      * @throws RejectedExecutionException if the notification can't be queued for dispatching
      */
-    void submitNotification( L listener, N notification )
-            throws RejectedExecutionException;
+    void submitNotification( L listener, N notification );
 
     /**
      * Submits notifications to be queued and dispatched to the given listener.
@@ -41,7 +40,6 @@ public interface NotificationManager<L, N> {
      * @param notifications the notifications to dispatch
      * @throws RejectedExecutionException if a notification can't be queued for dispatching
      */
-    void submitNotifications( L listener, Iterable<N> notifications )
-            throws RejectedExecutionException;
+    void submitNotifications( final L listener, Iterable<N> notifications);
 
 }
\ No newline at end of file