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    
017    package org.jetbrains.jet.lang.resolve;
018    
019    import com.google.common.collect.ImmutableMap;
020    import com.google.common.collect.Lists;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.TestOnly;
023    import org.jetbrains.jet.lang.diagnostics.Diagnostic;
024    import org.jetbrains.jet.util.slicedmap.*;
025    
026    import java.util.Collection;
027    import java.util.List;
028    
029    public class BindingTraceContext implements BindingTrace {
030        private final List<Diagnostic> diagnostics = Lists.newArrayList();
031    
032        // These flags are used for debugging of "Rewrite at slice..." exceptions
033        /* package */ final static boolean TRACK_REWRITES = false;
034        /* package */ final static boolean TRACK_WITH_STACK_TRACES = true;
035    
036        private final MutableSlicedMap map;
037    
038        private final BindingContext bindingContext = new BindingContext() {
039    
040            @NotNull
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        public BindingTraceContext() {
066            //noinspection ConstantConditions
067            this.map = TRACK_REWRITES ? new TrackingSlicedMap(TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create();
068        }
069    
070    
071        private BindingTraceContext(MutableSlicedMap map) {
072            this.map = map;
073        }
074    
075        @TestOnly
076        public static BindingTraceContext createTraceableBindingTrace() {
077            return new BindingTraceContext(new TrackingSlicedMap(TRACK_WITH_STACK_TRACES));
078        }
079    
080        @Override
081        public void report(@NotNull Diagnostic diagnostic) {
082            diagnostics.add(diagnostic);
083        }
084    
085        public void clearDiagnostics() {
086            diagnostics.clear();
087        }
088    
089        @Override
090        public BindingContext getBindingContext() {
091            return bindingContext;
092        }
093    
094        @Override
095        public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
096            map.put(slice, key, value);
097        }
098    
099        @Override
100        public <K> void record(WritableSlice<K, Boolean> slice, K key) {
101            record(slice, key, true);
102        }
103    
104        @Override
105        public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
106            return map.get(slice, key);
107        }
108    
109        @NotNull
110        @Override
111        public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
112            return map.getKeys(slice);
113        }
114    }