activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r380217 - in /incubator/activemq/trunk/activemq-core/src/gram: java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java script/GenerateCSharpClasses.groovy
Date Thu, 23 Feb 2006 20:28:50 GMT
Author: jstrachan
Date: Thu Feb 23 12:28:46 2006
New Revision: 380217

URL: http://svn.apache.org/viewcvs?rev=380217&view=rev
Log:
added the auto-generation of hashcode, equals and toString methods

Modified:
    incubator/activemq/trunk/activemq-core/src/gram/java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java
    incubator/activemq/trunk/activemq-core/src/gram/script/GenerateCSharpClasses.groovy

Modified: incubator/activemq/trunk/activemq-core/src/gram/java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/gram/java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java?rev=380217&r1=380216&r2=380217&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/gram/java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/gram/java/org/apache/activemq/openwire/tool/OpenWireCSharpClassesScript.java
Thu Feb 23 12:28:46 2006
@@ -17,8 +17,10 @@
 package org.apache.activemq.openwire.tool;
 
 import org.codehaus.jam.JClass;
+import org.codehaus.jam.JProperty;
 
-import java.io.File;
+import java.io.*;
+import java.util.Iterator;
 
 /**
  *
@@ -33,6 +35,53 @@
         }
         
         return super.run();
+    }
+    
+    public String makeHashCodeBody() throws Exception {
+        if (simpleName.endsWith("Id")) {
+            StringWriter buffer = new StringWriter();
+            PrintWriter out = new PrintWriter(buffer);
+            out.println("            int answer = 0;");
+            Iterator iter = getProperties().iterator();
+            while (iter.hasNext()) {
+                JProperty property = (JProperty) iter.next();
+                out.println("            answer = (answer * 37) + HashCode(" + property.getSimpleName()
+ ");");
+            }
+            out.println("            return answer;");
+            return buffer.toString();
+        }
+        return null;
+    }
+
+    public String makeEqualsBody() throws Exception {
+        if (simpleName.endsWith("Id")) {
+            StringWriter buffer = new StringWriter();
+            PrintWriter out = new PrintWriter(buffer);
+            
+            Iterator iter = getProperties().iterator();
+            while (iter.hasNext()) {
+                JProperty property = (JProperty) iter.next();
+                String name = property.getSimpleName();
+                out.println("            if (! Equals(this." + name + ", that." + name +
")) return false;");
+            }
+            out.println("            return true;");
+            return buffer.toString();
+        }
+        return null;
+    }
+    
+    public String makeToStringBody() throws Exception {
+            StringWriter buffer = new StringWriter();
+            PrintWriter out = new PrintWriter(buffer);
+            out.println("            return GetType().Name + \"[\"");
+            Iterator iter = getProperties().iterator();
+            while (iter.hasNext()) {
+                JProperty property = (JProperty) iter.next();
+                String name = property.getSimpleName();
+                out.println("                + \" " + name + "=\" + " + name);
+            }
+            out.println("                + \" ]\";");
+            return buffer.toString();
     }
 
 

Modified: incubator/activemq/trunk/activemq-core/src/gram/script/GenerateCSharpClasses.groovy
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/gram/script/GenerateCSharpClasses.groovy?rev=380217&r1=380216&r2=380217&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/gram/script/GenerateCSharpClasses.groovy (original)
+++ incubator/activemq/trunk/activemq-core/src/gram/script/GenerateCSharpClasses.groovy Thu
Feb 23 12:28:46 2006
@@ -69,12 +69,40 @@
 """
     }
 
+	def text = makeHashCodeBody()
+	if (text != null) out << 
+"""
+		public override int GetHashCode() {
+$text
+		}
+	
+"""	
+	   
+	text = makeEqualsBody()
+	if (text != null) out << 
+"""
+		public override bool Equals(object that) {
+	    	if (that is ${className}) {
+	    	    return Equals((${className}) that);
+			}
+			return false;
+    	}
+    
+		public virtual bool Equals(${className} that) {
+$text
+		}
+	
+"""	
+	    
+	text = makeToStringBody()
+	if (text != null) out << """
+		public override string ToString() {
+$text
+		}
+	
+"""	
+	    
     out << """
-
-        // TODO generate Equals method
-        // TODO generate GetHashCode method
-        // TODO generate ToString method
-
 
         public override byte GetDataStructureType() {
             return ID_${jclass.simpleName};



Mime
View raw message