*** 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 readonly string Name;
14       public readonly MTextProperty.Flags Flags;
15       public object Value;
16       public MPlist Plist;
17
18       public MSymbolData (string name, MTextProperty.Flags flags)
19       {
20         Name = name;
21         Flags = flags;
22       }
23     }
24
25     private MSymbolData data;
26
27     public static MSymbol nil = new MSymbol ("nil");
28     public static MSymbol t = new MSymbol ("t");
29     public static MSymbol symbol = new MSymbol ("symbol");
30     public static MSymbol mtext = new MSymbol ("mtext");
31     public static MSymbol plist = new MSymbol ("plist");
32     public static MSymbol integer = new MSymbol ("integer");
33
34     public MSymbol (string name)
35     {
36       if (! pool.ContainsKey (name))
37         {
38           data = new MSymbolData (name, MTextProperty.Flags.None);
39           pool.Add (name, data);
40         }
41       else
42         data = (MSymbolData) pool[name];
43     }
44
45     public MSymbol (string name, MTextProperty.Flags flags)
46     {
47       if (! pool.ContainsKey (name))
48         {
49           data = new MSymbolData (name, flags);
50           pool.Add (name, data);
51         }
52       else
53         {
54           if (((MSymbolData) pool[name]).Flags != flags)
55             throw new ArgumentException ("Invalid MTextProperty.Flags");
56         }
57     }
58
59     public MTextProperty.Flags TextPropertyFlags { get { return data.Flags; } }
60
61     public override string ToString ()
62     {
63       string str = "";
64
65       foreach (char c in data.Name)
66         {
67           if (c == '\\' || c == ' ' || c == '\'' || c == '\"' || c == ':')
68             str += "\\";
69           str += c;
70         }
71       return str;
72     }
73
74     public static bool operator== (MSymbol sym1, MSymbol sym2)
75     {
76       if (System.Object.ReferenceEquals(sym1, sym2))
77         return true;
78       if (((object) sym1 == null) || ((object) sym2 == null))
79         return false;
80       return sym1.data == sym2.data;
81     }
82
83     public static bool operator!= (MSymbol sym1, MSymbol sym2)
84     {
85       return ! (sym1 == sym2);
86     }
87
88     public override bool Equals (object sym)
89     {
90       if (sym == null)
91         return false;
92       return (this.data == ((MSymbol) sym).data);
93     }
94
95     public override int GetHashCode ()
96     {
97       return (data.Name.GetHashCode ());
98     }
99
100     public MPlist Find (MSymbol key)
101     {
102       return (data.Plist == null ? null : data.Plist.Find (key));
103     }
104
105     public object Get (MSymbol key)
106     {
107       return (data.Plist == null ? null : data.Plist.Get (key));
108     }
109
110     public object Put (MSymbol key, object val)
111     {
112       if (data.Plist == null)
113         data.Plist = new MPlist ();
114       return data.Plist.Put (key, val);
115     }
116   }
117 }