View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.parser.future;
16  
17  import java.util.concurrent.Future;
18  import java.util.concurrent.TimeUnit;
19  
20  /**
21   * This class is a simple wrapper on a CharSequence.
22   * <p>
23   * This Future object already has it result and does not use any background Thread and simply return the
24   * {@link CharSequence} provided at the construction time.
25   * 
26   * @author Nicolas Richeton
27   * 
28   */
29  public class CharSequenceFuture implements Future<CharSequence> {
30      private final CharSequence seq;
31  
32      /**
33       * Create a new Future object which will return the {@link CharSequence} provided as parameter.
34       * 
35       * @param charSequence
36       *            the sequence which will be returned on {@link #get()}
37       */
38      public CharSequenceFuture(CharSequence charSequence) {
39          this.seq = charSequence;
40      }
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see java.util.concurrent.Future#isDone()
46       */
47      @Override
48      public boolean isDone() {
49          // This future is already done per implementation.
50          return true;
51      }
52  
53      /*
54       * (non-Javadoc)
55       * 
56       * @see java.util.concurrent.Future#isCancelled()
57       */
58      @Override
59      public boolean isCancelled() {
60          // Cannot ever been cancelled
61          return false;
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)
68       */
69      @Override
70      public CharSequence get(long timeout, TimeUnit unit) {
71          // Get will never block, just use the default implementation.
72          return get();
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see java.util.concurrent.Future#get()
79       */
80      @Override
81      public CharSequence get() {
82          // Return the wrapped CharSequence.
83          return this.seq;
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see java.util.concurrent.Future#cancel(boolean)
90       */
91      @Override
92      public boolean cancel(boolean mayInterruptIfRunning) {
93          // Cannot be cancelled.
94          return false;
95      }
96  }