Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.gh.bmd.jrt.builder;
 
 
 
 
 import static com.gh.bmd.jrt.util.TimeDuration.fromUnit;

Class storing the transport channel configuration.

Each instance is immutable, thus, in order to modify a configuration parameter, a new builder must be created starting from the specific configuration.

The configuration has an asynchronous runner associated.
The number of input data buffered in the channel can be limited in order to avoid excessive memory consumption. In case the maximum number is reached when passing an input, the call blocks until enough data are consumed or the specified timeout elapses. In the latter case, a TimeoutException will be thrown.
By default the timeout is set to 0 so to avoid unexpected deadlocks.
The order of input data is not guaranteed. Nevertheless, it is possible to force data to be delivered in the same order as they are passed to the channels, at the cost of a slightly increase in memory usage and computation.

Created by davide-maestroni on 03/07/15.

 
 public final class ChannelConfiguration {

    
Constant indicating the default value of an integer attribute.
 
     public static final int DEFAULT = .;
 
     private static final DefaultConfigurable sDefaultConfigurable = new DefaultConfigurable();

    
Empty configuration constant.
The configuration has all the options set to their default.
 
     public static final ChannelConfiguration DEFAULT_CONFIGURATION = builder().buildConfiguration();
 
     private final Runner mAsyncRunner;
 
     private final int mChannelMaxSize;
 
     private final OrderType mChannelOrderType;
 
     private final TimeDuration mChannelTimeout;
 
     private final Log mLog;
 
     private final LogLevel mLogLevel;
 
     private final TimeDuration mPassTimeout;
 
     private final TimeoutActionType mTimeoutActionType;

    
Constructor.

Parameters:
asyncRunner the runner used for asynchronous inputs.
passTimeout the timeout for the channel to produce a result.
actionType the action to be taken if the timeout elapses before a readable result is available.
channelOrderType the order in which data are collected from the output channel.
channelMaxSize the maximum number of buffered data. Must be positive.
channelTimeout the maximum timeout while waiting for an object to be passed to the channel.
log the log instance.
logLevel the log level.
 
     private ChannelConfiguration(@Nullable final Runner asyncRunner,
             @Nullable final TimeDuration passTimeout, @Nullable final TimeoutActionType actionType,
             @Nullable final OrderType channelOrderTypefinal int channelMaxSize,
             @Nullable final TimeDuration channelTimeout, @Nullable final Log log,
             @Nullable final LogLevel logLevel) {
 
          = asyncRunner;
          = passTimeout;
         = actionType;
         = channelOrderType;
         = channelMaxSize;
         = channelTimeout;
         = log;
         = logLevel;
    }

    
Returns a channel configuration builder.

Returns:
the builder.
    @Nonnull
    public static Builder<ChannelConfigurationbuilder() {
        return new Builder<ChannelConfiguration>();
    }

    
Returns a channel configuration builder initialized with the specified configuration.

Parameters:
initialConfiguration the initial configuration.
Returns:
the builder.
    @Nonnull
    public static Builder<ChannelConfigurationbuilderFrom(
            @Nullable final ChannelConfiguration initialConfiguration) {
        return (initialConfiguration == null) ? builder()
                : new Builder<ChannelConfiguration>(initialConfiguration);
    }

    
Returns a channel configuration builder initialized with this configuration.

Returns:
the builder.
    @Nonnull
        return builderFrom(this);
    }

    
Returns the runner used for asynchronous inputs (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the runner instance.
    public Runner getAsyncRunnerOr(@Nullable final Runner valueIfNotSet) {
        final Runner runner = ;
        return (runner != null) ? runner : valueIfNotSet;
    }

    
Returns the maximum number of buffered data (DEFAULT by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the maximum size.
    public int getChannelMaxSizeOr(final int valueIfNotSet) {
        final int maxSize = ;
        return (maxSize != ) ? maxSize : valueIfNotSet;
    }

    
Returns the data order (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the order type.
    public OrderType getChannelOrderTypeOr(@Nullable final OrderType valueIfNotSet) {
        final OrderType orderType = ;
        return (orderType != null) ? orderType : valueIfNotSet;
    }

    
Returns the maximum timeout while waiting for an object to be passed to the channel (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the timeout.
    public TimeDuration getChannelTimeoutOr(@Nullable final TimeDuration valueIfNotSet) {
        final TimeDuration timeout = ;
        return (timeout != null) ? timeout : valueIfNotSet;
    }

    
Returns the log level (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the log level.
    public LogLevel getLogLevelOr(@Nullable final LogLevel valueIfNotSet) {
        final LogLevel logLevel = ;
        return (logLevel != null) ? logLevel : valueIfNotSet;
    }

    
Returns the log instance (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the log instance.
    public Log getLogOr(@Nullable final Log valueIfNotSet) {
        final Log log = ;
        return (log != null) ? log : valueIfNotSet;
    }

    
Returns the action to be taken if the timeout elapses before a readable result is available (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the action type.
            @Nullable final TimeoutActionType valueIfNotSet) {
        final TimeoutActionType timeoutActionType = ;
        return (timeoutActionType != null) ? timeoutActionType : valueIfNotSet;
    }

    
Returns the timeout for the channel to produce a readable result (null by default).

Parameters:
valueIfNotSet the default value if none was set.
Returns:
the timeout.
    public TimeDuration getPassTimeoutOr(@Nullable final TimeDuration valueIfNotSet) {
        final TimeDuration passTimeout = ;
        return (passTimeout != null) ? passTimeout : valueIfNotSet;
    }
    @Override
    public int hashCode() {
        // auto-generated code
        int result =  != null ? .hashCode() : 0;
        result = 31 * result + ;
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        return result;
    }
    @Override
    @SuppressWarnings("SimplifiableIfStatement")
    public boolean equals(final Object o) {
        // auto-generated code
        if (this == o) {
            return true;
        }
        if (!(o instanceof ChannelConfiguration)) {
            return false;
        }
        final ChannelConfiguration that = (ChannelConfigurationo;
        if ( != that.mChannelMaxSize) {
            return false;
        }
        if ( != null ? !.equals(that.mAsyncRunner)
                : that.mAsyncRunner != null) {
            return false;
        }
        if ( != that.mChannelOrderType) {
            return false;
        }
        if ( != null ? !.equals(that.mChannelTimeout)
                : that.mChannelTimeout != null) {
            return false;
        }
        if ( != null ? !.equals(that.mLog) : that.mLog != null) {
            return false;
        }
        if ( != that.mLogLevel) {
            return false;
        }
        if ( != null ? !.equals(that.mPassTimeout)
                : that.mPassTimeout != null) {
            return false;
        }
        return  == that.mTimeoutActionType;
    }
    @Override
    public String toString() {
        return "ChannelConfiguration{" +
                "mAsyncRunner=" +  +
                ", mChannelMaxSize=" +  +
                ", mChannelOrderType=" +  +
                ", mChannelTimeout=" +  +
                ", mLog=" +  +
                ", mLogLevel=" +  +
                ", mPassTimeout=" +  +
                ", mTimeoutActionType=" +  +
                '}';
    }

    
Interface defining a configurable object.

Parameters:
<TYPE> the configurable object type.
    public interface Configurable<TYPE> {

        
Sets the specified configuration and returns the configurable instance.

Parameters:
configuration the configuration.
Returns:
the configurable instance.
        @Nonnull
        TYPE setConfiguration(@Nonnull ChannelConfiguration configuration);
    }

    
Builder of channel configurations.

Parameters:
<TYPE> the configurable object type.
    public static final class Builder<TYPE> {
        private final Configurable<? extends TYPE> mConfigurable;
        private Runner mAsyncRunner;
        private int mChannelMaxSize;
        private OrderType mChannelOrderType;
        private TimeDuration mChannelTimeout;
        private Log mLog;
        private LogLevel mLogLevel;
        private TimeDuration mPassTimeout;
        private TimeoutActionType mTimeoutActionType;

        
Constructor.

Parameters:
configurable the configurable instance.
        @SuppressWarnings("ConstantConditions")
        public Builder(@Nonnull final Configurable<? extends TYPE> configurable) {
            if (configurable == null) {
                throw new NullPointerException("the configurable instance must no be null");
            }
             = configurable;
             = ;
        }

        
Constructor.

Parameters:
configurable the configurable instance.
initialConfiguration the initial configuration.
        @SuppressWarnings("ConstantConditions")
        public Builder(@Nonnull final Configurable<? extends TYPE> configurable,
                @Nonnull final ChannelConfiguration initialConfiguration) {
            if (configurable == null) {
                throw new NullPointerException("the configurable instance must no be null");
            }
             = configurable;
            setConfiguration(initialConfiguration);
        }

        
Sets the configuration and returns the configurable object.

Returns:
the configurable object.
        @Nonnull
        public TYPE set() {
            return .setConfiguration(buildConfiguration());
        }

        
Applies the specified configuration to this builder. A null value means that all the configuration options need to be set to their default value, otherwise only the set options will be applied.

Parameters:
configuration the channel configuration.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> with(@Nullable final ChannelConfiguration configuration) {
            if (configuration == null) {
                setConfiguration();
                return this;
            }
            final Runner asyncRunner = configuration.mAsyncRunner;
            if (asyncRunner != null) {
                withAsyncRunner(asyncRunner);
            }
            final TimeDuration passTimeout = configuration.mPassTimeout;
            if (passTimeout != null) {
                withPassTimeout(passTimeout);
            }
            final TimeoutActionType timeoutActionType = configuration.mTimeoutActionType;
            if (timeoutActionType != null) {
                withPassTimeoutAction(timeoutActionType);
            }
            final OrderType orderType = configuration.mChannelOrderType;
            if (orderType != null) {
                withChannelOrder(orderType);
            }
            final int maxSize = configuration.mChannelMaxSize;
            if (maxSize != ) {
                withChannelMaxSize(maxSize);
            }
            final TimeDuration channelTimeout = configuration.mChannelTimeout;
            if (channelTimeout != null) {
                withChannelTimeout(channelTimeout);
            }
            final Log log = configuration.mLog;
            if (log != null) {
                withLog(log);
            }
            final LogLevel logLevel = configuration.mLogLevel;
            if (logLevel != null) {
                withLogLevel(logLevel);
            }
            return this;
        }

        
Sets the asynchronous runner instance. A null value means that it is up to the specific implementation to choose a default one.

Parameters:
runner the runner instance.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withAsyncRunner(@Nullable final Runner runner) {
             = runner;
            return this;
        }

        
Sets the maximum number of data that the channel can retain before they are consumed. A ChannelConfiguration.DEFAULT value means that it is up to the specific implementation to choose a default one.

Parameters:
maxSize the maximum size.
Returns:
this builder.
Throws:
java.lang.IllegalArgumentException if the number is less than 1.
        @Nonnull
        public Builder<TYPE> withChannelMaxSize(final int maxSize) {
            if ((maxSize != ) && (maxSize <= 0)) {
                throw new IllegalArgumentException(
                        "the channel buffer size cannot be 0 or negative: " + maxSize);
            }
             = maxSize;
            return this;
        }

        
Sets the order in which data are collected from the channel. A null value means that it is up to the specific implementation to choose a default one.
Note that this is just the initial configuration, since the channel order can be dynamically changed through the dedicated methods.

Parameters:
orderType the order type.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withChannelOrder(@Nullable final OrderType orderType) {
             = orderType;
            return this;
        }

        
Sets the timeout for the channel to have room for additional data.

Parameters:
timeout the timeout.
timeUnit the timeout time unit.
Returns:
this builder.
Throws:
java.lang.IllegalArgumentException if the specified timeout is negative.
        @Nonnull
        public Builder<TYPE> withChannelTimeout(final long timeout,
                @Nonnull final TimeUnit timeUnit) {
            return withChannelTimeout(fromUnit(timeouttimeUnit));
        }

        
Sets the timeout for the channel to have room for additional data. A null value means that it is up to the specific implementation to choose a default one.

Parameters:
timeout the timeout.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withChannelTimeout(@Nullable final TimeDuration timeout) {
             = timeout;
            return this;
        }

        
Sets the log instance. A null value means that it is up to the specific implementation to choose a default one.

Parameters:
log the log instance.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withLog(@Nullable final Log log) {
             = log;
            return this;
        }

        
Sets the log level. A null value means that it is up to the specific implementation to choose a default one.

Parameters:
level the log level.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withLogLevel(@Nullable final LogLevel level) {
             = level;
            return this;
        }

        
Sets the timeout for the channel instance to produce a readable result.
Note that this is just the initial configuration, since the output timeout can be dynamically changed through the dedicated methods.

Parameters:
timeout the timeout.
timeUnit the timeout time unit.
Returns:
this builder.
Throws:
java.lang.IllegalArgumentException if the specified timeout is negative.
        @Nonnull
        public Builder<TYPE> withPassTimeout(final long timeout, @Nonnull final TimeUnit timeUnit) {
            return withPassTimeout(fromUnit(timeouttimeUnit));
        }

        
Sets the timeout for the channel instance to produce a readable result. A null value means that it is up to the specific implementation to choose a default one.
Note that this is just the initial configuration, since the output timeout can be dynamically changed through the dedicated methods.

Parameters:
timeout the timeout.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withPassTimeout(@Nullable final TimeDuration timeout) {
             = timeout;
            return this;
        }

        
Sets the action to be taken if the timeout elapses before a result can be read from the output channel. A null value means that it is up to the specific implementation to choose a default one.
Note that this is just the initial configuration, since the output timeout action can be dynamically changed through the dedicated methods.

Parameters:
actionType the action type.
Returns:
this builder.
        @Nonnull
        public Builder<TYPE> withPassTimeoutAction(@Nullable final TimeoutActionType actionType) {
             = actionType;
            return this;
        }
        @Nonnull
        private ChannelConfiguration buildConfiguration() {
            return new ChannelConfiguration(,
                                            ,
                                            );
        }
        private void setConfiguration(@Nonnull final ChannelConfiguration configuration) {
             = configuration.mAsyncRunner;
             = configuration.mPassTimeout;
             = configuration.mTimeoutActionType;
             = configuration.mChannelOrderType;
             = configuration.mChannelMaxSize;
             = configuration.mChannelTimeout;
             = configuration.mLog;
             = configuration.mLogLevel;
        }
    }

    
Default configurable implementation.
    private static class DefaultConfigurable implements Configurable<ChannelConfiguration> {
        @Nonnull
        public ChannelConfiguration setConfiguration(
                @Nonnull final ChannelConfiguration configuration) {
            return configuration;
        }
    }
New to GrepCode? Check out our FAQ X