View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.util.Objects;
22  
23  import org.eclipse.jetty.util.log.Log;
24  
25  /**
26   * <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p>
27   *
28   * @param <C> the type of the context object
29   */
30  public interface Promise<C>
31  {
32      /**
33       * <p>Callback invoked when the operation completes.</p>
34       *
35       * @param result the context
36       * @see #failed(Throwable)
37       */
38      public abstract void succeeded(C result);
39  
40      /**
41       * <p>Callback invoked when the operation fails.</p>
42       *
43       * @param x the reason for the operation failure
44       */
45      public void failed(Throwable x);
46  
47      /**
48       * <p>Empty implementation of {@link Promise}</p>
49       *
50       * @param <C> the type of the context object
51       */
52      public static class Adapter<C> implements Promise<C>
53      {
54          @Override
55          public void succeeded(C result)
56          {
57          }
58  
59          @Override
60          public void failed(Throwable x)
61          {
62              Log.getLogger(this.getClass()).warn(x);
63          }
64      }
65  
66      public static class Wrapper<W> implements Promise<W>
67      {
68          private final Promise<W> promise;
69  
70          public Wrapper(Promise<W> promise)
71          {
72              this.promise = Objects.requireNonNull(promise);
73          }
74  
75          @Override
76          public void succeeded(W result)
77          {
78              promise.succeeded(result);
79          }
80  
81          @Override
82          public void failed(Throwable x)
83          {
84              promise.failed(x);
85          }
86  
87          public Promise<W> getPromise()
88          {
89              return promise;
90          }
91  
92          public Promise<W> unwrap()
93          {
94              Promise<W> result = promise;
95              while (true)
96              {
97                  if (result instanceof Wrapper)
98                      result = ((Wrapper<W>)result).unwrap();
99                  else
100                     break;
101             }
102             return result;
103         }
104     }
105 }