Java Q&A
by Evan Easton

    
Listing One
// Pass in an invalid alignment
new Label("Read me!", 4);


Listing Two
java.lang.IllegalArgumentException: improper alignment: 4
    at java.awt.Label.setAlignment(Label.java:184) 
    at java.awt.Label.<init>(Label.java:127)
    at ...


Listing Three
// Pass in an invalid layout constraint 
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JButton("Push Me!"), -TOP+); 


Listing Four
java.lang.IllegalArgumentException: illegal component position
         at java.awt.Container.addImpl(Compiled Code) 
         at java.awt.Container.add(Container.java:230) 
         at ...


Listing Five
// Pass in an invalid Calendar field 
Calendar calendar = Calendar.getInstance(); 
calendar.set(123456, 2);


Listing Six
java.lang.ArrayIndexOutOfBoundsException: 123456
         at java.util.Calendar.set(Calendar.java:767) 
         at ...


Listing Seven
public class Label
  extends Component
{
  public static final HorizontalAlignment LEFT = new HorizontalAlignment(); 
  public static final HorizontalAlignment CENTER = new HorizontalAlignment(); 
  public static final HorizontalAlignment RIGHT = new HorizontalAlignment();
     
  private HorizontalAlignment alignment_; 
  public static class HorizontalAlignment
        {
                // can only be invoke inside Label class scope 
                private HorizontalAlignment()
                {
                }
        }
        public Label(String text, HorizontalAlignment alignment) 
        {
                ...
                setAlignment(alignment);
        }
        public void setAlignment(HorizontalAlignment alignment) 
        {
                // just make the assignment, no check needed 
                alignment_ = alignment;
                ...
        }
        ...
}
     

Listing Eight
// Valid at compile- and run-time
Label label = new Label("Read Me!", Label.RIGHT);
     
// Invalid at compile-time, which is good! 
label = new Label("Read Me!", 4);


Listing Nine
public class Label
  extends Component
  implements java.io.Serializable
{
  public static final HorizontalAlignment LEFT = new HorizontalAlignment(0); 
  public static final HorizontalAlignment CENTER = new HorizontalAlignment(1); 
  public static final HorizontalAlignment RIGHT = new HorizontalAlignment(2); 
  private HorizontalAlignment alignment_ = LEFT;
  /** Enum class for horizontal alignment of Labels. */
         public static class HorizontalAlignment
                 implements java.io.Serializable
         {
                 private int alignValue_;
                 // can only be invoke inside Label class scope 
                 private HorizontalAlignment(int align_value)
                 {
                         alignValue_ = align_value;
                 }
                 private int asInt()
                 {
                         return alignValue_;
                 }
                 /** Required for equality comparisons. */ 
                 public int hashCode()
                 {
                         return asInt();
                 }
                 /** Required for equality comparisons. */ 
                 public boolean equals(Object obj)
                 {
                   boolean ret = (obj == this);
                   if(!ret && obj != null && obj instanceof HorizontalAlignment)
                         ret = ((HorizontalAlignment)rhs).asInt() == asInt();
                   return ret;
                 }
         }
         public Label(String text, HorizontalAlignment alignment) 
         {
                 ...
                 setAlignment(alignment);
         }
         public void setAlignment(HorizontalAlignment alignment) 
         {
                 alignment_ = alignment;
                 ...
         }
         ...
}


Listing Ten
public final class HorizontalAlignment
  implements java.io.Serializable
{
  public static final HorizontalAlignment LEFT = new HorizontalAlignment(0); 
  public static final HorizontalAlignment CENTER = new HorizontalAlignment(1); 
  public static final HorizontalAlignment RIGHT = new HorizontalAlignment(2);
     
  private int alignValue_;
     
  // can only be invoke inside HorizontalAlignment class scope 
  private HorizontalAlignment(int align_value)
  {
                 alignValue_ = align_value;
  }
         /** Allow others to get the int value. Use carefully. */ 
         public int asInt()
         {
                 return alignValue_;
         }
         /** Required for equality comparisons. */ 
         public int hashCode()
         {
                 return asInt();
         }
         /** Required for equality comparisons. */ 
         public boolean equals(Object rhs)
         {
                 boolean ret = (obj == this);
                 if(!ret && obj != null && obj instanceof HorizontalAlignment)
                         ret = ((HorizontalAlignment)rhs).asInt() == asInt();
                 return ret;
         }
}


Listing Eleven
public final class HorizontalAlignment
   implements java.io.Serializable
 {
         // new field
   private static Hashtable instanceDictionary__ = new Hashtable(3, 0.75f);
     
         // changed constructor
         private HorizontalAlignment(int align_value) 
         {
                 alignValue_ = align_value;
                 // Store this in the instance dictionary using the
                 // int value as the key.  Needs to check for duplicate 
                 instanceDictionary__.put(new Integer(align_value), this);
         }
         // new method
         protected Object readResolve()
                 throws ObjectStreamException
         {
            // Simple look up of the instance with the same integer 
            // value as mine.  Needs to check for null entry.
            return (HorizontalAlignment)instanceDictionary__.get(new 
                                                          Integer(asInt()));
         }
         // all other fields and methods unchanged 
         ...
}


Listing Twelve
public class HorizontalAlignment
  extends Enum
{
  public static final HorizontalAlignment LEFT = new HorizontalAlignment(0); 
  public static final HorizontalAlignment CENTER = new HorizontalAlignment(1); 
  public static final HorizontalAlignment RIGHT = new HorizontalAlignment(2);
     
  private HorizontalAlignment(int id)
  {
                 super(id);
  }
}


Listing Thirteen 
public synchronized void setAlignment(int alignment) 
{
         switch (alignment) {
                 case LEFT:
                 case CENTER:
                 case RIGHT:
                         this.alignment = alignment;
                         LabelPeer peer = (LabelPeer)this.peer; 
                         if (peer != null) {
                                 peer.setAlignment(alignment);
                         }
                         return;
      }
      throw new IllegalArgumentException("improper alignment: " + alignment);
}


Listing Fourteen
public synchronized void setAlignment(HorizontalAlignment alignment) 
{
         alignment_ = alignment;
         LabelPeer peer = (LabelPeer)this.peer; 
         if(peer != null)
         {
                 peer.setAlignment(alignment.asInt());
         }
}
     




