io.github.mmm.code.impl.java.expression.constant.JavaEnumConstant Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mmm-code-java-impl Show documentation
Show all versions of mmm-code-java-impl Show documentation
Implementation of mmm-code-api for Java.
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.code.impl.java.expression.constant;
/**
* Implementation of {@link JavaConstant} for an {@link Enum} value.
*
* @author Joerg Hohwiller (hohwille at users.sourceforge.net)
* @since 1.0.0
* @param type of the {@link Enum} {@link #getValue() value}.
*/
public class JavaEnumConstant> extends JavaConstant {
private final boolean unqualified;
private JavaEnumConstant(T value, boolean qualified) {
super(value);
this.unqualified = qualified;
}
@Override
public String getSourceCode() {
Class> enumClass = this.value.getClass();
String name;
if (this.unqualified) {
name = enumClass.getSimpleName();
} else {
name = enumClass.getName();
}
return name + "." + this.value.name();
}
@Override
public JavaConstant withValue(T newValue) {
return new JavaEnumConstant<>(newValue, this.unqualified);
}
/**
* @param type of the {@link Enum} {@link #getValue() value}.
* @param value the constant value.
* @param unqualified - {@code true} to use the {@link Class#getSimpleName() simple name}, {@code false} otherwise
* (for {@link Class#getName() qualified name}).
* @return the {@link JavaConstant} for the given {@code value}.
*/
public static > JavaEnumConstant of(T value, boolean unqualified) {
return new JavaEnumConstant<>(value, unqualified);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy