You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2018/03/09 01:55:45 UTC

mnemonic git commit: MNEMONIC-468: FactoryProxyHelper supports shift operation

Repository: mnemonic
Updated Branches:
  refs/heads/master bed9ba4c9 -> ba3da6fa1


MNEMONIC-468: FactoryProxyHelper supports shift operation


Project: http://git-wip-us.apache.org/repos/asf/mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/mnemonic/commit/ba3da6fa
Tree: http://git-wip-us.apache.org/repos/asf/mnemonic/tree/ba3da6fa
Diff: http://git-wip-us.apache.org/repos/asf/mnemonic/diff/ba3da6fa

Branch: refs/heads/master
Commit: ba3da6fa12dc8149361bcca383ba539c08219a6a
Parents: bed9ba4
Author: Wang, Gang(Gary) <ga...@apache.org>
Authored: Thu Mar 8 17:53:56 2018 -0800
Committer: Wang, Gang(Gary) <ga...@apache.org>
Committed: Thu Mar 8 17:53:56 2018 -0800

----------------------------------------------------------------------
 .../mnemonic/EntityFactoryProxyHelper.java      | 41 +++++++++++++++++---
 1 file changed, 36 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mnemonic/blob/ba3da6fa/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
----------------------------------------------------------------------
diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java b/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
index 04d777d..0f67bb6 100644
--- a/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
+++ b/mnemonic-core/src/main/java/org/apache/mnemonic/EntityFactoryProxyHelper.java
@@ -17,6 +17,8 @@
 
 package org.apache.mnemonic;
 
+import org.apache.commons.lang3.tuple.Pair;
+
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
@@ -24,8 +26,19 @@ public class EntityFactoryProxyHelper<D extends Durable>
     implements EntityFactoryProxy {
 
   protected Method fcreatemtd, frestoremtd;
+  protected int shiftnum;
+
+  public EntityFactoryProxyHelper(Class<D> clazz)
+      throws ClassNotFoundException, NoSuchMethodException {
+    this(clazz, 0);
+  }
 
-  public EntityFactoryProxyHelper(Class<D> clazz) throws ClassNotFoundException, NoSuchMethodException {
+  public EntityFactoryProxyHelper(Class<D> clazz, int shiftnum)
+      throws ClassNotFoundException, NoSuchMethodException {
+    if (shiftnum < 0) {
+      throw new OutOfBoundsException("Shift number cannot be negative");
+    }
+    this.shiftnum = shiftnum;
     Class c = Class.forName(clazz.getName() + "Factory");
     Method[] fmtds = c.getDeclaredMethods();
     for (int i = 0; i < fmtds.length; ++i) {
@@ -52,9 +65,13 @@ public class EntityFactoryProxyHelper<D extends Durable>
   public <A extends RestorableAllocator<A>> D create(
       A allocator, EntityFactoryProxy[] factoryproxys,
       DurableType[] gfields, boolean autoreclaim) {
+    if (shiftnum > gfields.length - 1) {
+      throw new OutOfBoundsException("Shift number is out of the range of generic fields");
+    }
+    Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(gfields, factoryproxys, shiftnum);
     Object o = null;
     try {
-      o = fcreatemtd.invoke(null, allocator, factoryproxys, gfields, autoreclaim);
+      o = fcreatemtd.invoke(null, allocator, dpt.getRight(), dpt.getLeft(), autoreclaim);
     } catch (IllegalAccessException e) {
       e.printStackTrace();
     } catch (InvocationTargetException e) {
@@ -66,10 +83,15 @@ public class EntityFactoryProxyHelper<D extends Durable>
   @Override
   public <A extends RestorableAllocator<A>> D create(
       ParameterHolder<A> ph) {
+    if (shiftnum > ph.getGenericTypes().length - 1) {
+      throw new OutOfBoundsException("Shift number is out of the range of generic fields");
+    }
+    Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(ph.getGenericTypes(),
+        ph.getEntityFactoryProxies(), shiftnum);
     Object o = null;
     try {
       o = fcreatemtd.invoke(null, ph.getAllocator(),
-          ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getAutoReclaim());
+          dpt.getRight(), dpt.getLeft(), ph.getAutoReclaim());
     } catch (IllegalAccessException e) {
       e.printStackTrace();
     } catch (InvocationTargetException e) {
@@ -82,9 +104,13 @@ public class EntityFactoryProxyHelper<D extends Durable>
   public <A extends RestorableAllocator<A>> D restore(
       A allocator, EntityFactoryProxy[] factoryproxys,
       DurableType[] gfields, long phandler, boolean autoreclaim) {
+    if (shiftnum > gfields.length - 1) {
+      throw new OutOfBoundsException("Shift number is out of the range of generic fields");
+    }
+    Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(gfields, factoryproxys, shiftnum);
     Object o = null;
     try {
-      o = frestoremtd.invoke(null, allocator, factoryproxys, gfields, phandler, autoreclaim);
+      o = frestoremtd.invoke(null, allocator, dpt.getRight(), dpt.getLeft(), phandler, autoreclaim);
     } catch (IllegalAccessException e) {
       e.printStackTrace();
     } catch (InvocationTargetException e) {
@@ -96,10 +122,15 @@ public class EntityFactoryProxyHelper<D extends Durable>
   @Override
   public <A extends RestorableAllocator<A>> D restore(
       ParameterHolder<A> ph) {
+    if (shiftnum > ph.getGenericTypes().length - 1) {
+      throw new OutOfBoundsException("Shift number is out of the range of generic fields");
+    }
+    Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(ph.getGenericTypes(),
+        ph.getEntityFactoryProxies(), shiftnum);
     Object o = null;
     try {
       o = frestoremtd.invoke(null, ph.getAllocator(),
-          ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getHandler(), ph.getAutoReclaim());
+          ph.getEntityFactoryProxies(), dpt.getRight(), dpt.getLeft(), ph.getAutoReclaim());
     } catch (IllegalAccessException e) {
       e.printStackTrace();
     } catch (InvocationTargetException e) {