Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/amqp/Symbol.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.amqp;
23
24 import java.nio.ByteBuffer;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.apache.qpid.proton.codec.WritableBuffer;
29
30 public final class Symbol implements Comparable<Symbol>, CharSequence
31 {
32 private final String _underlying;
33 private final byte[] _underlyingBytes;
34
35 private static final ConcurrentHashMap<String, Symbol> _symbols = new ConcurrentHashMap<String, Symbol>(2048);
36
37 private Symbol(String underlying)
38 {
39 _underlying = underlying;
40 _underlyingBytes = underlying.getBytes(StandardCharsets.US_ASCII);
41 }
42
43 public int length()
44 {
45 return _underlying.length();
46 }
47
48 public int compareTo(Symbol o)
49 {
50 return _underlying.compareTo(o._underlying);
51 }
52
53 public char charAt(int index)
54 {
55 return _underlying.charAt(index);
56 }
57
58 public CharSequence subSequence(int beginIndex, int endIndex)
59 {
60 return _underlying.subSequence(beginIndex, endIndex);
61 }
62
63 @Override
64 public String toString()
65 {
66 return _underlying;
67 }
68
69 @Override
70 public int hashCode()
71 {
72 return _underlying.hashCode();
73 }
74
75 public static Symbol valueOf(String symbolVal)
76 {
77 return getSymbol(symbolVal);
78 }
79
80 public static Symbol getSymbol(String symbolVal)
81 {
82 if(symbolVal == null)
83 {
84 return null;
85 }
86 Symbol symbol = _symbols.get(symbolVal);
87 if(symbol == null)
88 {
89 symbolVal = symbolVal.intern();
90 symbol = new Symbol(symbolVal);
91 Symbol existing;
92 if((existing = _symbols.putIfAbsent(symbolVal, symbol)) != null)
93 {
94 symbol = existing;
95 }
96 }
97 return symbol;
98 }
99
100 public void writeTo(WritableBuffer buffer)
101 {
102 buffer.put(_underlyingBytes, 0, _underlyingBytes.length);
103 }
104
105 public void writeTo(ByteBuffer buffer)
106 {
107 buffer.put(_underlyingBytes, 0, _underlyingBytes.length);
108 }
109 }