From: Wouter Z. <dra...@us...> - 2008-02-04 01:31:36
|
User: draftdog Date: 08/02/03 17:31:38 Modified: andromda-java/conf/test Tag: V3_x_HEAD andromda.xml andromda-java/src/META-INF/andromda Tag: V3_x_HEAD cartridge.xml namespace.xml andromda-java/src/templates/java Tag: V3_x_HEAD ValueObject.vsl andromda-java/src/test/expected Tag: V3_x_HEAD cartridge-output.zip Log: added support for the generation of an equalProperties(..) method in value objects, this feature is disabled by default though Revision Changes Path No revision No revision 1.6.2.6 +2 -1 cartridges/andromda-java/conf/test/andromda.xml Index: andromda.xml =================================================================== RCS file: /cvsroot/andromda/cartridges/andromda-java/conf/test/andromda.xml,v retrieving revision 1.6.2.5 retrieving revision 1.6.2.6 diff -u -w -r1.6.2.5 -r1.6.2.6 --- andromda.xml 11 Jan 2008 17:14:59 -0000 1.6.2.5 +++ andromda.xml 4 Feb 2008 01:31:37 -0000 1.6.2.6 @@ -31,6 +31,7 @@ <property name="interface-impls">${test.output.dir}</property> <property name="mergeMappingsUri">file:${test.src.dir}/mappings/MergeMappings.xml</property> <property name="useArraysForMultiplicitiesOfTypeMany">true</property> + <property name="generateEqualPropertiesMethod">true</property> </properties> </namespace> </namespaces> No revision No revision 1.4.6.8 +11 -9 cartridges/andromda-java/src/META-INF/andromda/cartridge.xml Index: cartridge.xml =================================================================== RCS file: /cvsroot/andromda/cartridges/andromda-java/src/META-INF/andromda/cartridge.xml,v retrieving revision 1.4.6.7 retrieving revision 1.4.6.8 diff -u -w -r1.4.6.7 -r1.4.6.8 --- cartridge.xml 27 Dec 2006 06:15:46 -0000 1.4.6.7 +++ cartridge.xml 4 Feb 2008 01:31:38 -0000 1.4.6.8 @@ -8,10 +8,12 @@ <property reference="enablePropertyConstructors"/> <property reference="toDoTag"/> <property reference="enableTemplating"/> + <property reference="generateEqualPropertiesMethod"/> <!-- cartridge-property merge-point--> <condition name="enableTemplating">$enableTemplating.equalsIgnoreCase("true")</condition> <condition name="serializable">$serializable.equalsIgnoreCase("true")</condition> + <condition name="generateEqualPropertiesMethod">$generateEqualPropertiesMethod.equalsIgnoreCase("true")</condition> <!-- condition merge-point--> <!-- cartridge-resource merge-point --> 1.10.2.2 +7 -0 cartridges/andromda-java/src/META-INF/andromda/namespace.xml Index: namespace.xml =================================================================== RCS file: /cvsroot/andromda/cartridges/andromda-java/src/META-INF/andromda/namespace.xml,v retrieving revision 1.10.2.1 retrieving revision 1.10.2.2 diff -u -w -r1.10.2.1 -r1.10.2.2 --- namespace.xml 27 Dec 2006 06:15:46 -0000 1.10.2.1 +++ namespace.xml 4 Feb 2008 01:31:38 -0000 1.10.2.2 @@ -75,6 +75,13 @@ represents the name of the interface. </documentation> </property> + <property name="generateEqualPropertiesMethod"> + <default>false</default> + <documentation> + When set to 'true' the cartridge will generate an equalProperties(..) method which can be used + to compare a value object's properties to that of another instance's. + </documentation> + </property> </propertyGroup> </properties> </namespace> \ No newline at end of file No revision No revision 1.18.4.17 +83 -0 cartridges/andromda-java/src/templates/java/ValueObject.vsl Index: ValueObject.vsl =================================================================== RCS file: /cvsroot/andromda/cartridges/andromda-java/src/templates/java/ValueObject.vsl,v retrieving revision 1.18.4.16 retrieving revision 1.18.4.17 diff -u -w -r1.18.4.16 -r1.18.4.17 --- ValueObject.vsl 18 Jul 2007 13:40:40 -0000 1.18.4.16 +++ ValueObject.vsl 4 Feb 2008 01:31:38 -0000 1.18.4.17 @@ -167,5 +167,88 @@ #end #end +## +## +## equality methods +## +## +## only render the macro in case there are properties to include +#if (!$class.properties.empty && $generateEqualPropertiesMethod) + /** + * Compares the properties of this instance to the properties of the argument. This method will return + * {@code false} as soon as it detects that the argument is {@code null} or not of the same type as + * (or a sub-type of) this instance's type. + * + * <p/>For array, collection or map properties the comparison will be done one level deep, in other words: + * the elements will be compared using the {@code equals()} operation. + * + * <p/>Note that two properties will be considered equal when both values are {@code null}. + * + * @param thatObject the object containing the properties to compare against this instance + * @return this method will return {@code true} in case the argument has the same type as this class, or is a + * sub-type of this class and all properties as found on this class have equal values when queried on that + * argument instance; in all other cases this method will return {@code false} + */ + public boolean equalProperties(java.lang.Object thatObject) + { + if (thatObject == null || !this.getClass().isAssignableFrom(thatObject.getClass())) + { + return false; + } + + final ${class.fullyQualifiedName} that = (${class.fullyQualifiedName})thatObject; + +#if ($class.generalization) + return super.equalProperties(that) +#set ($operator = "&& ") +#else + return +#set ($operator = "") +#end +#foreach ($property in $class.properties) +## no identifiers and constants, the 'constant' feature is defined in NmsEntity (= read-only and static) +#if (!$property.constant && !$property.identifier) + ${operator}equal(this.${property.getterName}(), that.${property.getterName}()) +#set ($operator = "&& ") +#end +#end + ; + } + +#if (!$class.generalization) + /** + * This is a convenient helper method which is able to detect whether or not two values are equal. Two values + * are equal when they are both {@code null}, are arrays of the same length with equal elements or are + * equal objects (this includes {@link java.util.Collection} and {@link java.util.Map} instances). + * + * <p/>Note that for array, collection or map instances the comparison runs one level deep. + * + * @param first the first object to compare, may be {@code null} + * @param first the second object to compare, may be {@code null} + * @return this method will return {@code true} in case both objects are equal as explained above; + * in all other cases this method will return {@code false} + */ + protected static boolean equal(java.lang.Object first, java.lang.Object second) + { + final boolean equal; + + if (first == null) + { + equal = second == null; + } + else if (first.getClass().isArray() && (second != null) && second.getClass().isArray()) + { + equal = java.util.Arrays.equals((java.lang.Object[])first, (java.lang.Object[])second); + } + else // note that the following also covers java.util.Collection and java.util.Map + { + equal = first.equals(second); + } + + return equal; + } + +#end +#end // ${class.fullyQualifiedName} value-object java merge-point } \ No newline at end of file No revision No revision 1.29.2.35 +65 -58 cartridges/andromda-java/src/test/expected/cartridge-output.zip <<Binary file>> |