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