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.collect.Lists;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
023    import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
024    
025    import java.util.Collection;
026    import java.util.List;
027    
028    import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
029    
030    public class ResolutionCandidate<D extends CallableDescriptor> {
031        private final D candidateDescriptor;
032        private ReceiverValue thisObject; // receiver object of a method
033        private ReceiverValue receiverArgument; // receiver of an extension function
034        private ExplicitReceiverKind explicitReceiverKind;
035        private Boolean isSafeCall;
036    
037        private ResolutionCandidate(@NotNull D descriptor, @NotNull ReceiverValue thisObject, @NotNull ReceiverValue receiverArgument,
038                @NotNull ExplicitReceiverKind explicitReceiverKind, @Nullable Boolean isSafeCall) {
039            this.candidateDescriptor = descriptor;
040            this.thisObject = thisObject;
041            this.receiverArgument = receiverArgument;
042            this.explicitReceiverKind = explicitReceiverKind;
043            this.isSafeCall = isSafeCall;
044        }
045    
046        /*package*/ static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor) {
047            return new ResolutionCandidate<D>(descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, null);
048        }
049    
050        /* 'null' for isSafeCall parameter if it should be set later (with 'setSafeCall') */
051        public static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor, @Nullable Boolean isSafeCall) {
052            return new ResolutionCandidate<D>(descriptor, NO_RECEIVER, NO_RECEIVER, ExplicitReceiverKind.NO_EXPLICIT_RECEIVER, isSafeCall);
053        }
054    
055        public static <D extends CallableDescriptor> ResolutionCandidate<D> create(@NotNull D descriptor, @NotNull ReceiverValue thisObject,
056                @NotNull ReceiverValue receiverArgument, @NotNull ExplicitReceiverKind explicitReceiverKind, boolean isSafeCall) {
057            return new ResolutionCandidate<D>(descriptor, thisObject, receiverArgument, explicitReceiverKind, isSafeCall);
058        }
059    
060        public void setThisObject(@NotNull ReceiverValue thisObject) {
061            this.thisObject = thisObject;
062        }
063    
064        public void setReceiverArgument(@NotNull ReceiverValue receiverArgument) {
065            this.receiverArgument = receiverArgument;
066        }
067    
068        public void setExplicitReceiverKind(@NotNull ExplicitReceiverKind explicitReceiverKind) {
069            this.explicitReceiverKind = explicitReceiverKind;
070        }
071    
072        @NotNull
073        public D getDescriptor() {
074            return candidateDescriptor;
075        }
076    
077        @NotNull
078        public ReceiverValue getThisObject() {
079            return thisObject;
080        }
081    
082        @NotNull
083        public ReceiverValue getReceiverArgument() {
084            return receiverArgument;
085        }
086    
087        @NotNull
088        public ExplicitReceiverKind getExplicitReceiverKind() {
089            return explicitReceiverKind;
090        }
091    
092        @NotNull
093        public static <D extends CallableDescriptor> List<ResolutionCandidate<D>> convertCollection(@NotNull Collection<? extends D> descriptors, boolean isSafeCall) {
094            List<ResolutionCandidate<D>> result = Lists.newArrayList();
095            for (D descriptor : descriptors) {
096                result.add(create(descriptor, isSafeCall));
097            }
098            return result;
099        }
100    
101        public void setSafeCall(boolean safeCall) {
102            assert isSafeCall == null;
103            isSafeCall = safeCall;
104        }
105    
106        public boolean isSafeCall() {
107            assert isSafeCall != null;
108            return isSafeCall;
109        }
110    
111        @Override
112        public String toString() {
113            return candidateDescriptor.toString();
114        }
115    }