View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.damage;
6   
7   import util.Nullable;
8   
9   /***
10   * The type for damage being dealt.
11   *
12   * DamageType uses a Flyweight pattern and instances are created through the
13   * getX() calls.
14   *
15   * @author  Barrie Treloar
16   */
17  public class DamageType implements Nullable {
18      
19      /*** determines whether this object is a <code>Nullable</code>. */
20      private boolean nullable;
21  
22      /***
23       * Set whether this object is a <code>Nullable</code>.
24       *
25       * @param isNullable true if this object is a Null instance, false
26       *        otherwise.
27       */
28      public final void setNull(boolean isNullable) {
29          nullable = isNullable;
30      }
31  
32      /***
33       * {@inheritDoc}
34       */
35      public final boolean isNull() {
36          return nullable;
37      }
38      
39      /*** 
40       * DamageType is not instantied but accessed from the Flyweight
41       * factory methods getX(), where X is one of the available damage types.
42       */
43      protected DamageType(String type_) {
44          setType(type_);
45      }
46  
47      /***
48       * A damage type is equal to another damage type if they have the same type.
49       */
50      public boolean equals( Object o )
51      {
52          boolean result = false;
53  
54          if ( o instanceof DamageType )
55          {
56              DamageType other = (DamageType) o;
57              result = getType().equals( other.getType() );
58          }
59          return result;
60      }
61  
62      /***
63       * The string value of a damage type is it's type.
64       */
65      public String toString() {
66          return getType();
67      }
68      
69      private String type;
70      public String getType() { return type; }
71      public void setType(String type_) { type = type_; }
72      
73      private static DamageType physicalDamageType_;
74      
75      public static final DamageType getPhysicalDamageType() {
76          if (physicalDamageType_ == null) {
77              physicalDamageType_ = new DamageType("physical");
78          }
79          return physicalDamageType_;
80      }
81      
82      private static DamageType fireDamageType_;
83      
84      public static final DamageType getFireDamageType() {
85          if (fireDamageType_ == null) {
86              fireDamageType_ = new DamageType("fire");
87          }
88          return fireDamageType_;
89      }
90      
91      private static DamageType coldDamageType_;
92      public static final DamageType getColdDamageType() {
93          if (coldDamageType_ == null) {
94              coldDamageType_ = new DamageType("cold");
95          }
96          return coldDamageType_;
97      }
98      
99      /***
100      * List of all the <code>DamageType</code>s.
101      */
102     public static final DamageType[] ALL_DAMAGE_TYPES =
103     {
104         getColdDamageType(), getFireDamageType(),
105         getPhysicalDamageType()
106     };
107     
108 }