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        @Override
047        public BindingContext getBindingContext() {
048            return originalTrace.getBindingContext();
049        }
050    
051        @Override
052        public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
053            originalTrace.record(slice, key, value);
054            RecordHandler recordHandler = handlers.get(slice);
055            if (recordHandler != null) {
056                recordHandler.handleRecord(slice, key, value);
057            }
058        }
059    
060        @Override
061        public <K> void record(WritableSlice<K, Boolean> slice, K key) {
062            record(slice, key, true);
063        }
064    
065        @Override
066        public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
067            return originalTrace.get(slice, key);
068        }
069    
070        @Override
071        @NotNull
072        public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
073            return originalTrace.getKeys(slice);
074        }
075    
076        public <K, V> ObservableBindingTrace addHandler(@NotNull WritableSlice<K, V> slice, @NotNull RecordHandler<K, V> handler) {
077            handlers.put(slice, handler);
078            return this;
079        }
080        
081    }