
br.com.objectos.way.code.AnnotationInfo Maven / Gradle / Ivy
The newest version!
/*
* 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 java.util.List;
import java.util.Objects;
import java.util.Optional;
import br.com.objectos.way.testable.Equality;
import br.com.objectos.way.testable.Testable;
import br.com.objectos.way.testable.Tester;
/**
* @author [email protected] (Marcio Endo)
*/
public abstract class AnnotationInfo
implements
HasAnnotationInfoList,
HasNameInfo,
Testable {
@Override
public abstract PackageInfo packageInfo();
abstract AccessInfo accessInfo();
abstract String name();
abstract AnnotationValueInfoMap annotationValueInfoMap();
public abstract Optional enclosingSimpleTypeInfo();
@Override
public abstract List annotationInfoList();
AnnotationInfo() {
}
public Optional> annotationInfoArrayValue(String name) {
return annotationValueInfoMap().annotationInfoArrayValue(name);
}
public Optional annotationValueInfo(String name) {
return annotationValueInfoMap().get(name);
}
public List annotationValueInfoList() {
return annotationValueInfoMap().list();
}
public boolean booleanValue(String name, boolean defaultValue) {
Optional maybeValueInfo = annotationValueInfo(name);
return maybeValueInfo.isPresent()
? maybeValueInfo.get().booleanValue()
: defaultValue;
}
public Optional enclosingTypeInfo() {
return enclosingSimpleTypeInfo().flatMap(SimpleTypeInfo::typeInfo);
}
public Optional> enumConstantInfoArrayValue(String name) {
return annotationValueInfoMap().enumConstantInfoArrayValue(name);
}
public Optional enumConstantInfoValue(String name) {
return annotationValueInfo(name)
.map(AnnotationValueInfo::enumConstantInfoValue);
}
public > E enumValue(Class enumType, String name, E defaultValue) {
Objects.requireNonNull(enumType);
Objects.requireNonNull(name);
Objects.requireNonNull(defaultValue);
return enumConstantInfoValue(name).map(info -> info.getEnumValue(enumType)).orElse(defaultValue);
}
public int intValue(String name, int defaultValue) {
Optional maybeValueInfo = annotationValueInfo(name);
return maybeValueInfo.isPresent()
? maybeValueInfo.get().intValue()
: defaultValue;
}
@Override
public Equality isEqualTo(Object that) {
return Tester.of(AnnotationInfo.class)
.add("packageInfo", o -> o.packageInfo())
.add("accessInfo", o -> o.accessInfo())
.add("name", o -> o.name())
.add("annotationValueInfoMap", o -> o.annotationValueInfoMap())
.add("enclosingSimpleTypeInfo", o -> o.enclosingSimpleTypeInfo())
.add("annotationInfoList", o -> o.annotationInfoList())
.test(this, that);
}
@Override
public NameInfo nameInfo() {
return enclosingSimpleTypeInfo()
.map(SimpleTypeInfo::nameInfo)
.orElse(NameInfo.of())
.add(name());
}
@Override
public String simpleName() {
return name();
}
public abstract SimpleTypeInfo simpleTypeInfo();
public Optional> simpleTypeInfoArrayValue(String name) {
return annotationValueInfoMap().getTypeArray(name);
}
public Optional simpleTypeInfoValue(String name) {
return annotationValueInfo(name)
.map(AnnotationValueInfo::simpleTypeInfoValue);
}
public Optional stringValue(String name) {
return annotationValueInfo(name)
.map(AnnotationValueInfo::stringValue);
}
public String stringValue(String name, String defaultValue) {
Optional maybeValueInfo = annotationValueInfo(name);
return maybeValueInfo.isPresent()
? maybeValueInfo.get().stringValue()
: defaultValue;
}
@Override
public String toString() {
return "@" + name();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy