Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.companyAnnotatedJPA » [javadoc | source]
    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 org.apache.jdo.tck.pc.companyAnnotatedJPA;
   19   
   20   import java.io.Serializable;
   21   import java.util.Comparator;
   22   
   23   import javax.persistence;
   24   
   25   import org.apache.jdo.tck.pc.company.IAddress;
   26   import org.apache.jdo.tck.util.DeepEquality;
   27   import org.apache.jdo.tck.util.EqualityHelper;
   28   
   29   /**
   30    * This class represents a postal address.
   31    */
   32   @Embeddable
   33   public class JPAAppAddress 
   34       implements IAddress, Serializable, Comparable, Comparator, DeepEquality {
   35   
   36       private long    addrid;
   37       private String  street;
   38       private String  city;
   39       private String  state;
   40       private String  zipcode;
   41       private String  country;
   42   
   43       /** This is the JDO-required no-args constructor. The TCK relies on
   44        * this constructor for testing PersistenceManager.newInstance(PCClass).
   45        */
   46       public JPAAppAddress() {}
   47   
   48       /**
   49        * This constructor initializes the <code>JPAAppAddress</code> components.
   50        * 
   51        * 
   52        * 
   53        * @param addrid The address ID.
   54        * @param street The street address.
   55        * @param city The city.
   56        * @param state The state.
   57        * @param zipcode The zip code.
   58        * @param country The zip country.
   59        */
   60       public JPAAppAddress(long addrid, String street, String city, 
   61                      String state, String zipcode, String country)
   62       {
   63           this.addrid = addrid;
   64           this.street = street;
   65           this.city = city;
   66           this.state = state;
   67           this.zipcode = zipcode;
   68           this.country = country;
   69       }
   70   
   71       /**
   72        * Get the addrid associated with this object.
   73        * @return the addrid.
   74        */
   75       public long getAddrid() {
   76           return addrid;
   77       }
   78   
   79       /**
   80        * Set the id associated with this object.
   81        * @param id the id.
   82        */
   83       public void setAddrid(long id) {
   84           if (this.addrid != 0)
   85               throw new IllegalStateException("Id is already set.");
   86           this.addrid = id;
   87       }
   88   
   89       /** 
   90        * Get the street component of the address.
   91        * @return The street component of the address.
   92        */
   93       public String getStreet() {
   94           return street;
   95       }
   96   
   97       /**
   98        * Set the street component of the address.
   99        * @param street The street component.
  100        */
  101       public void setStreet(String street) {
  102           this.street = street;
  103       }
  104   
  105       /**
  106        * Get the city.
  107        * @return The city component of the address.
  108        */
  109       public String getCity() {
  110           return city;
  111       }
  112   
  113       /**
  114        * Set the city component of the address.
  115        * @param city The city.
  116        */
  117       public void setCity(String city) {
  118           this.city = city;
  119       }
  120       
  121       /**
  122        * Get the state component of the address.
  123        * @return The state.
  124        */
  125       public String getState() {
  126           return state;
  127       }
  128   
  129       /**
  130        * Set the state component of the address.
  131        * @param state The state.
  132        */
  133       public void setState(String state) {
  134           this.state = state;
  135       }
  136   
  137       /**
  138        * Get the zipcode component of the address.
  139        * @return The zipcode.
  140        */
  141       public String getZipcode() {
  142           return zipcode;
  143       }
  144   
  145       /**
  146        * Set the zip code component of the address.
  147        * @param zipcode The zipcode.
  148        */
  149       public void setZipcode(String zipcode) {
  150           this.zipcode = zipcode;
  151       }
  152   
  153       /**
  154        * Get the country component of the address.
  155        * @return The country.
  156        */
  157       public String getCountry() {
  158           return country;
  159       }
  160   
  161       /**
  162        * Set the country component of the address.
  163        * @param country The country.
  164        */
  165       public void setCountry(String country) {
  166           this.country = country;
  167       }
  168   
  169       /**
  170        * Returns a String representation of a <code>Address</code> object.
  171        * @return a String representation of a <code>Address</code> object.
  172        */
  173       public String toString() {
  174           return "Address(" + getFieldRepr() + ")";
  175       }
  176       
  177       /**
  178        * Returns a String representation of the non-relationship fields.
  179        * @return a String representation of the non-relationship fields.
  180        */
  181       protected String getFieldRepr() {
  182           StringBuffer rc = new StringBuffer();
  183           rc.append(addrid);
  184           rc.append(", street ").append(street);
  185           rc.append(", city ").append(city);
  186           rc.append(", state ").append(state);
  187           rc.append(", zipcode ").append(zipcode);
  188           rc.append(", country ").append(country);
  189           return rc.toString();
  190       }
  191   
  192       /** 
  193        * Returns <code>true</code> if all the fields of this instance are
  194        * deep equal to the coresponding fields of the specified Person.
  195        * @param other the object with which to compare.
  196        * @param helper EqualityHelper to keep track of instances that have
  197        * already been processed. 
  198        * @return <code>true</code> if all the fields are deep equal;
  199        * <code>false</code> otherwise.  
  200        * @throws ClassCastException if the specified instances' type prevents
  201        * it from being compared to this instance. 
  202        */
  203       public boolean deepCompareFields(Object other, 
  204                                        EqualityHelper helper) {
  205           JPAAppAddress otherAddress = (JPAAppAddress)other;
  206           String where = "Address<" + addrid + ">";
  207           return
  208               helper.equals(addrid, otherAddress.getAddrid(), where + ".addrid") &
  209               helper.equals(street, otherAddress.getStreet(), where + ".street") &
  210               helper.equals(city, otherAddress.getCity(), where + ".city") &
  211               helper.equals(state, otherAddress.getState(), where + ".state") &
  212               helper.equals(zipcode, otherAddress.getZipcode(), where + ".zipcode") &
  213               helper.equals(country, otherAddress.getCountry(), where + ".country");
  214       }
  215       
  216       /** 
  217        * Compares this object with the specified object for order. Returns a
  218        * negative integer, zero, or a positive integer as this object is less
  219        * than, equal to, or greater than the specified object. 
  220        * @param o The Object to be compared. 
  221        * @return a negative integer, zero, or a positive integer as this 
  222        * object is less than, equal to, or greater than the specified object. 
  223        * @throws ClassCastException - if the specified object's type prevents
  224        * it from being compared to this Object. 
  225        */
  226       public int compareTo(Object o) {
  227           return compareTo((JPAAppAddress)o);
  228       }
  229   
  230       /** 
  231        * Compare two instances. This is a method in Comparator.
  232        */
  233       public int compare(Object o1, Object o2) {
  234           return compare((JPAAppAddress)o1, (JPAAppAddress)o2);
  235       }
  236   
  237       /** 
  238        * Compares this object with the specified Address object for
  239        * order. Returns a negative integer, zero, or a positive integer as
  240        * this object is less than, equal to, or greater than the specified
  241        * object.  
  242        * @param other The Address object to be compared. 
  243        * @return a negative integer, zero, or a positive integer as this
  244        * object is less than, equal to, or greater than the specified Address
  245        * object. 
  246        */
  247       public int compareTo(JPAAppAddress other) {
  248           return compare(this, other);
  249       }
  250       
  251       /**
  252        * Compares its two JPAAppAddress arguments for order. Returns a negative
  253        * integer, zero, or a positive integer as the first argument is less
  254        * than, equal to, or greater than the second. 
  255        * 
  256        * 
  257        * 
  258        * @param o1 the first JPAAppAddresss object to be compared.
  259        * @param o2 the second JPAAppAddressobject to be compared.
  260        * @return a negative integer, zero, or a positive integer as the first
  261        * object is less than, equal to, or greater than the second object.
  262        */
  263       public static int compare(JPAAppAddress o1, JPAAppAddress o2) {
  264           return EqualityHelper.compare(o1.getAddrid(), o2.getAddrid());
  265       }
  266   
  267       /** 
  268        * Indicates whether some other object is "equal to" this one.
  269        * @param obj the object with which to compare.
  270        * @return <code>true</code> if this object is the same as the obj
  271        * argument; <code>false</code> otherwise. 
  272        */
  273       public boolean equals(Object obj) {
  274           if (obj instanceof JPAAppAddress) {
  275               return compareTo((JPAAppAddress)obj) == 0;
  276           }
  277           return false;
  278       }
  279   
  280       /**
  281        * Returns a hash code value for the object. 
  282        * @return a hash code value for this object.
  283        */
  284       public int hashCode() {
  285           return (int)addrid;
  286       }
  287       
  288       /**
  289        * This class is used to represent the application identifier 
  290        * for the <code>Address</code> class.
  291        */
  292       public static class Oid implements Serializable, Comparable {
  293   
  294           /**
  295            * This is the identifier field for <code>Address</code> and must
  296            * correspond in type and name to the field in
  297            * <code>Address</code>. 
  298            */
  299           public long addrid;
  300           
  301           /** The required public, no-arg constructor. */
  302           public Oid()
  303           {
  304               addrid = 0;
  305           }
  306   
  307           /**
  308            * A constructor to initialize the identifier field.
  309            * @param addrid the id of the Address.
  310            */
  311           public Oid(long addrid) {
  312               this.addrid = addrid;
  313           }
  314           
  315           public Oid(String s) { addrid = Long.parseLong(justTheId(s)); }
  316   
  317           public String toString() { return this.getClass().getName() + ": "  + addrid;}
  318   
  319   
  320           /** */
  321           public boolean equals(java.lang.Object obj) {
  322               if( obj==null || !this.getClass().equals(obj.getClass()) )
  323                   return( false );
  324               Oid o = (Oid) obj;
  325               if( this.addrid != o.addrid ) return( false );
  326               return( true );
  327           }
  328   
  329           /** */
  330           public int hashCode() {
  331               return( (int) addrid );
  332           }
  333           
  334           protected static String justTheId(String str) {
  335               return str.substring(str.indexOf(':') + 1);
  336           }
  337   
  338           /** */
  339           public int compareTo(Object obj) {
  340               // may throw ClassCastException which the user must handle
  341               Oid other = (Oid) obj;
  342               if( addrid < other.addrid ) return -1;
  343               if( addrid > other.addrid ) return 1;
  344               return 0;
  345           }
  346   
  347       }
  348   
  349   }

Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.pc.companyAnnotatedJPA » [javadoc | source]