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
;
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
);
39 //noinspection NonPrivateFieldAccessedInSynchronizedContext
40 return staticEntries
.get(idx
);
44 idx
= entries
.index(text
);
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
);
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
{{