View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  /*
20   * Copyright (c) 2012 the original author or authors.
21   *
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   *
26   *     http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   */
34  
35  package org.eclipse.jetty.util;
36  
37  /**
38   * <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p>
39   *
40   * <p>Semantically this is equivalent to an optimise Promise&lt;Void&gt;, but callback is a more meaningful 
41   * name than EmptyPromise</p>
42   */
43  public interface Callback
44  {
45      /**
46       * Instance of Adapter that can be used when the callback methods need an empty
47       * implementation without incurring in the cost of allocating a new Adapter object.
48       */
49      static Callback NOOP = new Callback(){};
50  
51  
52      /**
53       * <p>Callback invoked when the operation completes.</p>
54       *
55       * @see #failed(Throwable)
56       */
57      default void succeeded()
58      {}
59  
60      /**
61       * <p>Callback invoked when the operation fails.</p>
62       * @param x the reason for the operation failure
63       */
64      default void failed(Throwable x)
65      {}
66  
67      /**
68       * @return True if the callback is known to never block the caller
69       */
70      default boolean isNonBlocking()
71      {
72          return false;
73      }
74      
75      
76      /**
77       * Callback interface that declares itself as non-blocking
78       */
79      interface NonBlocking extends Callback
80      {
81          @Override
82          public default boolean isNonBlocking()
83          {
84              return true;
85          }
86      }
87      
88      
89      /**
90       * <p>Empty implementation of {@link Callback}</p>
91       */
92      @Deprecated
93      static class Adapter implements Callback
94      {}
95  }