Mutex.{Action,ExceptionAction} should use a generic type parameter for the merge_trunk_release55 prj_configs_49636_base
authorjglick@netbeans.org
Wed, 14 Jun 2006 01:12:43 +0000
changeset 176658fae05652a
parent 175 7dd7239e3249
child 177 b525ab56d9d4
Mutex.{Action,ExceptionAction} should use a generic type parameter for the
return type. (Tip: use Void if you plan to just return null.) Would ideally have
a type parameter for the exception type as well, but too late for that: it is
wrapped in a MutexException, which cannot be generified since it is a throwable.
If the original type signature were to throw Exception then it would have been
possible to parametrize that as well, leading to clearer client code.
openide.util/src/org/openide/util/Mutex.java
     1.1 --- a/openide.util/src/org/openide/util/Mutex.java	Wed Jun 14 01:06:17 2006 +0000
     1.2 +++ b/openide.util/src/org/openide/util/Mutex.java	Wed Jun 14 01:12:43 2006 +0000
     1.3 @@ -206,7 +206,7 @@
     1.4      * @param action the action to perform
     1.5      * @return the object returned from {@link Mutex.Action#run}
     1.6      */
     1.7 -    public Object readAccess(Action action) {
     1.8 +    public <T> T readAccess(Action<T> action) {
     1.9          if (this == EVENT) {
    1.10              try {
    1.11                  return doEventAccess(action);
    1.12 @@ -249,7 +249,7 @@
    1.13      * @exception RuntimeException if any runtime exception is thrown from the run method
    1.14      * @see #readAccess(Mutex.Action)
    1.15      */
    1.16 -    public Object readAccess(ExceptionAction action) throws MutexException {
    1.17 +    public <T> T readAccess(ExceptionAction<T> action) throws MutexException {
    1.18          if (this == EVENT) {
    1.19              return doEventAccess(action);
    1.20          }
    1.21 @@ -297,7 +297,7 @@
    1.22      * @param action the action to perform
    1.23      * @return the result of {@link Mutex.Action#run}
    1.24      */
    1.25 -    public Object writeAccess(Action action) {
    1.26 +    public <T> T writeAccess(Action<T> action) {
    1.27          if (this == EVENT) {
    1.28              try {
    1.29                  return doEventAccess(action);
    1.30 @@ -337,7 +337,7 @@
    1.31      * @see #writeAccess(Mutex.Action)
    1.32      * @see #readAccess(Mutex.ExceptionAction)
    1.33      */
    1.34 -    public Object writeAccess(ExceptionAction action) throws MutexException {
    1.35 +    public <T> T writeAccess(ExceptionAction<T> action) throws MutexException {
    1.36          if (this == EVENT) {
    1.37              return doEventAccess(action);
    1.38          }
    1.39 @@ -1211,9 +1211,9 @@
    1.40      }
    1.41  
    1.42      /** Methods for access to event queue and waiting for result.
    1.43 -    * @param run runabble to post later
    1.44 +    * @param run runnable to post later
    1.45      */
    1.46 -    private static Object doEventAccess(final ExceptionAction run)
    1.47 +    private static <T> T doEventAccess(final ExceptionAction<T> run)
    1.48      throws MutexException {
    1.49          if (isDispatchThread()) {
    1.50              try {
    1.51 @@ -1228,12 +1228,12 @@
    1.52          final Throwable[] arr = new Throwable[1];
    1.53  
    1.54          try {
    1.55 -            final Object[] res = new Object[1];
    1.56 +            final List<T> res = new ArrayList<T>(1);
    1.57              EventQueue.invokeAndWait(
    1.58                  new Runnable() {
    1.59                      public void run() {
    1.60                          try {
    1.61 -                            res[0] = run.run();
    1.62 +                            res.add(run.run());
    1.63                          } catch (Exception e) {
    1.64                              arr[0] = e;
    1.65                          } catch (LinkageError e) {
    1.66 @@ -1248,7 +1248,7 @@
    1.67              );
    1.68  
    1.69              if (arr[0] == null) {
    1.70 -                return res[0];
    1.71 +                return res.get(0);
    1.72              }
    1.73          } catch (InterruptedException e) {
    1.74              arr[0] = e;
    1.75 @@ -1323,11 +1323,12 @@
    1.76      /** Action to be executed in a mutex without throwing any checked exceptions.
    1.77      * Unchecked exceptions will be propagated to calling code.
    1.78      */
    1.79 -    public static interface Action extends ExceptionAction {
    1.80 +    public interface Action<T> extends ExceptionAction<T> {
    1.81          /** Execute the action.
    1.82          * @return any object, then returned from {@link Mutex#readAccess(Mutex.Action)} or {@link Mutex#writeAccess(Mutex.Action)}
    1.83 +         * @param T the type of object to return
    1.84          */
    1.85 -        public Object run();
    1.86 +        T run();
    1.87      }
    1.88  
    1.89      /** Action to be executed in a mutex, possibly throwing checked exceptions.
    1.90 @@ -1335,14 +1336,15 @@
    1.91      * code should catch the encapsulating exception and rethrow the
    1.92      * real one.
    1.93      * Unchecked exceptions will be propagated to calling code without encapsulation.
    1.94 +     * @param T the type of object to return
    1.95      */
    1.96 -    public static interface ExceptionAction {
    1.97 +    public interface ExceptionAction<T> {
    1.98          /** Execute the action.
    1.99          * Can throw an exception.
   1.100          * @return any object, then returned from {@link Mutex#readAccess(Mutex.ExceptionAction)} or {@link Mutex#writeAccess(Mutex.ExceptionAction)}
   1.101          * @exception Exception any exception the body needs to throw
   1.102          */
   1.103 -        public Object run() throws Exception;
   1.104 +        T run() throws Exception;
   1.105      }
   1.106  
   1.107      private static final class ThreadInfo {