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 boolean isSafeCall;
038    
039        private final Collection<Collection<ResolutionCandidate<D>>> candidatesList = Lists.newArrayList();
040    
041        private List<ResolutionTask<D, F>> tasks = null;
042    
043        public ResolutionTaskHolder(@NotNull JetReferenceExpression reference,
044                @NotNull BasicCallResolutionContext basicCallResolutionContext,
045                @NotNull PriorityProvider<ResolutionCandidate<D>> priorityProvider
046        ) {
047            this.reference = reference;
048            this.basicCallResolutionContext = basicCallResolutionContext;
049            this.priorityProvider = priorityProvider;
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            if (!candidates.isEmpty()) {
062                candidatesList.add(setIsSafeCall(candidates));
063            }
064        }
065    
066        public void addCandidates(@NotNull List<Collection<ResolutionCandidate<D>>> candidatesList) {
067            for (Collection<ResolutionCandidate<D>> candidates : candidatesList) {
068                addCandidates(candidates);
069            }
070        }
071    
072        public List<ResolutionTask<D, F>> getTasks() {
073            if (tasks == null) {
074                tasks = Lists.newArrayList();
075    
076                for (int priority = priorityProvider.getMaxPriority(); priority >= 0; priority--) {
077                    final int finalPriority = priority;
078                    for (Collection<ResolutionCandidate<D>> candidates : candidatesList) {
079                        Collection<ResolutionCandidate<D>> filteredCandidates = Collections2.filter(candidates, new Predicate<ResolutionCandidate<D>>() {
080                            @Override
081                            public boolean apply(@Nullable ResolutionCandidate<D> input) {
082                                return finalPriority == priorityProvider.getPriority(input);
083                            }
084                        });
085                        if (!filteredCandidates.isEmpty()) {
086                            tasks.add(new ResolutionTask<D, F>(filteredCandidates, reference, basicCallResolutionContext));
087                        }
088                    }
089                }
090            }
091            return tasks;
092        }
093    
094        public interface PriorityProvider<D> {
095            int getPriority(D candidate);
096    
097            int getMaxPriority();
098        }
099    }