1 /*
   2  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.util.concurrent.Future;
  29 import java.util.concurrent.TimeUnit;
  30 import java.util.concurrent.ExecutionException;
  31 import java.io.IOException;
  32 
  33 /**
  34  * A Future representing the result of an I/O operation that has already
  35  * completed.
  36  */
  37 
  38 final class CompletedFuture<V> implements Future<V> {
  39     private final V result;
  40     private final Throwable exc;
  41 
  42     private CompletedFuture(V result, Throwable exc) {
  43         this.result = result;
  44         this.exc = exc;
  45     }
  46 
  47     static <V> CompletedFuture<V> withResult(V result) {
  48         return new CompletedFuture<V>(result, null);
  49     }
  50 
  51     static <V> CompletedFuture<V> withFailure(Throwable exc) {
  52         // exception must be IOException or SecurityException
  53         if (!(exc instanceof IOException) && !(exc instanceof SecurityException))
  54             exc = new IOException(exc);
  55         return new CompletedFuture<V>(null, exc);
  56     }
  57 
  58     static <V> CompletedFuture<V> withResult(V result, Throwable exc) {
  59         if (exc == null) {
  60             return withResult(result);
  61         } else {
  62             return withFailure(exc);
  63         }
  64     }
  65 
  66     @Override
  67     public V get() throws ExecutionException {
  68         if (exc != null)
  69             throw new ExecutionException(exc);
  70         return result;
  71     }
  72 
  73     @Override
  74     public V get(long timeout, TimeUnit unit) throws ExecutionException {
  75         if (unit == null)
  76             throw new NullPointerException();
  77         if (exc != null)
  78             throw new ExecutionException(exc);
  79         return result;
  80     }
  81 
  82     @Override
  83     public boolean isCancelled() {
  84         return false;
  85     }
  86 
  87     @Override
  88     public boolean isDone() {
  89         return true;
  90     }
  91 
  92     @Override
  93     public boolean cancel(boolean mayInterruptIfRunning) {
  94         return false;
  95     }
  96 }