1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }