All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.jet.util.slicedmap.TrackingSlicedMap Maven / Gradle / Ivy

/*
 * Copyright 2010-2013 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 org.jetbrains.jet.util.slicedmap;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.utils.Printer;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class TrackingSlicedMap extends SlicedMapImpl {
    private final Map, SliceWithStackTrace> sliceTranslationMap = Maps.newHashMap();
    private final boolean trackWithStackTraces;

    public TrackingSlicedMap(boolean trackWithStackTraces) {
        super(Maps., Object>newLinkedHashMap());
        this.trackWithStackTraces = trackWithStackTraces;
    }

    private  SliceWithStackTrace wrapSlice(ReadOnlySlice slice) {
        SliceWithStackTrace translated = sliceTranslationMap.get(slice);
        if (translated == null) {
            translated = new SliceWithStackTrace(slice);
            sliceTranslationMap.put(slice, translated);
        }
        //noinspection unchecked
        return (SliceWithStackTrace) translated;
    }

    @Override
    public  V get(ReadOnlySlice slice, K key) {
        return super.get(wrapSlice(slice), key).value;
    }

    @Override
    public  Collection getKeys(WritableSlice slice) {
        return super.getKeys(wrapSlice(slice));
    }

    @NotNull
    @Override
    public Iterator, ?>> iterator() {
        Map, Object> map = Maps.newHashMap();

        Iterator, ?>> iterator = super.iterator();

        //noinspection WhileLoopReplaceableByForEach
        while (iterator.hasNext()) {
            Map.Entry, ?> entry = iterator.next();
            map.put(entry.getKey(), ((TrackableValue) entry.getValue()).value);
        }

        //noinspection unchecked
        return (Iterator) map.entrySet().iterator();
    }

    @Override
    public  void put(WritableSlice slice, K key, V value) {
        super.put(wrapSlice(slice), key, new TrackableValue(value, trackWithStackTraces));
    }

    @Override
    public  V remove(RemovableSlice slice, K key) {
        return super.remove(wrapSlice(slice), key).value;
    }

    @Override
    public void clear() {
        super.clear();
    }

    @Override
    @NotNull
    @TestOnly
    public  ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) {
        return super.getSliceContents(slice);
    }

    private static class TrackableValue {
        private final static StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];

        private final V value;
        private final StackTraceElement[] stackTrace;
        private final String threadName;

        private TrackableValue(V value, boolean storeStack) {
            this.value = value;
            this.stackTrace = storeStack ? Thread.currentThread().getStackTrace() : EMPTY_STACK_TRACE;
            this.threadName = Thread.currentThread().getName();
        }

        private Appendable printStackTrace(Appendable appendable) {
            Printer s = new Printer(appendable);
            s.println(value);
            s.println("Thread: " + threadName);
            s.println("Written at ");
            StackTraceElement[] trace = stackTrace;
            for (StackTraceElement aTrace : trace) {
                s.println("\tat " + aTrace);
            }
            s.println("---------");
            return appendable;
        }

        @Override
        public String toString() {
            return printStackTrace(new StringBuilder()).toString();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            TrackableValue other = (TrackableValue) o;

            if (value != null ? !value.equals(other.value) : other.value != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            return value != null ? value.hashCode() : 0;
        }
    }

    public class SliceWithStackTrace implements RemovableSlice> {

        private final ReadOnlySlice delegate;

        private SliceWithStackTrace(@NotNull ReadOnlySlice delegate) {
            this.delegate = delegate;
        }

        // Methods of ReadOnlySlice

        @Override
        public SlicedMapKey> makeKey(K key) {
            //noinspection unchecked
            return (SlicedMapKey) delegate.makeKey(key);
        }

        @Override
        public TrackableValue computeValue(SlicedMap map, K key, TrackableValue value, boolean valueNotFound) {
            return new TrackableValue(delegate.computeValue(map, key, value == null ? null : value.value, valueNotFound), trackWithStackTraces);
        }

        @Override
        public ReadOnlySlice> makeRawValueVersion() {
            return wrapSlice(delegate.makeRawValueVersion());
        }

        // Methods of WritableSlice

        private WritableSlice getWritableDelegate() {
            return (WritableSlice) delegate;
        }

        @Override
        public boolean isCollective() {
            return getWritableDelegate().isCollective();
        }

        @Override
        public RewritePolicy getRewritePolicy() {
            return getWritableDelegate().getRewritePolicy();
        }

        @Override
        public void afterPut(MutableSlicedMap map, K key, TrackableValue value) {
            getWritableDelegate().afterPut(map, key, value.value);
        }

        @Override
        public boolean check(K key, TrackableValue value) {
            return getWritableDelegate().check(key, value.value);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy