View Javadoc
1   /*
2    * Copyright 2013 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    *   https://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  
17  package io.netty.util.concurrent;
18  
19  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
20  
21  public class DefaultProgressivePromise<V> extends DefaultPromise<V> implements ProgressivePromise<V> {
22  
23      /**
24       * Creates a new instance.
25       *
26       * It is preferable to use {@link EventExecutor#newProgressivePromise()} to create a new progressive promise
27       *
28       * @param executor
29       *        the {@link EventExecutor} which is used to notify the promise when it progresses or it is complete
30       */
31      public DefaultProgressivePromise(EventExecutor executor) {
32          super(executor);
33      }
34  
35      protected DefaultProgressivePromise() { /* only for subclasses */ }
36  
37      @Override
38      public ProgressivePromise<V> setProgress(long progress, long total) {
39          if (total < 0) {
40              // total unknown
41              total = -1; // normalize
42              checkPositiveOrZero(progress, "progress");
43          } else if (progress < 0 || progress > total) {
44              throw new IllegalArgumentException(
45                      "progress: " + progress + " (expected: 0 <= progress <= total (" + total + "))");
46          }
47  
48          if (isDone()) {
49              throw new IllegalStateException("complete already");
50          }
51  
52          notifyProgressiveListeners(progress, total);
53          return this;
54      }
55  
56      @Override
57      public boolean tryProgress(long progress, long total) {
58          if (total < 0) {
59              total = -1;
60              if (progress < 0 || isDone()) {
61                  return false;
62              }
63          } else if (progress < 0 || progress > total || isDone()) {
64              return false;
65          }
66  
67          notifyProgressiveListeners(progress, total);
68          return true;
69      }
70  
71      @Override
72      public ProgressivePromise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener) {
73          super.addListener(listener);
74          return this;
75      }
76  
77      @Override
78      public ProgressivePromise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
79          super.addListeners(listeners);
80          return this;
81      }
82  
83      @Override
84      public ProgressivePromise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener) {
85          super.removeListener(listener);
86          return this;
87      }
88  
89      @Override
90      public ProgressivePromise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners) {
91          super.removeListeners(listeners);
92          return this;
93      }
94  
95      @Override
96      public ProgressivePromise<V> sync() throws InterruptedException {
97          super.sync();
98          return this;
99      }
100 
101     @Override
102     public ProgressivePromise<V> syncUninterruptibly() {
103         super.syncUninterruptibly();
104         return this;
105     }
106 
107     @Override
108     public ProgressivePromise<V> await() throws InterruptedException {
109         super.await();
110         return this;
111     }
112 
113     @Override
114     public ProgressivePromise<V> awaitUninterruptibly() {
115         super.awaitUninterruptibly();
116         return this;
117     }
118 
119     @Override
120     public ProgressivePromise<V> setSuccess(V result) {
121         super.setSuccess(result);
122         return this;
123     }
124 
125     @Override
126     public ProgressivePromise<V> setFailure(Throwable cause) {
127         super.setFailure(cause);
128         return this;
129     }
130 }