/*
* 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 org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import br.com.objectos.way.base.Testables;
import com.google.common.collect.ImmutableList;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class PackageInfoPojo implements PackageInfo {
private final String name;
public PackageInfoPojo(Builder builder) {
name = builder.getName();
}
public static PackageInfo of(QualifiedName ast) {
return new MetaPackageName(ast).build();
}
public static PackageInfo of(PackageDeclaration ast) {
return new MetaPackagePackage(ast).build();
}
@Override
public boolean isEqual(PackageInfo o) {
PackageInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.name, that.name)
.result();
}
@Override
public PackageInfoPojo toPojo() {
return this;
}
@Override
public TypeInfo toTypeInfo(TypeDeclaration type) {
return new TypeInfoAst(type).build();
}
@Override
public PackageDeclaration newPackageDeclaration(AST ast) {
PackageDeclaration declaration = ast.newPackageDeclaration();
Name theName = ast.newName(name);
declaration.setName(theName);
return declaration;
}
private static class MetaPackageName extends MetaPackageAst {
private final QualifiedName name;
public MetaPackageName(QualifiedName name) {
this.name = name;
}
@Override
public String getName() {
return name.getQualifier()
.getFullyQualifiedName();
}
}
private static class MetaPackagePackage extends MetaPackageAst {
private final PackageDeclaration declaration;
public MetaPackagePackage(PackageDeclaration declaration) {
this.declaration = declaration;
}
@Override
public String getName() {
return declaration.getName()
.getFullyQualifiedName();
}
}
private static abstract class MetaPackageAst implements PackageInfo.Builder {
@Override
public PackageInfo build() {
return new PackageInfoPojo(this);
}
}
private class TypeInfoAst implements TypeInfo.Builder {
private final TypeDeclaration type;
public TypeInfoAst(TypeDeclaration type) {
this.type = type;
}
@Override
public TypeInfo build() {
if (type.isInterface()) {
return new InterfaceInfoPojo(this);
}
return new ClassInfoPojo(this);
}
@Override
public PackageInfo getPackageInfo() {
return PackageInfoPojo.this;
}
@Override
public String getName() {
return type.getName()
.getIdentifier();
}
@Override
public List<MethodInfo> getMethodInfoList() {
MethodDeclaration[] methods;
methods = type.getMethods();
List<MethodDeclaration> methodList;
methodList = ImmutableList.copyOf(methods);
List<MethodInfo> infoList;
infoList = transform(methodList, MethodInfo.FROM_METHOD_DECLARATION);
return ImmutableList.copyOf(infoList);
}
}
}