/*
* 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.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import br.com.objectos.way.base.Testables;
import com.google.common.base.Optional;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class SimpleTypeInfoPojo implements SimpleTypeInfo {
final Optional<PackageInfo> packageInfo;
final String name;
public SimpleTypeInfoPojo(Builder builder) {
packageInfo = builder.getPackageInfo();
name = builder.getName();
}
@Override
public String getGetterPrefix() {
return "get";
}
@Override
public boolean isEqual(SimpleTypeInfo o) {
SimpleTypeInfoPojo that = o.toPojo();
return Testables.isEqualHelper()
.equal(this.getClass(), that.getClass())
.equal(this.packageInfo, that.packageInfo)
.equal(this.name, that.name)
.result();
}
@Override
public Optional<ImportInfo> toImportInfo() {
return packageInfo.transform(new PackageInfoToImportInfo(name));
}
@Override
public SimpleTypeInfoPojo toPojo() {
return this;
}
@Override
public String toString() {
String res = name;
if (packageInfo.isPresent()) {
PackageInfo pkg = packageInfo.get();
res = pkg.toImportInfo(name)
.toString();
}
return res;
}
@Override
public Type toType(AST ast) {
SimpleName typeName = ast.newSimpleName(name);
return ast.newSimpleType(typeName);
}
@Override
public SimpleName newNameSuffix(AST ast, String suffix) {
return ast.newSimpleName(name + suffix);
}
@Override
public void writeTo(AST ast, MethodDeclaration declaration) {
Type type = toType(ast);
declaration.setReturnType2(type);
}
}