    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package javax.jdo.pc;
    19
    20  import java.io.*;
    21  import java.util.*;
    22  import javax.jdo.PersistenceManager;
    23  import javax.jdo.spi.*;
    24
    25  import javax.jdo.spi.PersistenceCapable;
    26
    27  /**
    28   * This is a hand-enhanced version of a simple class with two fields. The
    29   * enhanced code assumes datastore identity.
    30   *
    31   * @version 2.0
    32   */
    33  public class PCPoint
    34      implements PersistenceCapable
    35  {
    36      public int x;
    37      public Integer y;
    38
    39      // JDO generated fields
    40      protected transient StateManager jdoStateManager;
    41      protected transient byte jdoFlags;
    42      private static final int jdoInheritedFieldCount = 0;
    43      private static final String jdoFieldNames[] = { "x", "y" };
    44      private static final Class jdoFieldTypes[];
    45      private static final byte jdoFieldFlags[] = {
    46          (byte)(PersistenceCapable.CHECK_READ + PersistenceCapable.CHECK_WRITE +
    47                 PersistenceCapable.SERIALIZABLE),
    48          (byte)(PersistenceCapable.CHECK_READ + PersistenceCapable.CHECK_WRITE +
    49                 PersistenceCapable.SERIALIZABLE),
    50      };
    51      private static final Class jdoPersistenceCapableSuperclass;
    52
    53      static
    54      {
    55          jdoFieldTypes = (new Class[] {
    56              Integer.TYPE, sunjdo$classForName$("java.lang.Integer")
    57          });
    58          jdoPersistenceCapableSuperclass = null;
    59          JDOImplHelper.registerClass(
    60              sunjdo$classForName$("javax.jdo.pc.PCPoint"),
    61              jdoFieldNames, jdoFieldTypes, jdoFieldFlags,
    62              jdoPersistenceCapableSuperclass, new PCPoint());
    63      }
    64
    65      /** JDO required no-args constructor. */
    66      public PCPoint() { }
    67
    68      /** Constructor. */
    69      public PCPoint(int x, Integer y) {
    70          jdoSetx(this, x);
    71          jdoSety(this, y);
    72      }
    73
    74      /** */
    75      public void setX(int x) {
    76          jdoSetx(this, x);
    77      }
    78
    79      /** */
    80      public int getX() {
    81          return jdoGetx(this);
    82      }
    83
    84      /** */
    85      public void setY(Integer y) {
    86          jdoSety(this, y);
    87      }
    88
    89      /** */
    90      public Integer getY() {
    91          return jdoGety(this);
    92      }
    93
    94      /** */
    95      public boolean equals(Object o) {
    96          if (o == null || !(o instanceof PCPoint))
    97              return false;
    98          PCPoint other = (PCPoint)o;
    99          if (jdoGetx(this) != jdoGetx(other))
    100             return false;
    101         if (jdoGety(this) == null)
    102             return jdoGety(other) == null;
    103         if (jdoGety(other) == null)
    104             return jdoGety(this) == null;
    105         else
    106             return jdoGety(this).intValue() == jdoGety(other).intValue();
    107     }
    108
    109     /** */
    110     public int hashCode() {
    111         int code = getX();
    112         if (getY() != null) {
    113             code += getY().intValue();
    114         }
    115         return code;
    116     }
    117
    118     /** */
    119     public String toString() {
    120         return "PCPoint(x: " + getX() + ", y: " + getY() + ")";
    121     }
    122
    123
    124     // Generated methods in least-derived PersistenceCapable class
    125
    126     public final boolean jdoIsPersistent() {
    127         StateManager statemanager = jdoStateManager;
    128         if (statemanager != null)
    129             return statemanager.isPersistent(this);
    130         else
    131             return false;
    132     }
    133
    134     public final boolean jdoIsTransactional() {
    135         StateManager statemanager = jdoStateManager;
    136         if (statemanager != null)
    137             return statemanager.isTransactional(this);
    138         else
    139             return false;
    140     }
    141
    142     public final boolean jdoIsNew() {
    143         StateManager statemanager = jdoStateManager;
    144         if (statemanager != null)
    145             return statemanager.isNew(this);
    146         else
    147             return false;
    148     }
    149
    150     public final boolean jdoIsDirty() {
    151         StateManager statemanager = jdoStateManager;
    152         if (statemanager != null)
    153             return statemanager.isDirty(this);
    154         else
    155             return false;
    156     }
    157
    158     public final boolean jdoIsDeleted() {
    159         StateManager statemanager = jdoStateManager;
    160         if (statemanager != null)
    161             return statemanager.isDeleted(this);
    162         else
    163             return false;
    164     }
    165
    166     public final boolean jdoIsDetached() {
    167         return false;
    168     }
    169
    170     public final void jdoMakeDirty(String s) {
    171         StateManager statemanager = jdoStateManager;
    172         if (statemanager != null)
    173             statemanager.makeDirty(this, s);
    174     }
    175
    176     public final PersistenceManager jdoGetPersistenceManager() {
    177         StateManager statemanager = jdoStateManager;
    178         if (statemanager != null)
    179             return statemanager.getPersistenceManager(this);
    180         else
    181             return null;
    182     }
    183
    184     public final Object jdoGetObjectId() {
    185         StateManager statemanager = jdoStateManager;
    186         if (statemanager != null)
    187             return statemanager.getObjectId(this);
    188         else
    189             return null;
    190     }
    191
    192     public final Object jdoGetTransactionalObjectId() {
    193         StateManager statemanager = jdoStateManager;
    194         if (statemanager != null)
    195             return statemanager.getTransactionalObjectId(this);
    196         else
    197             return null;
    198     }
    199
    200     public final Object jdoGetVersion() {
    201         StateManager statemanager = jdoStateManager;
    202         if (statemanager != null)
    203             return statemanager.getVersion(this);
    204         else
    205             return null;
    206     }
    207
    208     public final synchronized void jdoReplaceStateManager(
    209         StateManager statemanager) {
    210         StateManager statemanager1 = jdoStateManager;
    211         if (statemanager1 != null) {
    212             jdoStateManager = statemanager1.replacingStateManager(
    213                 this, statemanager);
    214             return;
    215         }
    216         else {
    217             JDOImplHelper.checkAuthorizedStateManager(statemanager);
    218             jdoStateManager = statemanager;
    219             jdoFlags = PersistenceCapable.LOAD_REQUIRED;
    220             return;
    221         }
    222     }
    223
    224     public final void jdoReplaceFlags() {
    225         StateManager statemanager = jdoStateManager;
    226         if (statemanager != null)
    227             jdoFlags = statemanager.replacingFlags(this);
    228     }
    229
    230     public final void jdoReplaceFields(int fields[]) {
    231         if (fields == null)
    232             throw new IllegalArgumentException("fields is null");
    233         int i = fields.length;
    234         for(int j = 0; j < i; j++)
    235             jdoReplaceField(fields[j]);
    236
    237     }
    238
    239     public final void jdoProvideFields(int fields[]) {
    240         if (fields == null)
    241             throw new IllegalArgumentException("fields is null");
    242         int i = fields.length;
    243         for(int j = 0; j < i; j++)
    244             jdoProvideField(fields[j]);
    245
    246     }
    247
    248     protected final void jdoPreSerialize() {
    249         StateManager statemanager = jdoStateManager;
    250         if (statemanager != null)
    251             statemanager.preSerialize(this);
    252     }
    253
    254     // Generated methods in PersistenceCapable root classes and all classes
    255     // that declare objectid-class in xml metadata:
    256
    257     public void jdoCopyKeyFieldsToObjectId(
    258         PersistenceCapable.ObjectIdFieldSupplier objectidfieldsupplier,
    259         Object obj) { }
    260
    261     public void jdoCopyKeyFieldsToObjectId(Object obj) {
    262     }
    263
    264     public void jdoCopyKeyFieldsFromObjectId(
    265         PersistenceCapable.ObjectIdFieldConsumer objectidfieldconsumer,
    266         Object obj) { }
    267
    268     protected void jdoCopyKeyFieldsFromObjectId(Object obj) {
    269     }
    270
    271     public Object jdoNewObjectIdInstance() {
    272         return null;
    273     }
    274
    275     public Object jdoNewObjectIdInstance(Object o) {
    276         return null;
    277     }
    278
    279     // Generated methods in all PersistenceCapable classes
    280
    281     public PersistenceCapable jdoNewInstance(StateManager statemanager) {
    282         PCPoint pcpoint = new PCPoint();
    283         pcpoint.jdoFlags = PersistenceCapable.LOAD_REQUIRED;
    284         pcpoint.jdoStateManager = statemanager;
    285         return pcpoint;
    286     }
    287
    288     public PersistenceCapable jdoNewInstance(
    289         StateManager statemanager, Object obj) {
    290         PCPoint pcpoint = new PCPoint();
    291         pcpoint.jdoCopyKeyFieldsFromObjectId(obj);
    292         pcpoint.jdoFlags = PersistenceCapable.LOAD_REQUIRED;
    293         pcpoint.jdoStateManager = statemanager;
    294         return pcpoint;
    295     }
    296
    297     protected static int jdoGetManagedFieldCount() {
    298         return 2;
    299     }
    300
    301      public static final int jdoGetx(PCPoint pcpoint) {
    302         if (pcpoint.jdoFlags <= PersistenceCapable.READ_WRITE_OK)
    303             return pcpoint.x;
    304         StateManager statemanager = pcpoint.jdoStateManager;
    305         if (statemanager == null)
    306             return pcpoint.x;
    307         if (statemanager.isLoaded(pcpoint, jdoInheritedFieldCount + 0))
    308             return pcpoint.x;
    309         else
    310             return statemanager.getIntField(
    311                 pcpoint, jdoInheritedFieldCount + 0, pcpoint.x);
    312     }
    313
    314     public static final Integer jdoGety(PCPoint pcpoint) {
    315         if (pcpoint.jdoFlags <= PersistenceCapable.READ_WRITE_OK)
    316             return pcpoint.y;
    317         StateManager statemanager = pcpoint.jdoStateManager;
    318         if (statemanager == null)
    319             return pcpoint.y;
    320         if (statemanager.isLoaded(pcpoint, jdoInheritedFieldCount + 1))
    321             return pcpoint.y;
    322         else
    323             return (Integer)statemanager.getObjectField(
    324                 pcpoint, jdoInheritedFieldCount + 1, pcpoint.y);
    325     }
    326
    327     public static final void jdoSetx(PCPoint pcpoint, int i) {
    328         if (pcpoint.jdoFlags == PersistenceCapable.READ_WRITE_OK) {
    329             pcpoint.x = i;
    330             return;
    331         }
    332         StateManager statemanager = pcpoint.jdoStateManager;
    333         if (statemanager == null) {
    334             pcpoint.x = i;
    335             return;
    336         }
    337         else {
    338             statemanager.setIntField(
    339                 pcpoint, jdoInheritedFieldCount + 0, pcpoint.x, i);
    340             return;
    341         }
    342     }
    343
    344     public static final void jdoSety(PCPoint pcpoint, Integer integer) {
    345         if (pcpoint.jdoFlags == PersistenceCapable.READ_WRITE_OK) {
    346             pcpoint.y = integer;
    347             return;
    348         }
    349         StateManager statemanager = pcpoint.jdoStateManager;
    350         if (statemanager == null) {
    351             pcpoint.y = integer;
    352             return;
    353         }
    354         else {
    355             statemanager.setObjectField(pcpoint, jdoInheritedFieldCount + 1, pcpoint.y, integer);
    356             return;
    357         }
    358     }
    359
    360     public void jdoReplaceField(int field) {
    361         StateManager statemanager = jdoStateManager;
    362         switch(field - jdoInheritedFieldCount) {
    363         case 0:
    364             if (statemanager == null) {
    365                 throw new IllegalStateException("jdoStateManager is null");
    366             }
    367             else {
    368                 x = statemanager.replacingIntField(this, field);
    369                 return;
    370             }
    371         case 1:
    372             if (statemanager == null) {
    373                 throw new IllegalStateException("jdoStateManager is null");
    374             }
    375             else {
    376                 y = (Integer)statemanager.replacingObjectField(this, field);
    377                 return;
    378             }
    379         }
    380         throw new IllegalArgumentException("field number out of range");
    381     }
    382
    383     public void jdoProvideField(int field) {
    384         StateManager statemanager = jdoStateManager;
    385         switch(field - jdoInheritedFieldCount) {
    386         case 0:
    387             if (statemanager == null) {
    388                 throw new IllegalStateException("jdoStateManager is null");
    389             }
    390             else {
    391                 statemanager.providedIntField(this, field, x);
    392                 return;
    393             }
    394         case 1:
    395             if (statemanager == null) {
    396                 throw new IllegalStateException("jdoStateManager is null");
    397             }
    398             else {
    399                 statemanager.providedObjectField(this, field, y);
    400                 return;
    401             }
    402         }
    403         throw new IllegalArgumentException("field number out of range");
    404     }
    405
    406     public void jdoCopyFields(Object obj, int fieldNumbers[]) {
    407         if (jdoStateManager == null)
    408             throw new IllegalStateException("jdoStateManager is null");
    409         if (!(obj instanceof PCPoint))
    410             throw new ClassCastException(obj.getClass().getName());
    411         if (fieldNumbers == null)
    412             throw new IllegalArgumentException("fieldNumber is null");
    413         PCPoint pcpoint = (PCPoint)obj;
    414         if (pcpoint.jdoStateManager != jdoStateManager)
    415             throw new IllegalArgumentException("wrong jdoStateManager");
    416         int i = fieldNumbers.length;
    417         for(int j = 0; j < i; j++)
    418             jdoCopyField(pcpoint, fieldNumbers[j]);
    419     }
    420
    421     protected final void jdoCopyField(PCPoint pcpoint, int fieldNumber)
    422     {
    423         switch(fieldNumber - jdoInheritedFieldCount) {
    424         case 0:
    425             if (pcpoint == null) {
    426                 throw new IllegalArgumentException("pcpoint is null");
    427             }
    428             else {
    429                 x = pcpoint.x;
    430                 return;
    431             }
    432         case 1:
    433             if (pcpoint == null) {
    434                 throw new IllegalArgumentException("pcpoint is null");
    435             }
    436             else {
    437                 y = pcpoint.y;
    438                 return;
    439             }
    440         }
    441         throw new IllegalArgumentException("field number out of range");
    442     }
    443
    444     private void writeObject(java.io.ObjectOutputStream out)
    445         throws java.io.IOException {
    446         jdoPreSerialize();
    447         out.defaultWriteObject();
    448     }
    449
    450     protected static final Class sunjdo$classForName$(String s) {
    451         try {
    452             return Class.forName(s);
    453         }
    454         catch(ClassNotFoundException ex) {
    455             throw new NoClassDefFoundError(ex.getMessage());
    456         }
    457     }
    458
    459
    460
    461 }