/*
* Copyright 2013 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.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import br.com.objectos.way.base.Testables;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class SourceFileInfoPojo implements SourceFileInfo {
private final PackageInfo packageInfo;
private final ImportInfoMap importInfoMap;
private final TypeInfoMap typeInfoMap;
public SourceFileInfoPojo(Builder builder) {
packageInfo = builder.getPackageInfo();
importInfoMap = builder.getImportInfoMap();
List<TypeInfo> typeList = builder.getTypeInfoList();
typeInfoMap = TypeInfoMap.mapOf(typeList);
}
public static SourceFileInfo of(CompilationUnit unit) {
return new SourceFileAst(unit).build();
}
@Override
public boolean isEqual(SourceFileInfo o) {
SourceFileInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.packageInfo, that.packageInfo)
.equal(this.importInfoMap, that.importInfoMap)
.equal(this.typeInfoMap, that.typeInfoMap)
.result();
}
@Override
public SourceFileInfoPojo toPojo() {
return this;
}
private static class SourceFileAst implements SourceFileInfo.Builder {
private final CompilationUnit unit;
public SourceFileAst(CompilationUnit unit) {
this.unit = unit;
}
@Override
public SourceFileInfo build() {
return new SourceFileInfoPojo(this);
}
@Override
public PackageInfo getPackageInfo() {
PackageDeclaration ast = unit.getPackage();
return PackageInfoPojo.of(ast);
}
@SuppressWarnings("unchecked")
@Override
public ImportInfoMap getImportInfoMap() {
List<ImportDeclaration> importDeclarationList;
importDeclarationList = unit.imports();
List<ImportInfo> _metaImportList;
_metaImportList = transform(importDeclarationList, ImportInfo.FROM_IMPORT_DECLARATION);
List<ImportInfo> metaImportList;
metaImportList = ImmutableList.copyOf(_metaImportList);
return ImportInfoMap.mapOf(metaImportList);
}
@SuppressWarnings("unchecked")
@Override
public List<TypeInfo> getTypeInfoList() {
List<TypeDeclaration> typeDeclarationList;
typeDeclarationList = unit.types();
Function<TypeDeclaration, TypeInfo> toMetaType;
toMetaType = toMetaType();
List<TypeInfo> typeList;
typeList = transform(typeDeclarationList, toMetaType);
return ImmutableList.copyOf(typeList);
}
private Function<TypeDeclaration, TypeInfo> toMetaType() {
PackageDeclaration packageDeclaration;
packageDeclaration = unit.getPackage();
PackageInfo metaPackage;
metaPackage = PackageInfoPojo.of(packageDeclaration);
return new TypeDeclarationToMetaType(metaPackage);
}
}
}