home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / Rectangle.java < prev    next >
Encoding:
Java Source  |  1997-01-24  |  2.6 KB  |  95 lines

  1. package COM.odi.demo.rep;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * A Rectangle has two Points, representing its upper-left
  7.  * and lower-right corners.  However, its persistent representation
  8.  * is formed by storing the x and y coordinates of the two points,
  9.  * rather than the points themselves.  This demonstates the control
  10.  * that the definer of a persistent class has over the persistent
  11.  * representation.  Note that Identity of the Point objects is not,
  12.  * preserved, since the Point objects are not persistent objects.
  13.  */
  14.  
  15. import COM.odi.*;
  16.  
  17. public class Rectangle extends Persistent {
  18.   transient Point a;
  19.   transient Point b;
  20.  
  21.   static RectangleClassInfo classInfo;
  22.  
  23.   Rectangle(Point a, Point b) {
  24.     this.a = a;
  25.     this.b = b;
  26.   }
  27.  
  28.   void describe() {
  29.     System.out.println("Rectangle with two points:");
  30.     a.describe();
  31.     b.describe();
  32.   }
  33.  
  34.   /* Annotations for persistence. */
  35.  
  36.   Rectangle(ClassInfo ignored) {}
  37.  
  38.   public void initializeContents(GenericObject handle) {
  39.     a = new Point(handle.getIntField(1, classInfo),
  40.           handle.getIntField(2, classInfo));
  41.     b = new Point(handle.getIntField(3, classInfo),
  42.           handle.getIntField(4, classInfo));
  43.   }
  44.  
  45.   public void flushContents(GenericObject handle) {
  46.     handle.setIntField(1, a.x, classInfo);
  47.     handle.setIntField(2, a.y, classInfo);
  48.     handle.setIntField(3, b.x, classInfo);
  49.     handle.setIntField(4, b.y, classInfo);
  50.   }
  51.  
  52.   public void clearContents() {
  53.     a = null;
  54.     b = null;
  55.   }
  56.  
  57.   /* This class is never used as a persistent hash key. */
  58.   public int hashCode() {
  59.     return super.hashCode();
  60.   }
  61.  
  62.   static {
  63.     classInfo = new RectangleClassInfo();
  64.     ClassInfo.register(classInfo);
  65.   }
  66. }
  67.  
  68. class RectangleClassInfo extends ClassInfo
  69. {
  70.   public Persistent create() { return new Rectangle(this); }
  71.   public Object[] createArray(int nDimensions,
  72.                   int sizeOfOuterDimension)
  73.           throws BadArrayDimensionsException {
  74.     switch (nDimensions) {
  75.     case 1: return new Rectangle[sizeOfOuterDimension];
  76.     case 2: 
  77.       /* The following has been broken into two lines to workaround a J++/JVC bug. */
  78.       Object ret = new Rectangle[sizeOfOuterDimension][];
  79.       return (Object[]) ret;
  80.     default: throw new BadArrayDimensionsException(nDimensions);
  81.     }
  82.   }
  83.   
  84.   public Class getClassDescriptor() throws ClassNotFoundException {
  85.     return Class.forName("COM.odi.demo.rep.Rectangle");
  86.   }
  87.   
  88.   public Field[] getFields() { return fields; }
  89.   private static Field[] fields =
  90.   { Field.createInt("ax"),
  91.     Field.createInt("ay"),
  92.     Field.createInt("bx"),
  93.     Field.createInt("by"), };
  94. }
  95.