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.calls.tasks;
018
019 import com.google.common.base.Predicate;
020 import com.google.common.collect.Collections2;
021 import com.google.common.collect.Lists;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
025 import org.jetbrains.jet.lang.psi.JetPsiUtil;
026 import org.jetbrains.jet.lang.psi.JetReferenceExpression;
027 import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
028
029 import java.util.Collection;
030 import java.util.List;
031
032 public class ResolutionTaskHolder<D extends CallableDescriptor, F extends D> {
033 private final BasicCallResolutionContext basicCallResolutionContext;
034 private final PriorityProvider<ResolutionCandidate<D>> priorityProvider;
035 private final TracingStrategy tracing;
036 private final boolean isSafeCall;
037
038 private final Collection<Collection<ResolutionCandidate<D>>> candidatesList = Lists.newArrayList();
039
040 private List<ResolutionTask<D, F>> tasks = null;
041
042 public ResolutionTaskHolder(
043 @NotNull BasicCallResolutionContext basicCallResolutionContext,
044 @NotNull PriorityProvider<ResolutionCandidate<D>> priorityProvider,
045 @NotNull TracingStrategy tracing
046 ) {
047 this.basicCallResolutionContext = basicCallResolutionContext;
048 this.priorityProvider = priorityProvider;
049 this.tracing = tracing;
050 this.isSafeCall = JetPsiUtil.isSafeCall(basicCallResolutionContext.call);
051 }
052
053 public Collection<ResolutionCandidate<D>> setIsSafeCall(@NotNull Collection<ResolutionCandidate<D>> candidates) {
054 for (ResolutionCandidate<D> candidate : candidates) {
055 candidate.setSafeCall(isSafeCall);
056 }
057 return candidates;
058 }
059
060 public void addCandidates(@NotNull Collection<ResolutionCandidate<D>> candidates) {
061 assertNotFinished();
062 if (!candidates.isEmpty()) {
063 candidatesList.add(setIsSafeCall(candidates));
064 }
065 }
066
067 public void addCandidates(@NotNull List<Collection<ResolutionCandidate<D>>> candidatesList) {
068 assertNotFinished();
069 for (Collection<ResolutionCandidate<D>> candidates : candidatesList) {
070 addCandidates(candidates);
071 }
072 }
073
074 private void assertNotFinished() {
075 assert tasks == null : "Can't add candidates after the resulting tasks were computed.";
076 }
077
078 public List<ResolutionTask<D, F>> getTasks() {
079 if (tasks == null) {
080 tasks = Lists.newArrayList();
081
082 for (int priority = priorityProvider.getMaxPriority(); priority >= 0; priority--) {
083 final int finalPriority = priority;
084 for (Collection<ResolutionCandidate<D>> candidates : candidatesList) {
085 Collection<ResolutionCandidate<D>> filteredCandidates = Collections2.filter(
086 candidates, new Predicate<ResolutionCandidate<D>>() {
087 @Override
088 public boolean apply(@Nullable ResolutionCandidate<D> input) {
089 return finalPriority == priorityProvider.getPriority(input);
090 }
091 }
092 );
093 if (!filteredCandidates.isEmpty()) {
094 tasks.add(new ResolutionTask<D, F>(filteredCandidates, basicCallResolutionContext, tracing));
095 }
096 }
097 }
098 }
099 return tasks;
100 }
101
102 public interface PriorityProvider<D> {
103 int getPriority(D candidate);
104
105 int getMaxPriority();
106 }
107 }