/**
 * Copyright 2010 - 2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.exodus.util;

import jetbrains.exodus.core.dataStructures.ConcurrentObjectCache;
import org.jetbrains.annotations.NotNull;

public class StringInterner {

    private static final ConcurrentObjectCache<String, String> cache = new ConcurrentObjectCache<String, String>(5000);

    private StringInterner() {
    }

    @SuppressWarnings({"RedundantStringConstructorCall"})
    public static String intern(final String s) {
        if (s == null) return null;
        final String result = cache.tryKey(s);
        if (result != null) {
            return result;
        }
        final String copy = new String(s);
        cache.cacheObject(copy, copy);
        return copy;
    }

    public static String intern(@NotNull final StringBuilder builder, final int maxLen) {
        final String result = builder.toString();
        final String cached = cache.tryKey(result);
        if (cached != null) {
            return cached;
        }
        if (builder.length() <= maxLen) {
            cache.cacheObject(result, result);
        }
        return result;
    }
}

@@ -770,16 +770,148 @@
 rner %7B%0A%0A
+    private static final int NUMBER_OF_GENERATIONS = 2;%0A    private static final int INTERNER_SIZE = 3089 * NUMBER_OF_GENERATIONS;%0A%0A
     priv
@@ -1019,12 +1019,44 @@
 ng%3E(
-5000
+INTERNER_SIZE, NUMBER_OF_GENERATIONS
 );%0A%0A
@@ -1601,16 +1601,62 @@
 ring();%0A
+        if (builder.length() %3C= maxLen) %7B%0A    
         
@@ -1703,24 +1703,28 @@
 t);%0A        
+ 
+   
 if (cached !
@@ -1737,32 +1737,36 @@
 ) %7B%0A            
+    
 return cached;%0A 
@@ -1773,54 +1773,16 @@
     
-   %7D%0A
        
- if (builder.length() %3C= maxLen) %7B
+%7D
 %0A   

a piece of cake: interner has two generations