update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / psi / impl / source / CharTableImpl.java
blob38de0a2b7a71fbbea952ecd4252230c46b788d86
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.psi.impl.source;
19 import com.intellij.util.CharTable;
20 import com.intellij.util.text.CharArrayCharSequence;
21 import com.intellij.util.text.CharArrayUtil;
22 import com.intellij.util.text.CharSequenceHashingStrategy;
23 import gnu.trove.THashSet;
25 /**
26 * @author max
28 public class CharTableImpl implements CharTable {
29 private static final int INTERN_THRESHOLD = 40; // 40 or more characters long tokens won't be interned.
30 private static final CharSequenceHashingStrategy HASHER = new CharSequenceHashingStrategy();
31 private static final MyTHashSet staticEntries = new MyStaticTHashSet();
32 private final MyTHashSet entries = new MyTHashSet();
34 public CharSequence intern(final CharSequence text) {
35 if (text.length() > INTERN_THRESHOLD) return createSequence(text);
37 int idx = staticEntries.index(text);
38 if (idx >= 0) {
39 //noinspection NonPrivateFieldAccessedInSynchronizedContext
40 return staticEntries.get(idx);
43 synchronized(this) {
44 idx = entries.index(text);
45 if (idx >= 0) {
46 //noinspection NonPrivateFieldAccessedInSynchronizedContext
47 return entries.get(idx);
50 // We need to create separate string just to prevent referencing all character data when original is string or char sequence over string
51 final CharSequence entry = createSequence(text);
52 boolean added = entries.add(entry);
53 assert added;
55 return entry;
59 public CharSequence intern(CharSequence baseText, int startOffset, int endOffset) {
60 if (endOffset - startOffset == baseText.length()) return baseText.toString();
61 return intern(baseText.subSequence(startOffset, endOffset));
64 private static CharSequence createSequence(final CharSequence text) {
65 final char[] buf = new char[text.length()];
66 CharArrayUtil.getChars(text, buf, 0);
67 return new CharArrayCharSequence(buf);
70 public static void staticIntern(final String text) {
71 synchronized(staticEntries) {
72 staticEntries.add(text);
76 private static class MyTHashSet extends THashSet<CharSequence> {
77 private MyTHashSet() {
78 super(10, 0.9f, HASHER);
81 public int index(final CharSequence obj) {
82 return super.index(obj);
85 public CharSequence get(int index) {
86 return (CharSequence)_set[index];
90 private static class MyStaticTHashSet extends MyTHashSet {{
91 add("==" );
92 add("!=" );
93 add("||" );
94 add("++" );
95 add("--" );
97 add("<" );
98 add("<=" );
99 add("<<=" );
100 add("<<" );
101 add(">" );
102 add("&" );
103 add("&&" );
105 add("+=" );
106 add("-=" );
107 add("*=" );
108 add("/=" );
109 add("&=" );
110 add("|=" );
111 add("^=" );
112 add("%=" );
114 add("(" );
115 add(")" );
116 add("{" );
117 add("}" );
118 add("[" );
119 add("]" );
120 add(";" );
121 add("," );
122 add("..." );
123 add("." );
125 add("=" );
126 add("!" );
127 add("~" );
128 add("?" );
129 add(":" );
130 add("+" );
131 add("-" );
132 add("*" );
133 add("/" );
134 add("|" );
135 add("^" );
136 add("%" );
137 add("@" );
139 add(" " );
140 add(" " );
141 add(" " );
142 add(" " );
143 add(" " );
144 add(" " );
145 add(" " );
146 add(" " );
147 add(" " );
148 add(" " );
149 add(" " );
150 add(" " );
151 add(" " );
152 add(" " );
153 add(" " );
154 add("\n" );
155 add("\n " );
156 add("\n " );
157 add("\n " );
158 add("\n " );
159 add("\n " );
160 add("\n " );
161 add("\n " );
162 add("\n " );
164 add("<");
165 add(">");
166 add("</");
167 add("/>");
168 add("\"");
169 add("\'");
170 add("<![CDATA[");
171 add("]]>");
172 add("<!--");
173 add("-->");
174 add("<!DOCTYPE");
175 add("SYSTEM");
176 add("PUBLIC");
177 add("<?");
178 add("?>");
180 add("<%");
181 add("%>");
182 add("<%=");
183 add("<%@");
184 add("${");
185 add("");