/*
* 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 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 br.com.objectos.way.base.WayIterables;
import com.google.common.base.Function;
import com.google.common.base.Optional;
/**
* @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 Optional<InterfaceInfo> getInterfaceInfo() {
return typeInfoMap.getInterfaceInfo();
}
@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 pkg = unit.getPackage();
return PackageDeclarationWrapper.wrapperOf(pkg)
.toPackageInfo();
}
@SuppressWarnings("unchecked")
@Override
public ImportInfoMap getImportInfoMap() {
List<ImportDeclaration> imports = unit.imports();
return ImportDeclarationWrapper.wrapAll(imports)
.transform(ImportDeclarationWrapper.TO_IMPORT_INFO)
.toList()
.asMap(ImportInfoMap.MAP);
}
@SuppressWarnings("unchecked")
@Override
public List<TypeInfo> getTypeInfoList() {
return WayIterables.<TypeDeclaration> from(unit.types())
.transform(toMetaType())
.toImmutableList();
}
private Function<TypeDeclaration, TypeInfo> toMetaType() {
PackageDeclaration packageDeclaration;
packageDeclaration = unit.getPackage();
PackageInfo packageInfo;
packageInfo = PackageDeclarationWrapper.wrapperOf(packageDeclaration)
.toPackageInfo();
return new TypeDeclarationToMetaType(packageInfo);
}
}
}