home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / StringConstant.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  2.3 KB  |  112 lines

  1. /*
  2.  *
  3.  * @(#) StringConstant.java 1.4@(#)
  4.  *
  5.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  6.  * 
  7.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  8.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  9.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  10.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  11.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  12.  * THIS SOFTWARE OR ITS DERIVATIVES.
  13.  * 
  14.  */
  15.  
  16. /**
  17.  * <p>
  18.  * sunw.demo.classfile.StringConstant
  19.  * </p> 
  20.  *
  21.  * @version 1.0
  22.  * @author Laurence P. G. Cable
  23.  */
  24.  
  25.  
  26. package sunw.demo.classfile;
  27.  
  28. import java.io.DataOutputStream;
  29. import java.io.IOException;
  30.  
  31. import sunw.demo.classfile.ClassFile;
  32. import sunw.demo.classfile.ConstantPoolEntry;
  33. import sunw.demo.classfile.UTF8Constant;
  34.  
  35. /**
  36.  * <p> this class provides minimal support for CONSTANT_STRING CPE's <p>
  37.  */
  38.  
  39. final class StringConstant extends ConstantPoolEntry {
  40.  
  41.     private UTF8Constant string;
  42.  
  43.     /**
  44.      * <p> construct a CONSTANT_STRING CPE </p>
  45.      *
  46.      * @param str    the constant 
  47.      * @param cf    the class file
  48.      */
  49.  
  50.     StringConstant(String str, ClassFile cf) {
  51.         super(CONSTANT_STRING, cf);
  52.     
  53.         string = cf.addUTF8Constant(str);
  54.  
  55.         addToConstantPool();
  56.     }
  57.  
  58.     /**
  59.      * <p> construct a CONSTANT_STRING CPE </p>
  60.      *
  61.      * @param utf8    the utf8 constant 
  62.      * @param cf    the class file
  63.      */
  64.  
  65.     StringConstant(UTF8Constant utf8, ClassFile cf) {
  66.         super(CONSTANT_STRING, cf);
  67.  
  68.     string = utf8;
  69.  
  70.         addToConstantPool();
  71.     }
  72.  
  73.     /**
  74.      * <p> write the constant to the stream </p>
  75.      *
  76.      * @param dos the output stream
  77.      *
  78.      * @throws IOException
  79.      */
  80.  
  81.     void write(DataOutputStream dos) throws IOException {
  82.         dos.writeByte(getTag());
  83.         dos.writeShort(string.getConstantPoolIndex());
  84.     }
  85.  
  86.     /**
  87.      * @return the string constant
  88.      */
  89.  
  90.     String getString() { return string.getString(); }
  91.  
  92.     /**
  93.      * @return object equality
  94.      */
  95.  
  96.     public boolean equals(Object o) {
  97.         if (o instanceof StringConstant) {
  98.             return string.equals(((StringConstant)o).string);
  99.         } 
  100.  
  101.         return false;
  102.     }
  103.  
  104.  
  105.     /**
  106.      * @return a hashcode for the object.
  107.      */
  108.     public int hashCode() {
  109.     return string.hashCode();    
  110.     }
  111. }
  112.