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.websocket.jsr356.encoders;
20  
21  import java.util.concurrent.ExecutionException;
22  import java.util.concurrent.Future;
23  import java.util.concurrent.TimeUnit;
24  import java.util.concurrent.TimeoutException;
25  
26  import javax.websocket.Encoder;
27  
28  /**
29   * A <code>Future&lt;Void&gt;</code> that is already failed as a result of an Encode error
30   */
31  public class EncodeFailedFuture implements Future<Void>
32  {
33      private final String msg;
34      private final Throwable cause;
35  
36      public EncodeFailedFuture(Object data, Encoder encoder, Class<?> encoderType, Throwable cause)
37      {
38          this.msg = String.format("Unable to encode %s using %s as %s",data.getClass().getName(),encoder.getClass().getName(),encoderType.getName());
39          this.cause = cause;
40      }
41  
42      @Override
43      public boolean cancel(boolean mayInterruptIfRunning)
44      {
45          return false;
46      }
47  
48      @Override
49      public Void get() throws InterruptedException, ExecutionException
50      {
51          throw new ExecutionException(msg,cause);
52      }
53  
54      @Override
55      public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
56      {
57          throw new ExecutionException(msg,cause);
58      }
59  
60      @Override
61      public boolean isCancelled()
62      {
63          return false;
64      }
65  
66      @Override
67      public boolean isDone()
68      {
69          return true;
70      }
71  }