1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.lib.template_framework;
/**
* Represents a tokenized {@link Template} (after calling {@code asToken()}) ready for
* instantiation either as a {@link Token} inside another {@link Template} or as
* a {@link String} with {@link #render}.
*/
public sealed abstract class TemplateToken implements Token
permits TemplateToken.ZeroArgs,
TemplateToken.OneArg,
TemplateToken.TwoArgs,
TemplateToken.ThreeArgs
{
private TemplateToken() {}
/**
* Represents a tokenized zero-argument {@link Template} ready for instantiation
* either as a {@link Token} inside another {@link Template} or as a {@link String}
* with {@link #render}.
*/
static final class ZeroArgs extends TemplateToken implements Token {
private final Template.ZeroArgs zeroArgs;
ZeroArgs(Template.ZeroArgs zeroArgs) {
this.zeroArgs = zeroArgs;
}
@Override
public TemplateBody instantiate() {
return zeroArgs.instantiate();
}
@Override
public void visitArguments(ArgumentVisitor visitor) {}
}
/**
* Represents a tokenized one-argument {@link Template}, already filled with arguments, ready for
* instantiation either as a {@link Token} inside another {@link Template} or as a {@link String}
* with {@link #render}.
*
* @param <T1> The type of the (first) argument.
*/
static final class OneArg<T1> extends TemplateToken implements Token {
private final Template.OneArg<T1> oneArgs;
private final T1 arg1;
OneArg(Template.OneArg<T1> oneArgs, T1 arg1) {
this.oneArgs = oneArgs;
this.arg1 = arg1;
}
@Override
public TemplateBody instantiate() {
return oneArgs.instantiate(arg1);
}
@Override
public void visitArguments(ArgumentVisitor visitor) {
visitor.visit(oneArgs.arg1Name(), arg1);
}
}
/**
* Represents a tokenized two-argument {@link Template}, already filled with arguments, ready for
* instantiation either as a {@link Token} inside another {@link Template} or as a {@link String}
* with {@link #render}.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
*/
static final class TwoArgs<T1, T2> extends TemplateToken implements Token {
private final Template.TwoArgs<T1, T2> twoArgs;
private final T1 arg1;
private final T2 arg2;
TwoArgs(Template.TwoArgs<T1, T2> twoArgs, T1 arg1, T2 arg2) {
this.twoArgs = twoArgs;
this.arg1 = arg1;
this.arg2 = arg2;
}
@Override
public TemplateBody instantiate() {
return twoArgs.instantiate(arg1, arg2);
}
@Override
public void visitArguments(ArgumentVisitor visitor) {
visitor.visit(twoArgs.arg1Name(), arg1);
visitor.visit(twoArgs.arg2Name(), arg2);
}
}
/**
* Represents a tokenized three-argument {@link TemplateToken}, already filled with arguments, ready for
* instantiation either as a {@link Token} inside another {@link Template} or as a {@link String}
* with {@link #render}.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
* @param <T3> The type of the second argument.
*/
static final class ThreeArgs<T1, T2, T3> extends TemplateToken implements Token {
private final Template.ThreeArgs<T1, T2, T3> threeArgs;
private final T1 arg1;
private final T2 arg2;
private final T3 arg3;
ThreeArgs(Template.ThreeArgs<T1, T2, T3> threeArgs, T1 arg1, T2 arg2, T3 arg3) {
this.threeArgs = threeArgs;
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
}
@Override
public TemplateBody instantiate() {
return threeArgs.instantiate(arg1, arg2, arg3);
}
@Override
public void visitArguments(ArgumentVisitor visitor) {
visitor.visit(threeArgs.arg1Name(), arg1);
visitor.visit(threeArgs.arg2Name(), arg2);
visitor.visit(threeArgs.arg3Name(), arg3);
}
}
abstract TemplateBody instantiate();
@FunctionalInterface
interface ArgumentVisitor {
void visit(String name, Object value);
}
abstract void visitArguments(ArgumentVisitor visitor);
final String render() {
return Renderer.render(this);
}
final String render(float fuel) {
return Renderer.render(this, fuel);
}
}
|