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.Maps;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.jet.lang.diagnostics.Diagnostic;
022    import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
023    import org.jetbrains.jet.util.slicedmap.WritableSlice;
024    
025    import java.util.Collection;
026    import java.util.Map;
027    
028    public class ObservableBindingTrace implements BindingTrace {
029        public interface RecordHandler<K, V> {
030    
031            void handleRecord(WritableSlice<K, V> slice, K key, V value);
032        }
033    
034        private final BindingTrace originalTrace;
035    
036        private Map<WritableSlice, RecordHandler> handlers = Maps.newHashMap();
037    
038        public ObservableBindingTrace(BindingTrace originalTrace) {
039            this.originalTrace = originalTrace;
040        }
041        @Override
042        public void report(@NotNull Diagnostic diagnostic) {
043            originalTrace.report(diagnostic);
044        }
045    
046        @NotNull
047        @Override
048        public BindingContext getBindingContext() {
049            return originalTrace.getBindingContext();
050        }
051    
052        @Override
053        public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
054            originalTrace.record(slice, key, value);
055            RecordHandler recordHandler = handlers.get(slice);
056            if (recordHandler != null) {
057                recordHandler.handleRecord(slice, key, value);
058            }
059        }
060    
061        @Override
062        public <K> void record(WritableSlice<K, Boolean> slice, K key) {
063            record(slice, key, true);
064        }
065    
066        @Override
067        public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
068            return originalTrace.get(slice, key);
069        }
070    
071        @Override
072        @NotNull
073        public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
074            return originalTrace.getKeys(slice);
075        }
076    
077        public <K, V> ObservableBindingTrace addHandler(@NotNull WritableSlice<K, V> slice, @NotNull RecordHandler<K, V> handler) {
078            handlers.put(slice, handler);
079            return this;
080        }
081        
082    }