home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / ObjectStreamField.java < prev    next >
Text File  |  1997-08-30  |  3KB  |  102 lines

  1. /*
  2.  * @(#)ObjectStreamField.java    1.8 97/02/05
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  */
  21.  
  22. package java.io;
  23.  
  24. /**
  25.  * A description of a field in a class.
  26.  *
  27.  * @author  unascribed
  28.  * @version 1.8, 02/05/97
  29.  */
  30. class ObjectStreamField {
  31.     ObjectStreamField(String n, char t, int o, String ts)
  32.     {
  33.     //    System.out.println("new field, " + n + " " + t + " " + ts);
  34.     name = n;
  35.     type = t;
  36.     offset = o;
  37.     typeString = ts;
  38.     }
  39.  
  40.     /*
  41.      * Default constructor creates an empty field.
  42.      * Usually used just to get to the sort functions.
  43.      */ 
  44.     ObjectStreamField() {
  45.     }
  46.  
  47.     /**
  48.      * test if this field is a primitive or not.
  49.      */
  50.     boolean isPrimitive() {
  51.     return (type != '[' && type != 'L');
  52.     }
  53.  
  54.     /**
  55.      * Compare this with another ObjectStreamField.
  56.      * return -1 if this is smaller, 0 if equal, 1 if greater
  57.      * types that are primitives are "smaller" than objects.
  58.      * if equal, the names are compared.
  59.      */
  60.     int compare(ObjectStreamField other) {
  61.     boolean thisprim = (typeString == null);
  62.     boolean otherprim = (other.typeString == null);
  63.  
  64.     if (thisprim != otherprim) {
  65.         return (thisprim ? -1 : 1);
  66.     }
  67.     return name.compareTo(other.name);
  68.     }
  69.  
  70.     /**
  71.      * Compare the types of two class descriptors.
  72.      * The match if they have the same primitive types.
  73.      * or if they are both objects and the object types match.
  74.      */
  75.     boolean typeEquals(ObjectStreamField other) {
  76.     if (other == null || type != other.type)
  77.       return false;
  78.  
  79.     /* Return true if the primitive types matched */
  80.     if (typeString == null && other.typeString == null)
  81.         return true;
  82.  
  83.     /* compare the object types */
  84.     return typeString.equals(other.typeString);
  85.     }
  86.  
  87.     /**
  88.      * Return a string describing this field.
  89.      */
  90.     public String toString() {
  91.     if (typeString != null)
  92.         return typeString + " " + name + " @" + offset;
  93.     else
  94.         return type + " " + name + " @" + offset;
  95.     }
  96.  
  97.     String name;        // the name of the field
  98.     char type;            // type first byte of the type signature
  99.     int  offset;        // Offset into the object of the field
  100.     String typeString;        // iff object, typename
  101. }
  102.