/*
* Copyright 2013-2017 consulo.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package consulo.csharp.lang.psi.impl.light.builder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import consulo.annotations.RequiredReadAction;
import consulo.csharp.lang.psi.CSharpSimpleParameterInfo;
import consulo.csharp.lang.psi.impl.source.CSharpLikeMethodDeclarationImplUtil;
import consulo.dotnet.psi.DotNetGenericParameter;
import consulo.dotnet.psi.DotNetGenericParameterList;
import consulo.dotnet.psi.DotNetLikeMethodDeclaration;
import consulo.dotnet.psi.DotNetParameter;
import consulo.dotnet.psi.DotNetParameterList;
import consulo.dotnet.psi.DotNetQualifiedElement;
import consulo.dotnet.psi.DotNetType;
import consulo.dotnet.resolve.DotNetTypeRef;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.containers.ContainerUtil;
/**
* @author VISTALL
* @since 17.05.14
*/
@SuppressWarnings("unchecked")
public abstract class CSharpLightLikeMethodDeclarationBuilder<T extends CSharpLightLikeMethodDeclarationBuilder<T>> extends
CSharpLightNamedElementWiModifierListBuilder<T> implements DotNetLikeMethodDeclaration
{
private List<DotNetParameter> myParameters = Collections.emptyList();
private List<DotNetGenericParameter> myGenericParameters = Collections.emptyList();
private String myParentQName;
private DotNetTypeRef myReturnType;
public CSharpLightLikeMethodDeclarationBuilder(Project project)
{
super(project);
}
@RequiredReadAction
@NotNull
public CSharpSimpleParameterInfo[] getParameterInfos()
{
return CSharpLikeMethodDeclarationImplUtil.getParametersInfos(this);
}
@Override
public boolean isEquivalentTo(PsiElement another)
{
return CSharpLikeMethodDeclarationImplUtil.isEquivalentTo(this, another);
}
@Nullable
@Override
public DotNetType getReturnType()
{
return null;
}
@RequiredReadAction
@NotNull
@Override
public DotNetTypeRef getReturnTypeRef()
{
return myReturnType;
}
@Nullable
@Override
public PsiElement getCodeBlock()
{
return null;
}
@Nullable
@Override
public DotNetGenericParameterList getGenericParameterList()
{
return null;
}
@NotNull
@Override
public DotNetGenericParameter[] getGenericParameters()
{
return ContainerUtil.toArray(myGenericParameters, DotNetGenericParameter.ARRAY_FACTORY);
}
@Override
public int getGenericParametersCount()
{
return myGenericParameters.size();
}
@NotNull
@Override
public DotNetTypeRef[] getParameterTypeRefs()
{
DotNetParameter[] parameters = getParameters();
DotNetTypeRef[] typeRefs = new DotNetTypeRef[parameters.length];
for(int i = 0; i < parameters.length; i++)
{
DotNetParameter parameter = parameters[i];
typeRefs[i] = parameter.toTypeRef(false);
}
return typeRefs;
}
@Nullable
@Override
public DotNetParameterList getParameterList()
{
return null;
}
@NotNull
@Override
public DotNetParameter[] getParameters()
{
return ContainerUtil.toArray(myParameters, DotNetParameter.ARRAY_FACTORY);
}
@Nullable
@Override
public String getPresentableParentQName()
{
PsiElement parent = getParent();
if(parent instanceof DotNetQualifiedElement)
{
return ((DotNetQualifiedElement) parent).getPresentableQName();
}
return myParentQName;
}
@Nullable
@Override
public String getPresentableQName()
{
String parentQName = getPresentableParentQName();
if(StringUtil.isEmpty(parentQName))
{
return getName();
}
return parentQName + "." + getName();
}
@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement
place)
{
return CSharpLikeMethodDeclarationImplUtil.processDeclarations(this, processor, state, lastParent, place);
}
@NotNull
public T withReturnType(DotNetTypeRef type)
{
myReturnType = type;
return (T) this;
}
@NotNull
public T withParentQName(String parentQName)
{
myParentQName = parentQName;
return (T) this;
}
public T addGenericParameter(DotNetGenericParameter genericParameter)
{
if(myGenericParameters.isEmpty())
{
myGenericParameters = new ArrayList<DotNetGenericParameter>(2);
}
if(genericParameter instanceof CSharpLightGenericParameterBuilder)
{
((CSharpLightGenericParameterBuilder) genericParameter).setIndex(myGenericParameters.size());
}
myGenericParameters.add(genericParameter);
return (T) this;
}
@NotNull
public T addParameter(DotNetParameter parameter)
{
if(myParameters.isEmpty())
{
myParameters = new ArrayList<DotNetParameter>(5);
}
if(parameter instanceof CSharpLightElementBuilder)
{
((CSharpLightElementBuilder) parameter).withParent(this);
}
if(parameter instanceof CSharpLightParameterBuilder)
{
((CSharpLightParameterBuilder) parameter).setMethod(this);
}
myParameters.add(parameter);
return (T) this;
}
}