001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.lang.resolve;
018
019import com.google.common.collect.ImmutableMap;
020import com.google.common.collect.Lists;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.TestOnly;
023import org.jetbrains.jet.lang.diagnostics.Diagnostic;
024import org.jetbrains.jet.util.slicedmap.*;
025
026import java.util.Collection;
027import java.util.List;
028
029public class BindingTraceContext implements BindingTrace {
030    private final List<Diagnostic> diagnostics = Lists.newArrayList();
031
032    // This flag is used for debugging of "Rewrite at slice..." exceptions
033    // NOTE: sometimes TrackingSlicedMap throws a ClassCastException (after you have fixed the rewrite).
034    // I gave up debugging it, because it still serves its purpose. Any suggestions on how to fix it are welcome. (abreslav)
035    private final static boolean TRACK_REWRITES = false;
036    @SuppressWarnings("ConstantConditions")
037    private final MutableSlicedMap map = TRACK_REWRITES ? new TrackingSlicedMap(SlicedMapImpl.create()) : SlicedMapImpl.create();
038
039    private final BindingContext bindingContext = new BindingContext() {
040
041        @Override
042        public Collection<Diagnostic> getDiagnostics() {
043            return diagnostics;
044        }
045
046        @Override
047        public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
048            return BindingTraceContext.this.get(slice, key);
049        }
050
051        @NotNull
052        @Override
053        public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
054            return BindingTraceContext.this.getKeys(slice);
055        }
056
057        @NotNull
058        @TestOnly
059        @Override
060        public <K, V> ImmutableMap<K, V> getSliceContents(@NotNull ReadOnlySlice<K, V> slice) {
061            return map.getSliceContents(slice);
062        }
063    };
064
065    @Override
066    public void report(@NotNull Diagnostic diagnostic) {
067        diagnostics.add(diagnostic);
068    }
069
070    public void clearDiagnostics() {
071        diagnostics.clear();
072    }
073
074    @Override
075    public BindingContext getBindingContext() {
076        return bindingContext;
077    }
078
079    @Override
080    public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
081        map.put(slice, key, value);
082    }
083
084    @Override
085    public <K> void record(WritableSlice<K, Boolean> slice, K key) {
086        record(slice, key, true);
087    }
088
089    @Override
090    public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
091        return map.get(slice, key);
092    }
093
094    @NotNull
095    @Override
096    public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
097        return map.getKeys(slice);
098    }
099}