1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.proton.amqp.Symbol;
13 
14 import std.algorithm.comparison;
15 import hunt.io.ByteBuffer;
16 import hunt.collection.HashMap;
17 import hunt.proton.codec.WritableBuffer;
18 import hunt.collection.Map;
19 
20 class Symbol
21 {
22     private  string _underlying;
23     private  byte[] _underlyingBytes;
24 
25 
26      __gshared Map!(string, Symbol) _symbols ;
27 
28     shared static this()
29     {
30         _symbols = new HashMap!(string, Symbol);
31     }
32 
33     this (string underlying)
34     {
35         _underlying = underlying;
36         _underlyingBytes = cast(byte[])underlying;
37     }
38 
39     public string getUnderlying()
40     {
41         return  _underlying;
42     }
43 
44     public int length()
45     {
46         return cast(int)_underlying.length;
47     }
48 
49     override bool opEquals(Object o) {
50         return this._underlying == (cast(Symbol)o)._underlying;
51     }
52 
53     override int opCmp(Object o)
54     {
55 
56         return cmp(this._underlying,(cast(Symbol)o)._underlying);
57     }
58 
59 
60     public char charAt(int index)
61     {
62         return _underlying[index];
63     }
64 
65     public string subSequence(int beginIndex, int endIndex)
66     {
67         return _underlying[beginIndex .. endIndex];
68     }
69 
70     override
71     public string toString()
72     {
73         return _underlying;
74     }
75 
76     //override
77     public int hashCode()
78     {
79         return cast(int)(_underlying.hashOf());
80     }
81 
82     override
83     public  size_t toHash() @trusted nothrow
84     {
85         return  cast(int)(_underlying.hashOf());
86     }
87 
88     static Symbol valueOf(string symbolVal)
89     {
90         return getSymbol(symbolVal);
91     }
92 
93     static Symbol getSymbol(string symbolVal)
94     {
95         //if(symbolVal is null)
96         //{
97         //    return null;
98         //}
99 
100         if (_symbols is null)
101         {
102             _symbols = new HashMap!(string, Symbol);
103         }
104         Symbol symbol = null;
105       //  synchronized(this)
106         {
107             symbol = _symbols.get(symbolVal);
108             if(symbol is null)
109             {
110                 // symbolVal = symbolVal.intern();
111                 symbol = new Symbol(symbolVal);
112                 _symbols.put(symbolVal,symbol);
113             }
114         }
115         return symbol;
116     }
117 
118     public void writeTo(WritableBuffer buffer)
119     {
120         buffer.put(_underlyingBytes, 0, cast(int)_underlyingBytes.length);
121     }
122 
123     public void writeTo(ByteBuffer buffer)
124     {
125         buffer.put(_underlyingBytes, 0, cast(int)_underlyingBytes.length);
126     }
127 }