ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [2/4] incubator-ignite git commit: # IGNITE-32 WIP: Added constructors generation.
Date Sun, 28 Dec 2014 04:25:49 GMT
# IGNITE-32 WIP: Added constructors generation.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/38c721eb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/38c721eb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/38c721eb

Branch: refs/heads/ignite-32
Commit: 38c721ebc63f4b2be189ed41cad66e8357f6177c
Parents: e8acedf
Author: AKuznetsov <akuznetsov@gridgain.com>
Authored: Sun Dec 28 11:18:08 2014 +0700
Committer: AKuznetsov <akuznetsov@gridgain.com>
Committed: Sun Dec 28 11:18:08 2014 +0700

----------------------------------------------------------------------
 .../ignite/schema/pojo/PojoCodeGenerator.java   | 76 +++++++++++++-------
 1 file changed, 50 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/38c721eb/modules/schema-load/src/main/java/org/apache/ignite/schema/pojo/PojoCodeGenerator.java
----------------------------------------------------------------------
diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/pojo/PojoCodeGenerator.java
b/modules/schema-load/src/main/java/org/apache/ignite/schema/pojo/PojoCodeGenerator.java
index aacdab9..a46b8cd 100644
--- a/modules/schema-load/src/main/java/org/apache/ignite/schema/pojo/PojoCodeGenerator.java
+++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/pojo/PojoCodeGenerator.java
@@ -73,10 +73,11 @@ public class PojoCodeGenerator {
      * @param pkg Package name.
      * @param type Type name.
      * @param descs Type descriptors.
+     * @param constructor If {@code true} then generate empty and full constructors.
      * @throws IOException If failed to write generated code into file.
      */
     private static void generateCode(String pkg, String type, Collection<GridCacheQueryTypeDescriptor>
descs,
-        File pkgFolder) throws IOException {
+        File pkgFolder, boolean constructor) throws IOException {
         if (descs.isEmpty())
             return; // TODO IGNITE-32 UI should pass non metadata.
 
@@ -87,19 +88,49 @@ public class PojoCodeGenerator {
         add0(src, "/** Code generated by Apache Ignite Schema Load utility. */");
         add0(src, "public class " + type + " {");
 
+        // Fields.
         for (GridCacheQueryTypeDescriptor desc : descs) {
             add1(src, "private " + desc.getJavaType().getSimpleName() + " " + desc.getJavaName()
+ ";");
             add0(src, "");
         }
 
+        // Constructors.
+        if (constructor) {
+
+            add1(src, "/** Empty constructor. */");
+            add1(src, "public " + type + "() {");
+            add2(src, "// No-op.");
+            add1(src, "}");
+
+            add0(src, "");
+
+            add1(src, "/** Full constructor. */");
+            add1(src, "public " + type + "(");
+
+            Iterator<GridCacheQueryTypeDescriptor> it = descs.iterator();
+
+            while(it.hasNext()) {
+                GridCacheQueryTypeDescriptor desc = it.next();
+
+                add2(src, desc.getJavaType().getSimpleName() + " " + desc.getJavaName() +
(it.hasNext() ? "," : ""));
+            }
+            add1(src, ") {");
+
+            for (GridCacheQueryTypeDescriptor desc : descs)
+                add2(src, String.format("this.%1$s = %1$s;", desc.getJavaName()));
+
+            add1(src, "}");
+
+            add0(src, "");
+        }
+
+        // Methods.
         for (GridCacheQueryTypeDescriptor desc : descs) {
             String fldName = desc.getJavaName();
             String fldType = desc.getJavaType().getSimpleName();
 
             String mtdName = capitalizeFirst(fldName);
 
-            add0(src, "");
-
             add1(src, "public " + fldType + " get" + mtdName + "() {");
             add2(src, "return " + fldName + ";");
             add1(src, "}");
@@ -109,9 +140,9 @@ public class PojoCodeGenerator {
             add1(src, "public void set" + mtdName + "(" + fldType + " " + fldName + ") {");
             add2(src, "this." + fldName + " = " + fldName + ";");
             add1(src, "}");
-        }
 
-        add0(src, "");
+            add0(src, "");
+        }
 
         add1(src, "/** {@inheritDoc} */");
         add1(src, "@Override public boolean equals(Object o) {");
@@ -128,10 +159,8 @@ public class PojoCodeGenerator {
         add2(src, String.format("%1$s that = (%1$s)o;", type));
 
         for (GridCacheQueryTypeDescriptor desc : descs) {
-            String fldName = desc.getJavaName();
-
             add0(src, "");
-            add2(src, String.format("if (%1$s != null ? !%1$s.equals(that.%1$s) : that.%1$s
!= null)", fldName));
+            add2(src, String.format("if (%1$s != null ? !%1$s.equals(that.%1$s) : that.%1$s
!= null)", desc.getJavaName()));
             add3(src, "return false;");
         }
 
@@ -146,16 +175,12 @@ public class PojoCodeGenerator {
 
         Iterator<GridCacheQueryTypeDescriptor> it = descs.iterator();
 
-        GridCacheQueryTypeDescriptor firstFld = it.next();
-
-        add2(src, String.format("int res = %1$s != null ? %1$s.hashCode() : 0;", firstFld.getJavaName()));
+        add2(src, String.format("int res = %1$s != null ? %1$s.hashCode() : 0;", it.next().getJavaName()));
 
-        while(it.hasNext()) {
-            String fldName = it.next().getJavaName();
+        add0(src, "");
 
-            add0(src, "");
-            add2(src, String.format("res = 31 * res + (%1$s != null ? %1$s.hashCode() : 0);",
fldName));
-        }
+        while(it.hasNext())
+            add2(src, String.format("res = 31 * res + (%1$s != null ? %1$s.hashCode() : 0);",
it.next().getJavaName()));
 
         add0(src, "");
         add2(src, "return res;");
@@ -165,15 +190,12 @@ public class PojoCodeGenerator {
 
         add1(src, "/** {@inheritDoc} */");
         add1(src, "@Override public String toString() {");
-        add2(src, "return \"" + type + " [\"");
+        add2(src, "return \"" + type + " [\" + ");
 
-        for (GridCacheQueryTypeDescriptor desc : descs) {
-            String fldName = desc.getJavaName();
-
-            add3(src, String.format("+ \" %1$s=\" + %1$s", fldName));
-        }
+        for (GridCacheQueryTypeDescriptor desc : descs)
+            add3(src, String.format("\" %1$s=\" + %1$s + ", desc.getJavaName()));
 
-        add2(src, " + \"]\";");
+        add2(src, "\"]\";");
         add1(src, "}");
 
         add0(src, "}");
@@ -190,16 +212,18 @@ public class PojoCodeGenerator {
      * @param meta Type metadata.
      * @param outFolder Output folder.
      * @param pkg Types package.
+     * @param constructor If {@code true} then generate empty and full constructors.
      * @throws IOException If failed to write generated code into file.
      */
-    public static void generate(GridCacheQueryTypeMetadata meta, String outFolder, String
pkg) throws IOException {
+    public static void generate(GridCacheQueryTypeMetadata meta, String outFolder, String
pkg, boolean constructor)
+        throws IOException {
         File pkgFolder = new File(outFolder, pkg.replace('.', File.separatorChar));
 
         if (!pkgFolder.exists() && !pkgFolder.mkdirs())
             throw new IOException("Failed to create folders for package: " + pkg);
 
-        generateCode(pkg, meta.getKeyType(), meta.getKeyDescriptors(), pkgFolder);
+        generateCode(pkg, meta.getKeyType(), meta.getKeyDescriptors(), pkgFolder, constructor);
 
-        generateCode(pkg, meta.getType(), meta.getValueDescriptors(), pkgFolder);
+        generateCode(pkg, meta.getType(), meta.getValueDescriptors(), pkgFolder, constructor);
     }
 }


Mime
View raw message