home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-24 | 2.6 KB | 95 lines |
- package COM.odi.demo.rep;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * A Rectangle has two Points, representing its upper-left
- * and lower-right corners. However, its persistent representation
- * is formed by storing the x and y coordinates of the two points,
- * rather than the points themselves. This demonstates the control
- * that the definer of a persistent class has over the persistent
- * representation. Note that Identity of the Point objects is not,
- * preserved, since the Point objects are not persistent objects.
- */
-
- import COM.odi.*;
-
- public class Rectangle extends Persistent {
- transient Point a;
- transient Point b;
-
- static RectangleClassInfo classInfo;
-
- Rectangle(Point a, Point b) {
- this.a = a;
- this.b = b;
- }
-
- void describe() {
- System.out.println("Rectangle with two points:");
- a.describe();
- b.describe();
- }
-
- /* Annotations for persistence. */
-
- Rectangle(ClassInfo ignored) {}
-
- public void initializeContents(GenericObject handle) {
- a = new Point(handle.getIntField(1, classInfo),
- handle.getIntField(2, classInfo));
- b = new Point(handle.getIntField(3, classInfo),
- handle.getIntField(4, classInfo));
- }
-
- public void flushContents(GenericObject handle) {
- handle.setIntField(1, a.x, classInfo);
- handle.setIntField(2, a.y, classInfo);
- handle.setIntField(3, b.x, classInfo);
- handle.setIntField(4, b.y, classInfo);
- }
-
- public void clearContents() {
- a = null;
- b = null;
- }
-
- /* This class is never used as a persistent hash key. */
- public int hashCode() {
- return super.hashCode();
- }
-
- static {
- classInfo = new RectangleClassInfo();
- ClassInfo.register(classInfo);
- }
- }
-
- class RectangleClassInfo extends ClassInfo
- {
- public Persistent create() { return new Rectangle(this); }
- public Object[] createArray(int nDimensions,
- int sizeOfOuterDimension)
- throws BadArrayDimensionsException {
- switch (nDimensions) {
- case 1: return new Rectangle[sizeOfOuterDimension];
- case 2:
- /* The following has been broken into two lines to workaround a J++/JVC bug. */
- Object ret = new Rectangle[sizeOfOuterDimension][];
- return (Object[]) ret;
- default: throw new BadArrayDimensionsException(nDimensions);
- }
- }
-
- public Class getClassDescriptor() throws ClassNotFoundException {
- return Class.forName("COM.odi.demo.rep.Rectangle");
- }
-
- public Field[] getFields() { return fields; }
- private static Field[] fields =
- { Field.createInt("ax"),
- Field.createInt("ay"),
- Field.createInt("bx"),
- Field.createInt("by"), };
- }
-