001 /*
002 * Copyright 2010-2015 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.kotlin.resolve;
018
019 import com.google.common.collect.Maps;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.diagnostics.Diagnostic;
023 import org.jetbrains.kotlin.psi.JetExpression;
024 import org.jetbrains.kotlin.types.JetType;
025 import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice;
026 import org.jetbrains.kotlin.util.slicedMap.WritableSlice;
027
028 import java.util.Collection;
029 import java.util.Map;
030
031 public class ObservableBindingTrace implements BindingTrace {
032 public interface RecordHandler<K, V> {
033
034 void handleRecord(WritableSlice<K, V> slice, K key, V value);
035 }
036
037 private final BindingTrace originalTrace;
038
039 private Map<WritableSlice, RecordHandler> handlers = Maps.newHashMap();
040
041 public ObservableBindingTrace(BindingTrace originalTrace) {
042 this.originalTrace = originalTrace;
043 }
044 @Override
045 public void report(@NotNull Diagnostic diagnostic) {
046 originalTrace.report(diagnostic);
047 }
048
049 @NotNull
050 @Override
051 public BindingContext getBindingContext() {
052 return originalTrace.getBindingContext();
053 }
054
055 @Override
056 public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
057 originalTrace.record(slice, key, value);
058 RecordHandler recordHandler = handlers.get(slice);
059 if (recordHandler != null) {
060 recordHandler.handleRecord(slice, key, value);
061 }
062 }
063
064 @Override
065 public <K> void record(WritableSlice<K, Boolean> slice, K key) {
066 record(slice, key, true);
067 }
068
069 @Override
070 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
071 return originalTrace.get(slice, key);
072 }
073
074 @Override
075 @NotNull
076 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
077 return originalTrace.getKeys(slice);
078 }
079
080 @Nullable
081 @Override
082 public JetType getType(@NotNull JetExpression expression) {
083 return originalTrace.getType(expression);
084 }
085
086 @Override
087 public void recordType(@NotNull JetExpression expression, @Nullable JetType type) {
088 originalTrace.recordType(expression, type);
089 }
090
091 public <K, V> ObservableBindingTrace addHandler(@NotNull WritableSlice<K, V> slice, @NotNull RecordHandler<K, V> handler) {
092 handlers.put(slice, handler);
093 return this;
094 }
095
096 }