/*
* 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 org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
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.SimpleName;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import br.com.objectos.way.base.Testables;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class PackageInfoPojo implements PackageInfo {
private final String name;
public PackageInfoPojo(Builder builder) {
name = builder.getName();
}
@Override
public boolean isEqual(PackageInfo o) {
PackageInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.name, that.name)
.result();
}
@Override
public boolean isJavaLang() {
return "java.lang".equals(name);
}
@Override
public ImportInfo toImportInfo(String name) {
return new ImportInfoBuilder(name).build();
}
@Override
public PackageInfoPojo toPojo() {
return this;
}
@Override
public String toString() {
return name;
}
@Override
public TypeInfo toTypeInfo(TypeDeclaration type) {
return TypeDeclarationWrapper.wrapperOf(type)
.toTypeInfo(this);
}
@Override
public ImportDeclaration newImportDeclaration(AST ast, Optional<String> name) {
Name qualifier = ast.newName(this.name);
SimpleName theName = ast.newSimpleName(name.or("*"));
QualifiedName qualifiedName = ast.newQualifiedName(qualifier, theName);
ImportDeclaration declaration = ast.newImportDeclaration();
declaration.setName(qualifiedName);
return declaration;
}
@Override
public PackageDeclaration newPackageDeclaration(AST ast) {
PackageDeclaration declaration = ast.newPackageDeclaration();
Name theName = ast.newName(name);
declaration.setName(theName);
return declaration;
}
@Override
public void writeTo(CompilationUnit compilationUnit) {
AST ast = compilationUnit.getAST();
PackageDeclaration declaration = ast.newPackageDeclaration();
Name theName = ast.newName(name);
declaration.setName(theName);
compilationUnit.setPackage(declaration);
}
@Override
public final int hashCode() {
return Objects.hashCode(name);
}
@Override
public final boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof PackageInfoPojo) {
final PackageInfoPojo that = (PackageInfoPojo) obj;
return Objects.equal(this.name, that.name);
} else {
return false;
}
}
private class ImportInfoBuilder implements ImportInfo.Builder {
private final String name;
public ImportInfoBuilder(String name) {
this.name = name;
}
@Override
public ImportInfo build() {
return new ImportInfoPojo(this);
}
@Override
public PackageInfo getPackageInfo() {
return PackageInfoPojo.this;
}
@Override
public Optional<String> getName() {
return Optional.fromNullable(name);
}
@Override
public boolean isMetaStatic() {
return false;
}
}
}