View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.millscript.datatypes;
22  
23  import org.millscript.commons.util.EMap;
24  import org.millscript.commons.util.map.EHashMap;
25  
26  /**
27   * This class represents MillScripts Atom type, the equivalent of an interned
28   * String in Java.
29   */
30  public final class Atom {
31  
32      /**
33       * This atom's symbol, i.e. the atom.
34       */
35      private String symbol;
36  
37      /**
38       * A table of all the available atoms. This is used to ensure we return a
39       * unique atom for a given symbol. This table is a mapping from an atoms
40       * symbol to it's atom.
41       */
42      private static EMap< String, Atom > table = new EHashMap< String, Atom >();
43  
44      /**
45       * Constructs a new atom for the specified symbol.
46       *
47       * @param a the symbol for this atom
48       */
49      private Atom( final String a ) {
50          this.symbol = a;
51      }
52  
53      /**
54       * Returns the symbol for this atom.
55       *
56       * @return  a String containing the symbol for this atom
57       */
58      String getString() {
59          return symbol;
60      }
61  
62      /**
63       * Returns the unique atom for the specified symbol. If an atom for the
64       * specified symbol already exists, it will be returned, otherwise a new
65       * atom will be created.
66       *
67       * @param symbol    the symbol we want an atom for
68       * @return  an Atom for the specified symbol
69       */
70      public static Atom make( final String symbol ) {
71          final String sym = symbol.intern();
72          Atom x = table.get( sym );
73          if ( x == null ) {
74              x = new Atom( sym );
75              table.insert( sym, x );
76          }
77          return x;
78      }
79  
80      /**
81       * @see java.lang.Object#toString()
82       */
83      @Override
84      public String toString() {
85          return "`" + symbol + "`";
86      }
87  
88  }