]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java
Use java.util.Consumer instead of os.utils.datastructures.Callback
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplSocket.java
index 2ff47c29266912afa68ee0515fd5c47d1b809b89..9a86dc1fa2ae813b0b3a137b51a9063546af27af 100644 (file)
@@ -27,6 +27,7 @@ import java.util.TreeMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
 
 import org.eclipse.core.runtime.Platform;
 import org.simantics.databoard.Bindings;
@@ -155,7 +156,6 @@ import org.simantics.db.service.XSupport;
 import org.simantics.layer0.Layer0;
 import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
-import org.simantics.utils.datastructures.Callback;
 import org.simantics.utils.threads.logger.ITask;
 import org.simantics.utils.threads.logger.ThreadLogger;
 
@@ -324,31 +324,29 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         private Object result;
         final Semaphore sema = new Semaphore(0);
         private Throwable throwable = null;
-        final Callback<DatabaseException> callback = new Callback<DatabaseException>() {
-            @Override
-            public void run(DatabaseException e) {
-                synchronized (TaskHelper.this) {
-                    throwable = e;
-                }
+        final Consumer<DatabaseException> callback = e -> {
+            synchronized (TaskHelper.this) {
+                throwable = e;
             }
         };
         final Procedure<Object> proc = new Procedure<Object>() {
             @Override
             public void execute(Object result) {
-                callback.run(null);
+                callback.accept(null);
             }
             @Override
             public void exception(Throwable t) {
                 if (t instanceof DatabaseException)
-                    callback.run((DatabaseException)t);
+                    callback.accept((DatabaseException)t);
                 else
-                    callback.run(new DatabaseException("" + name + "operation failed.", t));
+                    callback.accept(new DatabaseException("" + name + "operation failed.", t));
             }
         };
         final WriteTraits writeTraits = new WriteTraits() {};
         TaskHelper(String name) {
             this.name = name;
         }
+        @SuppressWarnings("unchecked")
         <T> T getResult() {
             return (T)result;
         }
@@ -387,7 +385,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
             return null;
     }
 
-    public <T> void scheduleRequest(final Write request, final Callback<DatabaseException> callback, final Semaphore notify) {
+    public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify) {
         scheduleRequest(request, callback, notify, null);
     }
 
@@ -395,7 +393,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
      * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.Write, org.simantics.utils.datastructures.Callback, java.util.concurrent.Semaphore, java.lang.Boolean)
      */
     @Override
-    public <T> void scheduleRequest(final Write request, final Callback<DatabaseException> callback, final Semaphore notify, Boolean combine) {
+    public <T> void scheduleRequest(final Write request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
 
         assert (request != null);
 
@@ -440,12 +438,12 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                         @Override
                         public void execute(Object result) {
-                            if(callback != null) callback.run(null);
+                            if(callback != null) callback.accept(null);
                         }
 
                         @Override
                         public void exception(Throwable t) {
-                            if(callback != null) callback.run((DatabaseException)t);
+                            if(callback != null) callback.accept((DatabaseException)t);
                         }
 
                     });
@@ -616,7 +614,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
     }
 
-    public <T> void scheduleRequest(final DelayedWrite request, final Callback<DatabaseException> callback, final Semaphore notify) {
+    public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify) {
         scheduleRequest(request, callback, notify, null);
     }
 
@@ -624,7 +622,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
      * @see fi.vtt.simantics.procore.internal.WriteRequestScheduler#scheduleRequest(org.simantics.db.request.DelayedWrite, org.simantics.utils.datastructures.Callback, java.util.concurrent.Semaphore, java.lang.Boolean)
      */
     @Override
-    public <T> void scheduleRequest(final DelayedWrite request, final Callback<DatabaseException> callback, final Semaphore notify, Boolean combine) {
+    public <T> void scheduleRequest(final DelayedWrite request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
 
         final ITask total = ThreadLogger.getInstance().begin("ScheduleDelayedWrite");
 
@@ -642,13 +640,13 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     @Override
                     public void execute(Object result) {
                         if (callback != null)
-                            callback.run(null);
+                            callback.accept(null);
                     }
                     @Override
                     public void exception(Throwable t) {
                         if (callback != null) {
-                            if (t instanceof DatabaseException) callback.run((DatabaseException) t);
-                            else callback.run(new DatabaseException(t));
+                            if (t instanceof DatabaseException) callback.accept((DatabaseException) t);
+                            else callback.accept(new DatabaseException(t));
                         } else
                             Logger.defaultLogError("Unhandled exception", t);
                     }
@@ -1293,7 +1291,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
         try {
 
-            int thread = request.hashCode() & queryProvider2.THREAD_MASK;
+            //int thread = request.hashCode() & queryProvider2.THREAD_MASK;
 
             fireSessionVariableChange(SessionVariables.QUEUED_WRITES);
 
@@ -1358,12 +1356,12 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
     }
 
-    public <T> void scheduleRequest(final WriteOnly request, final Callback<DatabaseException> callback, final Semaphore notify) {
+    public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify) {
         scheduleRequest(request, callback, notify, null);
     }
 
     @Override
-    public <T> void scheduleRequest(final WriteOnly request, final Callback<DatabaseException> callback, final Semaphore notify, Boolean combine) {
+    public <T> void scheduleRequest(final WriteOnly request, final Consumer<DatabaseException> callback, final Semaphore notify, Boolean combine) {
 
         assertAlive();
 
@@ -1396,12 +1394,12 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                             @Override
                             public void execute(Object result) {
-                                if(callback != null) callback.run(null);
+                                if(callback != null) callback.accept(null);
                             }
 
                             @Override
                             public void exception(Throwable t) {
-                                if(callback != null) callback.run((DatabaseException)t);
+                                if(callback != null) callback.accept((DatabaseException)t);
                             }
 
                         });
@@ -1424,7 +1422,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
 
                             if(!(e instanceof CancelTransactionException)) {
                                 if (callback != null)
-                                    callback.run(new DatabaseException(e));
+                                    callback.accept(new DatabaseException(e));
                             }
 
                             writeState.except(e);
@@ -1963,14 +1961,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         assertAlive();
         Semaphore notify = new Semaphore(0);
         final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
-        scheduleRequest(request, new Callback<DatabaseException>() {
-
-            @Override
-            public void run(DatabaseException e) {
-              exception.set(e);
-            }
-
-        }, notify);
+        scheduleRequest(request, e -> exception.set(e), notify);
         acquire(notify, request);
         if(exception.get() != null) throw exception.get();
     }
@@ -2064,12 +2055,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         assertNotSession();
         Semaphore notify = new Semaphore(0);
         final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
-        scheduleRequest(request, new Callback<DatabaseException>() {
-            @Override
-            public void run(DatabaseException e) {
-                exception.set(e);
-            }
-        }, notify);
+        scheduleRequest(request, e -> exception.set(e), notify);
         acquire(notify, request);
         if(exception.get() != null) throw exception.get();
     }
@@ -2080,12 +2066,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         assertAlive();
         Semaphore notify = new Semaphore(0);
         final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
-        scheduleRequest(request, new Callback<DatabaseException>() {
-            @Override
-            public void run(DatabaseException e) {
-                exception.set(e);
-            }
-        }, notify);
+        scheduleRequest(request, e -> exception.set(e), notify);
         acquire(notify, request);
         if(exception.get() != null) throw exception.get();
     }
@@ -2117,7 +2098,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
     }
 
     @Override
-    public void asyncRequest(final Write request, final Callback<DatabaseException> callback) {
+    public void asyncRequest(final Write request, final Consumer<DatabaseException> callback) {
 
         scheduleRequest(request, callback, null);
 
@@ -2145,7 +2126,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
     }
 
     @Override
-    public void asyncRequest(final DelayedWrite request, final Callback<DatabaseException> callback) {
+    public void asyncRequest(final DelayedWrite request, final Consumer<DatabaseException> callback) {
 
         scheduleRequest(request, callback, null);
 
@@ -2162,7 +2143,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
     }
 
     @Override
-    public void asyncRequest(final WriteOnly request, final Callback<DatabaseException> callback) {
+    public void asyncRequest(final WriteOnly request, final Consumer<DatabaseException> callback) {
 
         scheduleRequest(request, callback, null);
 
@@ -2286,16 +2267,16 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
         return true;
     }
 
-    static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, final Callback<ReadGraphImpl> runnable) {
+    static void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, final Consumer<ReadGraphImpl> runnable) {
 
-        Callback<ReadGraphImpl> composite = new Callback<ReadGraphImpl>() {
+        Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
 
             AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void accept(ReadGraphImpl graph) {
                 if(ready.decrementAndGet() == 0) {
-                    runnable.run(graph);
+                    runnable.accept(graph);
                 }
             }
 
@@ -2309,24 +2290,24 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     e.printStackTrace();
                 }
             } else {
-                composite.run(graph);
+                composite.accept(graph);
             }
         }
 
-        composite.run(graph);
+        composite.accept(graph);
 
     }
 
-    static  void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, int predicate, final Callback<ReadGraphImpl> runnable) {
+    static  void loadVirtualStatements(final VirtualGraphServerSupportImpl support, ReadGraphImpl graph, int subject, int predicate, final Consumer<ReadGraphImpl> runnable) {
 
-        Callback<ReadGraphImpl> composite = new Callback<ReadGraphImpl>() {
+        Consumer<ReadGraphImpl> composite = new Consumer<ReadGraphImpl>() {
 
             AtomicInteger ready = new AtomicInteger(support.providers.size() + 1);
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void accept(ReadGraphImpl graph) {
                 if(ready.decrementAndGet() == 0) {
-                    runnable.run(graph);
+                    runnable.accept(graph);
                 }
             }
 
@@ -2340,11 +2321,11 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     e.printStackTrace();
                 }
             } else {
-                composite.run(graph);
+                composite.accept(graph);
             }
         }
 
-        composite.run(graph);
+        composite.accept(graph);
 
     }