View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel;
17  
18  
19  /**
20   * The {@link ReceiveBufferSizePredictorFactory} that creates a new
21   * {@link AdaptiveReceiveBufferSizePredictor}.
22   */
23  public class AdaptiveReceiveBufferSizePredictorFactory implements
24          ReceiveBufferSizePredictorFactory {
25  
26      private final int minimum;
27      private final int initial;
28      private final int maximum;
29  
30      /**
31       * Creates a new factory with the default parameters.  With the default
32       * parameters, the expected buffer size starts from {@code 1024}, does not
33       * go down below {@code 64}, and does not go up above {@code 65536}.
34       */
35      public AdaptiveReceiveBufferSizePredictorFactory() {
36          this(AdaptiveReceiveBufferSizePredictor.DEFAULT_MINIMUM,
37                  AdaptiveReceiveBufferSizePredictor.DEFAULT_INITIAL,
38                  AdaptiveReceiveBufferSizePredictor.DEFAULT_MAXIMUM);
39      }
40  
41      /**
42       * Creates a new factory with the specified parameters.
43       *
44       * @param minimum  the inclusive lower bound of the expected buffer size
45       * @param initial  the initial buffer size when no feed back was received
46       * @param maximum  the inclusive upper bound of the expected buffer size
47       */
48      public AdaptiveReceiveBufferSizePredictorFactory(int minimum, int initial, int maximum) {
49          if (minimum <= 0) {
50              throw new IllegalArgumentException("minimum: " + minimum);
51          }
52          if (initial < minimum) {
53              throw new IllegalArgumentException("initial: " + initial);
54          }
55          if (maximum < initial) {
56              throw new IllegalArgumentException("maximum: " + maximum);
57          }
58  
59          this.minimum = minimum;
60          this.initial = initial;
61          this.maximum = maximum;
62      }
63  
64      public ReceiveBufferSizePredictor getPredictor() throws Exception {
65          return new AdaptiveReceiveBufferSizePredictor(minimum, initial, maximum);
66      }
67  }