*** empty log message ***
[m17n/m17n-lib-cs.git] / MSymbol.cs
1 using System;
2 using System.Collections;
3 using M17N.Core;
4
5 namespace M17N.Core
6 {
7   public class MSymbol
8   {
9     static private Hashtable pool = new Hashtable ();
10
11     private class MSymbolData
12     {
13       public string name;
14       public object value;
15       public MPlist plist;
16
17       public MSymbolData (string name)
18       {
19         this.name = name;
20       }
21     }
22
23     private MSymbolData data;
24
25     public static MSymbol nil = new MSymbol ("nil");
26     public static MSymbol t = new MSymbol ("t");
27     public static MSymbol symbol = new MSymbol ("symbol");
28     public static MSymbol mtext = new MSymbol ("mtext");
29     public static MSymbol plist = new MSymbol ("plist");
30     public static MSymbol integer = new MSymbol ("integer");
31
32     public MSymbol (string name)
33     {
34       if (! pool.ContainsKey (name))
35         {
36           data = new MSymbolData (name);
37           pool.Add (name, data);
38         }
39       else
40         data = (MSymbolData) pool[name];
41     }
42
43     public override string ToString ()
44     {
45       string str = "";
46
47       foreach (char c in data.name)
48         {
49           if (c == '\\' || c == ' ' || c == '\'' || c == '\"' || c == ':')
50             str += "\\";
51           str += c;
52         }
53       return str;
54     }
55
56     public static bool operator== (MSymbol sym1, MSymbol sym2)
57     {
58       if (System.Object.ReferenceEquals(sym1, sym2))
59         return true;
60       if (((object) sym1 == null) || ((object) sym2 == null))
61         return false;
62       return sym1.data == sym2.data;
63     }
64
65     public static bool operator!= (MSymbol sym1, MSymbol sym2)
66     {
67       return ! (sym1 == sym2);
68     }
69
70     public override bool Equals (object sym)
71     {
72       if (sym == null)
73         return false;
74       return (this.data == ((MSymbol) sym).data);
75     }
76
77     public override int GetHashCode ()
78     {
79       return (data.name.GetHashCode ());
80     }
81
82     public MPlist Find (MSymbol key)
83     {
84       return (data.plist == null ? null : data.plist.Find (key));
85     }
86
87     public object Get (MSymbol key)
88     {
89       return (data.plist == null ? null : data.plist.Get (key));
90     }
91
92     public object Put (MSymbol key, object val)
93     {
94       if (data.plist == null)
95         data.plist = new MPlist ();
96       return data.plist.Put (key, val);
97     }
98   }
99 }