I have a enum with some links. In those links is the same version number (1.1.1
), for now as a String, which I would like to replace with constant. How to do this?
I could edit link while returning it (replace some string with my constant), but it does not seems like clean solution. Thank you for any help!
package test.intro;
import test.version;
public enum LinkType
{
LINK1("test/1.1.1/doc/test1.pdf"),
LINK2("test/1.1.1/doc/test2.pdf"),
LINK3("test/1.1.1/doc/test3.pdf");
public final String href;
//my constant I want to use:
private final String versionName = version.getVersionName();
private LinkType(String href)
{
this.href = href;
}
public String getHref()
{
return href;
}
}
CodePudding user response:
You could do something like this:
package test.intro;
import test.version;
public enum LinkType {
LINK1("test/%s/doc/test1.pdf"),
LINK2("test/%s/doc/test2.pdf"),
LINK3("test/%s/doc/test3.pdf");
public final String hrefTemplate;
private LinkType(String hrefTemplate) {
this.hrefTemplate = hrefTemplate;
}
public String getHrefTemplate() {
return this.hrefTemplate;
}
public String getHref() {
return String.format(this.hrefTemplate, version.getVersionName());
// or return this.hrefTemplate.formatted(version.getVersionName()); if you have Java >= 13
}
}
CodePudding user response:
I guess version.getVersionName()
is a static method?
Then you could just write LINK1("test/" version.getVersionName() "/doc/test1.pdf")
etc.