Dear Wiki user,

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

The "Enums" page has been changed by ZackShoylev:
https://wiki.apache.org/jclouds/Enums?action=diff&rev1=6&rev2=7

  ## page was copied from Templating
  ## page was renamed from templating
- = Templating =
+ = Enums =
  
+ jclouds's GSON adapters support enum types. This sometimes requires the enum 
type to provide a fromValue method.
+ Here is an example that
+  * Supports custom serialization/deserialization values for the enum 
constants.
+  * Supports UNRECOGNIZED for unknown values (when deserializing)
+  * Supports null values
- 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]
- 
- Use the code below for the templates. 
- 
- To apply the template, use Ctrl + Alt + Shift + T. Specify your class and all 
the field. The code will be inserted at the cursor. Make sure to Source -> 
Format after, as the template code will not be formatted. If your eclipse is 
configured using the jclouds formatter, it should format the code properly.
- 
- == A full domain class ==
  
  {{{
+ public enum NetworkStatus {
+    ACTIVE("active"), 
+    DOWN("down"),
+    BUILD("build"), 
+    ERROR("error"),
+    UNRECOGNIZED("unrecognized");
+    
+    private final String name;
- #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
  
+    private NetworkStatus(String name) {
+       this.name = name;
-    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 name();
     }
  
     /**
-    * @return the Builder for ${class.name}
+     * This provides GSON enum support in jclouds.
+     * @param name The string representation of this enum value.
+     * @return The corresponding enum value.
-    */
+     */
-    public static Builder builder() {
-       return new Builder();
+    public static NetworkStatus fromValue(String name) {
+       if (name != null) {
+          for (NetworkStatus value : NetworkStatus.values()) {
+            if (name.equalsIgnoreCase(value.name)) {
+              return value;
+            }
+          }
+          return UNRECOGNIZED;
+        }
+        return null;
     }
+ }
-    
-    /**
-    * 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, toString 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 template ==
- 
- 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 */
-    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();
-    }
- }}}
- 

Reply via email to