001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.serialization;
018    
019    import gnu.trove.TObjectHashingStrategy;
020    import gnu.trove.TObjectIntHashMap;
021    import kotlin.Function1;
022    import kotlin.KotlinPackage;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    
026    import java.util.List;
027    
028    public final class Interner<T> {
029        private final Interner<T> parent;
030        private final int firstIndex;
031        private final TObjectIntHashMap<T> interned;
032    
033        public Interner(Interner<T> parent, @NotNull TObjectHashingStrategy<T> hashing) {
034            this.parent = parent;
035            this.firstIndex = parent != null ? parent.interned.size() + parent.firstIndex : 0;
036            this.interned = new TObjectIntHashMap<T>(hashing);
037        }
038    
039        public Interner(@NotNull TObjectHashingStrategy<T> hashing) {
040            this(null, hashing);
041        }
042    
043        @SuppressWarnings("unchecked")
044        public Interner(@Nullable Interner<T> parent) {
045            this(parent, TObjectHashingStrategy.CANONICAL);
046        }
047    
048        public Interner() {
049            this((Interner<T>) null);
050        }
051    
052        private int find(@NotNull T obj) {
053            assert parent == null || parent.interned.size() + parent.firstIndex == firstIndex :
054                    "Parent changed in parallel with child: indexes will be wrong";
055            if (parent != null) {
056                int index = parent.find(obj);
057                if (index >= 0) return index;
058            }
059            if (interned.contains(obj)) {
060                return interned.get(obj);
061            }
062            return -1;
063        }
064    
065        public int intern(@NotNull T obj) {
066            int index = find(obj);
067            if (index >= 0) return index;
068    
069            index = firstIndex + interned.size();
070            interned.put(obj, index);
071            return index;
072        }
073    
074        @SuppressWarnings("unchecked")
075        @NotNull
076        public List<T> getAllInternedObjects() {
077            return KotlinPackage.toSortedListBy((T[]) interned.keys(), new Function1<T, Integer>() {
078                @Override
079                public Integer invoke(T key) {
080                    return interned.get(key);
081                }
082            });
083        }
084    }