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        private final static boolean TRACK_REWRITES = false;
034        private final static boolean TRACK_WITH_STACK_TRACES = true;
035    
036        private final MutableSlicedMap map;
037    
038        private final BindingContext bindingContext = new BindingContext() {
039    
040            @Override
041            public Collection<Diagnostic> getDiagnostics() {
042                return diagnostics;
043            }
044    
045            @Override
046            public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
047                return BindingTraceContext.this.get(slice, key);
048            }
049    
050            @NotNull
051            @Override
052            public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
053                return BindingTraceContext.this.getKeys(slice);
054            }
055    
056            @NotNull
057            @TestOnly
058            @Override
059            public <K, V> ImmutableMap<K, V> getSliceContents(@NotNull ReadOnlySlice<K, V> slice) {
060                return map.getSliceContents(slice);
061            }
062        };
063    
064        public BindingTraceContext() {
065            //noinspection ConstantConditions
066            this.map = TRACK_REWRITES ? new TrackingSlicedMap(TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create();
067        }
068    
069    
070        private BindingTraceContext(MutableSlicedMap map) {
071            this.map = map;
072        }
073    
074        @TestOnly
075        public static BindingTraceContext createTraceableBindingTrace() {
076            return new BindingTraceContext(new TrackingSlicedMap(TRACK_WITH_STACK_TRACES));
077        }
078    
079        @Override
080        public void report(@NotNull Diagnostic diagnostic) {
081            diagnostics.add(diagnostic);
082        }
083    
084        public void clearDiagnostics() {
085            diagnostics.clear();
086        }
087    
088        @Override
089        public BindingContext getBindingContext() {
090            return bindingContext;
091        }
092    
093        @Override
094        public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
095            map.put(slice, key, value);
096        }
097    
098        @Override
099        public <K> void record(WritableSlice<K, Boolean> slice, K key) {
100            record(slice, key, true);
101        }
102    
103        @Override
104        public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
105            return map.get(slice, key);
106        }
107    
108        @NotNull
109        @Override
110        public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
111            return map.getKeys(slice);
112        }
113    }