/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* 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 br.com.objectos.way.code;
import static com.google.common.collect.Lists.transform;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import br.com.objectos.way.base.Testables;
import br.com.objectos.way.base.WayIterables;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class MethodInfoPojo implements MethodInfo {
private final String name;
private final AccessInfo accessInfo;
private final SimpleTypeInfo returnTypeInfo;
private final List<ParameterInfo> parameterInfoList;
public MethodInfoPojo(Builder builder) {
name = builder.getName();
accessInfo = builder.getAccessInfo();
returnTypeInfo = builder.getReturnTypeInfo();
parameterInfoList = builder.getParameterInfoList();
}
@Override
public boolean isEqual(MethodInfo o) {
MethodInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.name, that.name)
.equal(this.accessInfo, that.accessInfo)
.equal(this.returnTypeInfo, that.returnTypeInfo)
.allEqual(this.parameterInfoList, that.parameterInfoList)
.result();
}
@Override
public MethodInfo toAccessInfo(AccessInfo accessInfo) {
return new MethodInfoAccessInfo(accessInfo).build();
}
@Override
public Optional<GetterInfo> toGetterInfo() {
if (!parameterInfoList.isEmpty()) {
return Optional.absent();
}
if (!name.startsWith(getterPrefix())) {
return Optional.absent();
}
GetterInfo info = new GetterInfoBuilder().build();
return Optional.of(info);
}
@Override
public Set<ImportInfo> toImportInfoSet() {
List<Optional<ImportInfo>> optionalList = WayIterables.from(parameterInfoList)
.transform(ParameterInfo.TO_IMPORT_INFO)
.add(returnTypeInfo.toImportInfo())
.toImmutableList();
Iterable<ImportInfo> presentList;
presentList = Optional.presentInstances(optionalList);
return ImmutableSet.copyOf(presentList);
}
@SuppressWarnings("unchecked")
@Override
public MethodDeclaration toMethodDeclaration(AST ast) {
MethodDeclaration method = ast.newMethodDeclaration();
SimpleName methodName = ast.newSimpleName(name);
method.setName(methodName);
accessInfo.writeTo(method);
returnTypeInfo.writeTo(ast, method);
List<SingleVariableDeclaration> parameterDeclarationList;
parameterDeclarationList = transform(parameterInfoList, new ParameterInfoToSingleVariableDeclaration(ast));
method.parameters().addAll(parameterDeclarationList);
return method;
}
@Override
public MethodInvocation toMethodInvocation(AST ast, SimpleName variableName) {
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setExpression(variableName);
invocation.setName(ast.newSimpleName(name));
return invocation;
}
@Override
public MethodInfoPojo toPojo() {
return this;
}
SimpleTypeInfo getReturnTypeInfo() {
return returnTypeInfo;
}
private String getterPrefix() {
return returnTypeInfo.getGetterPrefix();
}
private class GetterInfoBuilder implements GetterInfo.Builder {
@Override
public GetterInfo build() {
return new GetterInfoPojo(this);
}
@Override
public MethodInfo getMethodInfo() {
return MethodInfoPojo.this;
}
@Override
public String getFieldName() {
String prefix = getterPrefix();
String field = name.replace(prefix, "");
return WayCode.lowerCaseFirstChar(field);
}
}
private class MethodInfoAccessInfo implements MethodInfo.Builder {
private final AccessInfo newAccessInfo;
public MethodInfoAccessInfo(AccessInfo newAccessInfo) {
this.newAccessInfo = newAccessInfo;
}
@Override
public MethodInfo build() {
return new MethodInfoPojo(this);
}
@Override
public String getName() {
return name;
}
@Override
public AccessInfo getAccessInfo() {
return newAccessInfo;
}
@Override
public SimpleTypeInfo getReturnTypeInfo() {
return returnTypeInfo;
}
@Override
public List<ParameterInfo> getParameterInfoList() {
return parameterInfoList;
}
}
}