You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by Apache Wiki <wi...@apache.org> on 2014/04/21 22:34:49 UTC

[Jclouds Wiki] Update of "templating" by ZackShoylev

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jclouds Wiki" for change notification.

The "templating" page has been changed by ZackShoylev:
https://wiki.apache.org/jclouds/templating

New page:
= Templating =

When implementing large or numerous domain objects, these usually require getters, hashCode, equals, toString, and Builder support, and even constructors. All this is repetitive, and because usually these classes should not need much extra functionality, can be templated.

I use this eclipse plugin for templating: http://www.3pintech.com/products/fast-code/

How to use:
Window -> Preferences -> Fast Code -> Templates -> Templates -> New
Template Name: jclouds CreateSomething domain class
Description: creates a domain class
Allowed File Names: *.java
Allow Multiple Variation: Not checked
Variations: [leave empty]
First Template Item: Class
Second Template Item: Field
Number Required items: 1
Required Getter Setter: none
Additional parameters: [leave empty]

== A full domain class ==

{{{
#set ($cpOutput = "")
#set ($length = 0)
#set ($Qt = '"')
#foreach ($field in ${fields})
	#set ($length = $length + 1)
#end
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	#set ($cpOutput = "${cpOutput}${Qt}${field.name}${Qt}")
	#if (!${last})#set ($cpOutput = "${cpOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput = "${fOutput}${field.type.name} ${field.name}")
	#if (!${last})#set ($fOutput = "${fOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput2 = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput2 = "${fOutput2}${field.name}")
	#if (!${last})#set ($fOutput2 = "${fOutput2}, ")#end
#set ($i = $i + 1)
#end


   @ConstructorProperties({${cpOutput}})
   protected ${class.name}(${fOutput}) {
#foreach ($field in ${fields})
     this.${field.name} = ${field.name};
#end
   }
   
#foreach ($field in ${fields})
   /**
   * @return the ${field.name} of the ${class.name}
   */
   public ${field.type.name} ${field.getter}() {
      return ${field.name};
   }
#end

   @Override
   public int hashCode() {
      return Objects.hashCode(${fOutput2});
   }
   
   @Override
   public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null || getClass() != obj.getClass()) return false;
         ${class.name} that = ${class.name}.class.cast(obj);
         return
#set ($i = 0)
#foreach ($field in ${fields})
#if(${i} > 0)
            && Objects.equal(this.${field.name}, that.${field.name})
#else
            Objects.equal(this.${field.name}, that.${field.name})
#end
#set ($i = $i + 1)
#end
	 ;
   }
   
   protected Objects.ToStringHelper string() {
         return Objects.toStringHelper(this)
         #foreach ($field in ${fields})
	    .add("${field.name}", ${field.name})
	 #end
	;
   }

   @Override
   public String toString() {
      return string().toString();
   }

   /**
   * @return the Builder for ${class.name}
   */
   public static Builder builder() {
      return new Builder();
   }
   
   /**
   * Gets a Builder configured as this object.
   */
   public Builder toBuilder() {
      return new Builder().from${class.name}(this);
   }
   
   public static class Builder {
#foreach ($field in ${fields})
     protected ${field.type.name} ${field.name};
#end
#foreach ($field in ${fields})

      /**
      * Provide the ${field.name} to the ${class.name}'s Builder.
      * @return the Builder.
      * @see ${class.name}#${field.getter}()
      */
      public Builder ${field.name}(${field.type.name} ${field.name}) {
         this.${field.name} = ${field.name};
         return this;
      }
#end

      /**
      * @return a ${class.name} constructed with this Builder.
      */
      public ${class.name} build() {
         return new ${class.name}(${fOutput2});
      }
      
      /**
      * @return a Builder from another ${class.name}.
      */
      public Builder from${class.name}(${class.name} in) {
         return this
#foreach ($field in ${fields})
         .${field.name}(in.${field.getter}())
#end
	;
      }
   }
}}}

== Hashcode, equals, string only ==

{{{
#set ($cpOutput = "")
#set ($length = 0)
#set ($Qt = '"')
#foreach ($field in ${fields})
	#set ($length = $length + 1)
#end
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	#set ($cpOutput = "${cpOutput}${Qt}${field.name}${Qt}")
	#if (!${last})#set ($cpOutput = "${cpOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput = "${fOutput}${field.type.name} ${field.name}")
	#if (!${last})#set ($fOutput = "${fOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput2 = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput2 = "${fOutput2}${field.name}")
	#if (!${last})#set ($fOutput2 = "${fOutput2}, ")#end
#set ($i = $i + 1)
#end

   @Override
   public int hashCode() {
      return Objects.hashCode(${fOutput2});
   }
   
   @Override
   public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null || getClass() != obj.getClass()) return false;
         ${class.name} that = ${class.name}.class.cast(obj);
         return
#set ($i = 0)
#foreach ($field in ${fields})
#if(${i} > 0)
            && Objects.equal(this.${field.name}, that.${field.name})
#else
            Objects.equal(this.${field.name}, that.${field.name})
#end
#set ($i = $i + 1)
#end
	 ;
   }
   
   protected Objects.ToStringHelper string() {
         return Objects.toStringHelper(this)
         #foreach ($field in ${fields})
	    .add("${field.name}", ${field.name})
	 #end
	;
   }

   @Override
   public String toString() {
      return string().toString();
   }
}}}

== Something extends CreateSomething domain teamplete ==

Note: This one requires some manual steps.

{{{
#set ($cpOutput = "")
#set ($length = 0)
#set ($Qt = '"')
#foreach ($field in ${fields})
	#set ($length = $length + 1)
#end
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	#set ($cpOutput = "${cpOutput}${Qt}${field.name}${Qt}")
	#if (!${last})#set ($cpOutput = "${cpOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput = "${fOutput}${field.type.name} ${field.name}")
	#if (!${last})#set ($fOutput = "${fOutput}, ")#end
#set ($i = $i + 1)
#end
#set ($fOutput2 = "")
#set ($i = 0)
#foreach ($field in ${fields})
	#if (${i} < $length - 1)
		#set ($last = false)
	#else
		#set ($last = true)
	#end	
	
	#set ($fOutput2 = "${fOutput2}${field.name}")
	#if (!${last})#set ($fOutput2 = "${fOutput2}, ")#end
#set ($i = $i + 1)
#end

   /* TODO: Ensure it works with parent fields */
   @ConstructorProperties({${cpOutput}})
   protected ${class.name}(${fOutput}) {
     super(...); // parent fields
#foreach ($field in ${fields})
     this.${field.name} = ${field.name};
#end
   }
   
#foreach ($field in ${fields})
   /**
   * @return the ${field.name} of the ${class.name}
   */
   public ${field.type.name} ${field.getter}() {
      return ${field.name};
   }
#end

   @Override
   public int hashCode() {
      return Objects.hashCode(super.hashCode(), ${fOutput2});
   }
   
   @Override
   public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null || getClass() != obj.getClass()) return false;
         ${class.name} that = ${class.name}.class.cast(obj);
         return super.equals(obj)
#foreach ($field in ${fields})
            && Objects.equal(this.${field.name}, that.${field.name})
#end
	 ;
   }
   
   protected Objects.ToStringHelper string() {
         return super.string()
         #foreach ($field in ${fields})
	    .add("${field.name}", ${field.name})
	 #end
	;
   }

   @Override
   public String toString() {
      return string().toString();
   }
}}}