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;
return "h";
case DAYS:
return "d";
+ default:
+ LOG.warn("Unhandled time unit {}", unit);
+ return "";
}
-
- LOG.warn("Unhandled time unit {}", unit);
- return "";
}
}
* @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;
/**
* @return Combined hash code
*/
public static int nextHashCode(final int hashCode, final Object obj) {
- return 31 * hashCode + obj.hashCode();
+ return PRIME * hashCode + obj.hashCode();
}
/**
*/
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();
}
*/
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;
* @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) {
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")
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);
}
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;
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
* @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 );
}
* @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 ) );
}
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 );
}
}
* @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.
* @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