* 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.yangtools.yang.common;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import java.io.Serializable;
import java.util.Collection;
-import java.util.Collections;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
private static class RpcResultImpl<T> implements RpcResult<T>, Serializable {
private static final long serialVersionUID = 1L;
- private final Collection<RpcError> errors;
+ private final ImmutableList<RpcError> errors;
private final T result;
private final boolean successful;
- RpcResultImpl( final boolean successful, final T result,
- final Collection<RpcError> errors ) {
+ RpcResultImpl(final boolean successful, final T result, final ImmutableList<RpcError> errors) {
this.successful = successful;
this.result = result;
- this.errors = errors;
+ this.errors = requireNonNull(errors);
}
@Override
- public Collection<RpcError> getErrors() {
+ public ImmutableList<RpcError> getErrors() {
return errors;
}
private final ErrorType errorType;
private final Throwable cause;
- RpcErrorImpl( final ErrorSeverity severity, final ErrorType errorType,
+ RpcErrorImpl(final ErrorSeverity severity, final ErrorType errorType,
final String tag, final String message, final String applicationTag, final String info,
- final Throwable cause ) {
+ final Throwable cause) {
this.severity = severity;
this.errorType = errorType;
this.tag = tag;
private T result;
private final boolean successful;
- private RpcResultBuilder( final boolean successful, final T result ) {
+ private RpcResultBuilder(final boolean successful, final T result) {
this.successful = successful;
this.result = result;
}
/**
* Returns a builder for a successful result.
*/
- public static <T> RpcResultBuilder<T> success() {
- return new RpcResultBuilder<T>( true, null );
+ public static <T> @NonNull RpcResultBuilder<T> success() {
+ return new RpcResultBuilder<>(true, null);
}
/**
*
* @param result the result value
*/
- public static <T> RpcResultBuilder<T> success( final T result ) {
- return new RpcResultBuilder<T>( true, result );
+ public static <T> @NonNull RpcResultBuilder<T> success(final T result) {
+ return new RpcResultBuilder<>(true, result);
}
/**
*
* @param builder builder for the result value
*/
- public static <T> RpcResultBuilder<T> success( final Builder<T> builder ) {
+ public static <T> @NonNull RpcResultBuilder<T> success(final Builder<T> builder) {
return success(builder.build());
}
/**
* Returns a builder for a failed result.
*/
- public static <T> RpcResultBuilder<T> failed() {
- return new RpcResultBuilder<T>( false, null );
+ public static <T> @NonNull RpcResultBuilder<T> failed() {
+ return new RpcResultBuilder<>(false, null);
}
/**
*
* @param success true if successful, false otherwise.
*/
- public static <T> RpcResultBuilder<T> status( final boolean success ) {
- return new RpcResultBuilder<T>( success, null );
+ public static <T> @NonNull RpcResultBuilder<T> status(final boolean success) {
+ return new RpcResultBuilder<>(success, null);
}
/**
*
* @param other the other RpcResult.
*/
- public static <T> RpcResultBuilder<T> from( final RpcResult<T> other ) {
- return new RpcResultBuilder<T>( other.isSuccessful(), other.getResult() )
- .withRpcErrors( other.getErrors() );
+ public static <T> @NonNull RpcResultBuilder<T> from(final RpcResult<T> other) {
+ return new RpcResultBuilder<>(other.isSuccessful(), other.getResult())
+ .withRpcErrors(other.getErrors());
}
/**
*
* @return an RpcError
*/
- public static RpcError newError( final ErrorType errorType, final String tag, final String message ) {
- return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
- tag != null ? tag : "operation-failed", message, null, null, null );
+ public static @NonNull RpcError newError(final ErrorType errorType, final String tag, final String message) {
+ return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
+ tag != null ? tag : "operation-failed", message, null, null, null);
}
/**
* @param tag a short string that identifies the general type of error condition. See
* {@link RpcError#getTag} for a list of suggested values.
* @param message a string suitable for human display that describes the error condition.
- * * @param applicationTag a short string that identifies the specific type of error condition.
+ * @param applicationTag a short string that identifies the specific type of error condition.
* @param info a string containing additional information to provide extended
* and/or implementation-specific debugging information.
* @param cause the exception that triggered the error.
*
* @return an RpcError
*/
- public static RpcError newError( final ErrorType errorType, final String tag, final String message,
- final String applicationTag, final String info, final Throwable cause ) {
- return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
- tag != null ? tag : "operation-failed", message, applicationTag, info, cause );
+ public static @NonNull RpcError newError(final ErrorType errorType, final String tag, final String message,
+ final String applicationTag, final String info, final Throwable cause) {
+ return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
+ tag != null ? tag : "operation-failed", message, applicationTag, info, cause);
}
/**
*
* @return an RpcError
*/
- public static RpcError newWarning( final ErrorType errorType, final String tag, final String message ) {
- return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
+ public static @NonNull RpcError newWarning(final ErrorType errorType, final String tag, final String message) {
+ return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
}
/**
* @param tag a short string that identifies the general type of warning condition. See
* {@link RpcError#getTag} for a list of suggested values.
* @param message a string suitable for human display that describes the warning condition.
- * * @param applicationTag a short string that identifies the specific type of warning condition.
+ * @param applicationTag a short string that identifies the specific type of warning condition.
* @param info a string containing additional information to provide extended
* and/or implementation-specific debugging information.
* @param cause the exception that triggered the warning.
*
* @return an RpcError
*/
- public static RpcError newWarning( final ErrorType errorType, final String tag, final String message,
- final String applicationTag, final String info, final Throwable cause ) {
- return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message,
- applicationTag, info, cause );
+ public static @NonNull RpcError newWarning(final ErrorType errorType, final String tag, final String message,
+ final String applicationTag, final String info, final Throwable cause) {
+ return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message,
+ applicationTag, info, cause);
}
/**
*
* @param result the result value
*/
- public RpcResultBuilder<T> withResult( final T result ) {
+ @SuppressWarnings("checkstyle:hiddenField")
+ public @NonNull RpcResultBuilder<T> withResult(final T result) {
this.result = result;
return this;
}
*
* @param builder builder for the result value
*/
- public RpcResultBuilder<T> withResult( final Builder<T> builder ) {
+ public @NonNull RpcResultBuilder<T> withResult(final Builder<T> builder) {
return withResult(builder.build());
}
- private void addError( final ErrorSeverity severity, final ErrorType errorType,
+ private void addError(final ErrorSeverity severity, final ErrorType errorType,
final String tag, final String message, final String applicationTag, final String info,
- final Throwable cause ) {
+ final Throwable cause) {
- addError( new RpcErrorImpl( severity, errorType,
+ addError(new RpcErrorImpl(severity, errorType,
tag != null ? tag : "operation-failed", message,
- applicationTag, info, cause ) );
+ applicationTag, info, cause));
}
- private void addError( final RpcError error ) {
+ private void addError(final RpcError error) {
if (errors == null) {
- errors = new ImmutableList.Builder<RpcError>();
+ errors = new ImmutableList.Builder<>();
}
- errors.add( error );
+ errors.add(error);
}
/**
* {@link RpcError#getTag} for a list of suggested values.
* @param message a string suitable for human display that describes the warning condition.
*/
- public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message ) {
- addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
+ public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message) {
+ addError(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
return this;
}
* and/or implementation-specific debugging information.
* @param cause the exception that triggered the warning.
*/
- public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message,
- final String applicationTag, final String info, final Throwable cause ) {
- addError( ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause );
+ public @NonNull RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message,
+ final String applicationTag, final String info, final Throwable cause) {
+ addError(ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause);
return this;
}
* @param errorType the conceptual layer at which the error occurred.
* @param message a string suitable for human display that describes the error condition.
*/
- public RpcResultBuilder<T> withError( final ErrorType errorType, final String message ) {
- addError( ErrorSeverity.ERROR, errorType, null, message, null, null, null );
+ public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String message) {
+ addError(ErrorSeverity.ERROR, errorType, null, message, null, null, null);
return this;
}
* {@link RpcError#getTag} for a list of suggested values.
* @param message a string suitable for human display that describes the error condition.
*/
- public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message ) {
- addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
+ public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message) {
+ addError(ErrorSeverity.ERROR, errorType, tag, message, null, null, null);
return this;
}
* @param message a string suitable for human display that describes the error condition.
* @param cause the exception that triggered the error.
*/
- public RpcResultBuilder<T> withError( final ErrorType errorType, final String message,
- final Throwable cause ) {
- addError( ErrorSeverity.ERROR, errorType, null, message, null, null, cause );
+ public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String message,
+ final Throwable cause) {
+ addError(ErrorSeverity.ERROR, errorType, null, message, null, null, cause);
return this;
}
* and/or implementation-specific debugging information.
* @param cause the exception that triggered the error.
*/
- public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message,
- final String applicationTag, final String info, final Throwable cause ) {
- addError( ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause );
+ public @NonNull RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message,
+ final String applicationTag, final String info, final Throwable cause) {
+ addError(ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause);
return this;
}
*
* @param error the RpcError
*/
- public RpcResultBuilder<T> withRpcError( final RpcError error ) {
- addError( error );
+ public @NonNull RpcResultBuilder<T> withRpcError(final RpcError error) {
+ addError(error);
return this;
}
/**
* Adds RpcErrors.
*
- * @param errors the list of RpcErrors
+ * @param rpcErrors the list of RpcErrors
*/
- public RpcResultBuilder<T> withRpcErrors( final Collection<RpcError> errors ) {
- if (errors != null) {
- for (RpcError error : errors) {
- addError( error );
+ public RpcResultBuilder<T> withRpcErrors(final Collection<? extends RpcError> rpcErrors) {
+ if (rpcErrors != null) {
+ for (RpcError error : rpcErrors) {
+ addError(error);
}
}
return this;
@Override
public RpcResult<T> build() {
-
- return new RpcResultImpl<T>( successful, result,
- errors != null ? errors.build() : Collections.<RpcError>emptyList() );
+ return new RpcResultImpl<>(successful, result, errors != null ? errors.build() : ImmutableList.of());
}
/**
- * Builds RpcResult and wraps it in a Future
+ * Builds RpcResult and wraps it in a Future.
*
- * This is a convenience method to assist those writing rpcs
- * that produce immediate results. It allows you to replace
- *
- * Futures.immediateFuture(rpcResult.build())
- *
- * with
- *
- * rpcResult.buildFuture();
+ * <p>
+ * This is a convenience method to assist those writing RPCs that produce immediate results. It allows you to
+ * replace {@code FluentFuture.from(Futures.immediateFuture(rpcResult.build()))} with
+ * {@code rpcResult.buildFuture()}
*
* @return Future for RpcResult built by RpcResultBuilder
- *
*/
- public ListenableFuture<RpcResult<T>> buildFuture() {
- return Futures.immediateFuture(build());
+ public @NonNull FluentFuture<RpcResult<T>> buildFuture() {
+ return FluentFuture.from(Futures.immediateFuture(build()));
}
}