home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / classes / com / ms / xml / util / Atom.java < prev    next >
Encoding:
Java Source  |  1997-11-03  |  2.2 KB  |  110 lines

  1. /*
  2.  * @(#)Atom.java 1.0 6/3/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. package com.ms.xml.util;
  9.  
  10. import java.lang.String;
  11. import java.util.Hashtable;
  12.  
  13. /**
  14.  * This is a general purpose object to allow efficient
  15.  * sharing of duplicate strings in the system.  It does this by
  16.  * creating a global HashTable of all Atoms that have been 
  17.  * constructed.
  18.  *
  19.  * @version 1.0, 6/3/97
  20.  */
  21. public class Atom
  22. {
  23.     /**
  24.      * The shared string
  25.      */
  26.     String s;
  27.     
  28.     /**
  29.      * Cached hash value for improved compare speed.
  30.      */    
  31.     int hash;
  32.     
  33.     /**
  34.      * Hash table for shared atoms.
  35.      */
  36.     static Hashtable atoms = new Hashtable(500);
  37.  
  38.     /**
  39.      * Creates Atom new object with the passed in collation key.
  40.      * This is private because all Atoms should be constructed
  41.      * via the static Atom.create() method.
  42.      */
  43.     Atom(String s, int h) 
  44.     {
  45.         this.s = s;
  46.         this.hash = h;
  47.     }
  48.  
  49.     /**
  50.      * private constructor
  51.      */
  52.     Atom()
  53.     {
  54.     }
  55.  
  56.     /**
  57.      * Create a Atom object for this string.
  58.      * Atoms are case sensitive - i.e. it assumes any case folding
  59.      * has already been done.
  60.      */    
  61.     public static Atom create(String s)
  62.     {
  63.         if (s == null) return null;
  64.  
  65.         Object o = atoms.get(s);
  66.         if (o == null)
  67.         {
  68.             Atom n = new Atom(s, s.hashCode());
  69.             atoms.put(s, n);
  70.             return n;
  71.         }
  72.         else
  73.         {
  74.             return (Atom)o;
  75.         }
  76.     }
  77.      
  78.     /**
  79.      * Return the hash code for the name.
  80.      * @return returns the hash code for the name.
  81.      */
  82.     public int hashCode()
  83.     {
  84.         return hash;
  85.     }
  86.  
  87.     /**
  88.      * Return the string represented by the Atom.
  89.      */
  90.     public String toString() 
  91.     {
  92.         return s;
  93.     }
  94.     
  95.     /**
  96.      * Return whether this Atom is equal to another given Atom.
  97.      */
  98.     public boolean equals(Object that) 
  99.     {
  100.         if (this == that) 
  101.         {
  102.             return true;
  103.         }
  104.         if (that == null || getClass() != that.getClass()) 
  105.         {
  106.             return false;
  107.         }
  108.         return s.equals(((Atom)that).s);
  109.     }
  110. }