Don't pass five parameters to LeafElement's constuctor to initialize single myText...
[fedora-idea.git] / lang-impl / src / com / intellij / psi / impl / source / CharTableImpl.java
blobd460c49dc22e0f495a3807087312a216f844a748
1 package com.intellij.psi.impl.source;
3 import com.intellij.util.CharTable;
4 import com.intellij.util.text.CharArrayUtil;
5 import com.intellij.util.text.CharSequenceHashingStrategy;
6 import gnu.trove.THashSet;
8 /**
9 * @author max
11 public class CharTableImpl implements CharTable {
12 private final static int INTERN_THRESHOLD = 40; // 40 or more characters long tokens won't be interned.
13 private final static CharSequenceHashingStrategy HASHER = new CharSequenceHashingStrategy();
14 private final static MyTHashSet staticEntries = new MyTHashSet();
15 private final MyTHashSet entries = new MyTHashSet();
17 public CharSequence intern(final CharSequence text) {
18 if (text.length() > INTERN_THRESHOLD) return createSequence(text);
19 int idx;
21 synchronized(staticEntries) {
22 idx = staticEntries.index(text);
23 if (idx >= 0) {
24 //noinspection NonPrivateFieldAccessedInSynchronizedContext
25 return staticEntries.get(idx);
29 synchronized(this) {
30 idx = entries.index(text);
31 if (idx >= 0) {
32 //noinspection NonPrivateFieldAccessedInSynchronizedContext
33 return entries.get(idx);
36 // We need to create separate string just to prevent referencing all character data when original is string or char sequence over string
37 final CharSequence entry = createSequence(text);
38 boolean added = entries.add(entry);
39 assert added;
41 return entry;
45 public CharSequence intern(CharSequence baseText, int startOffset, int endOffset) {
46 if (endOffset - startOffset == baseText.length()) return baseText.toString();
47 return intern(baseText.subSequence(startOffset, endOffset));
50 private static CharSequence createSequence(final CharSequence text) {
51 final char[] buf = new char[text.length()];
52 CharArrayUtil.getChars(text, buf, 0);
53 return StringFactory.createStringFromConstantArray(buf);
56 public static void staticIntern(final String text) {
57 synchronized(staticEntries) {
58 staticEntries.add(text);
62 private final static class MyTHashSet extends THashSet<CharSequence> {
63 public MyTHashSet() {
64 super(10, 0.9f, CharTableImpl.HASHER);
67 public int index(final CharSequence obj) {
68 return super.index(obj);
71 public CharSequence get(int index) {
72 return (CharSequence)_set[index];
76 static {
77 CharTableImpl.staticIntern("==" );
78 CharTableImpl.staticIntern("!=" );
79 CharTableImpl.staticIntern("||" );
80 CharTableImpl.staticIntern("++" );
81 CharTableImpl.staticIntern("--" );
83 CharTableImpl.staticIntern("<" );
84 CharTableImpl.staticIntern("<=" );
85 CharTableImpl.staticIntern("<<=" );
86 CharTableImpl.staticIntern("<<" );
87 CharTableImpl.staticIntern(">" );
88 CharTableImpl.staticIntern("&" );
89 CharTableImpl.staticIntern("&&" );
91 CharTableImpl.staticIntern("+=" );
92 CharTableImpl.staticIntern("-=" );
93 CharTableImpl.staticIntern("*=" );
94 CharTableImpl.staticIntern("/=" );
95 CharTableImpl.staticIntern("&=" );
96 CharTableImpl.staticIntern("|=" );
97 CharTableImpl.staticIntern("^=" );
98 CharTableImpl.staticIntern("%=" );
100 CharTableImpl.staticIntern("(" );
101 CharTableImpl.staticIntern(")" );
102 CharTableImpl.staticIntern("{" );
103 CharTableImpl.staticIntern("}" );
104 CharTableImpl.staticIntern("[" );
105 CharTableImpl.staticIntern("]" );
106 CharTableImpl.staticIntern(";" );
107 CharTableImpl.staticIntern("," );
108 CharTableImpl.staticIntern("..." );
109 CharTableImpl.staticIntern("." );
111 CharTableImpl.staticIntern("=" );
112 CharTableImpl.staticIntern("!" );
113 CharTableImpl.staticIntern("~" );
114 CharTableImpl.staticIntern("?" );
115 CharTableImpl.staticIntern(":" );
116 CharTableImpl.staticIntern("+" );
117 CharTableImpl.staticIntern("-" );
118 CharTableImpl.staticIntern("*" );
119 CharTableImpl.staticIntern("/" );
120 CharTableImpl.staticIntern("|" );
121 CharTableImpl.staticIntern("^" );
122 CharTableImpl.staticIntern("%" );
123 CharTableImpl.staticIntern("@" );
125 CharTableImpl.staticIntern(" " );
126 CharTableImpl.staticIntern("\n" );
127 CharTableImpl.staticIntern("\n " );
128 CharTableImpl.staticIntern("\n " );
129 CharTableImpl.staticIntern("\n " );
130 CharTableImpl.staticIntern("\n " );
131 CharTableImpl.staticIntern("\n " );
132 CharTableImpl.staticIntern("\n " );
133 CharTableImpl.staticIntern("\n " );
134 CharTableImpl.staticIntern("\n " );
136 CharTableImpl.staticIntern("<");
137 CharTableImpl.staticIntern(">");
138 CharTableImpl.staticIntern("</");
139 CharTableImpl.staticIntern("/>");
140 CharTableImpl.staticIntern("\"");
141 CharTableImpl.staticIntern("\'");
142 CharTableImpl.staticIntern("<![CDATA[");
143 CharTableImpl.staticIntern("]]>");
144 CharTableImpl.staticIntern("<!--");
145 CharTableImpl.staticIntern("-->");
146 CharTableImpl.staticIntern("<!DOCTYPE");
147 CharTableImpl.staticIntern("SYSTEM");
148 CharTableImpl.staticIntern("PUBLIC");
149 CharTableImpl.staticIntern("<?");
150 CharTableImpl.staticIntern("?>");
152 CharTableImpl.staticIntern("<%");
153 CharTableImpl.staticIntern("%>");
154 CharTableImpl.staticIntern("<%=");
155 CharTableImpl.staticIntern("<%@");
156 CharTableImpl.staticIntern("${");
157 CharTableImpl.staticIntern("");