Skip to content

Commit

Permalink
Merge pull request #1335 from scireum/feature/sbi/SequencedCollections
Browse files Browse the repository at this point in the history
Simplifies code by utilizing sequenced collection helpers
  • Loading branch information
sabieber authored Dec 5, 2023
2 parents e3d70c7 + ade0a4c commit dda73a3
Show file tree
Hide file tree
Showing 40 changed files with 100 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/main/java/sirius/pasta/noodle/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,6 @@ private Object pop() {
if (stack.isEmpty()) {
throw createVmError("Stack underflow");
}
return stack.remove(stack.size() - 1);
return stack.removeLast();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public VariableScoper getVariableScoper() {
public void error(Position pos, String message, Object... params) {
if (!Sirius.isStartedAsTest()
&& !errors.isEmpty()
&& errors.get(errors.size() - 1).getPosition().getLine() == pos.getLine()) {
&& errors.getLast().getPosition().getLine() == pos.getLine()) {
return;
}
if (skipErrors) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/sirius/pasta/noodle/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1005,19 +1005,19 @@ private Method findSAM(Position position, Class<?> calleeType) {
private Node handleSpecialMethods(Char position, Node self, String methodName, List<Node> parameters) {
if (KEYWORD_METHOD_AS.equals(methodName)
&& parameters.size() == 1
&& parameters.get(0).isConstant()
&& (Class.class.isAssignableFrom(parameters.get(0).getType()))) {
Class<?> type = (Class<?>) parameters.get(0).getConstantValue();
&& parameters.getFirst().isConstant()
&& (Class.class.isAssignableFrom(parameters.getFirst().getType()))) {
Class<?> type = (Class<?>) parameters.getFirst().getConstantValue();
if (!Transformable.class.isAssignableFrom(self.getType())) {
return new NativeCast(position, self, type);
}
}

if (KEYWORD_METHOD_IS.equals(methodName)
&& parameters.size() == 1
&& parameters.get(0).isConstant()
&& (Class.class.isAssignableFrom(parameters.get(0).getType()))) {
Class<?> type = (Class<?>) parameters.get(0).getConstantValue();
&& parameters.getFirst().isConstant()
&& (Class.class.isAssignableFrom(parameters.getFirst().getType()))) {
Class<?> type = (Class<?>) parameters.getFirst().getConstantValue();
if (!Transformable.class.isAssignableFrom(self.getType())) {
return new InstanceOfCheck(position, self, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected Scope() {
*/
public void pop() {
while (variables.size() > maxIndex) {
variables.remove(variables.size() - 1);
variables.removeLast();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BlockStatement(Position position) {
public void addStatement(Node statement, CompilationContext compilationContext) {
if (returnType != null) {
// Only report the first statement as dead code and skip all others...
if (statements.get(statements.size() - 1) instanceof ReturnStatement) {
if (statements.getLast() instanceof ReturnStatement) {
compilationContext.error(position, "Dead code: Nothing after a return statement will be executed.");
}

Expand All @@ -68,7 +68,7 @@ public Node reduce(CompilationContext compilationContext) {
statements.stream().map(statement -> statement.reduce(compilationContext)).collect(Collectors.toList());

if (statements.size() == 1) {
return statements.get(0);
return statements.getFirst();
}

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/pasta/noodle/macros/ApplyMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.isEmpty() || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.isEmpty() || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected at least a String as first argument.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Class<?> getType() {

@Override
protected void verifyArguments(CompilationContext compilationContext, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand All @@ -52,8 +52,8 @@ protected void verifyArguments(CompilationContext compilationContext, Position p
public void verify(CompilationContext context, Position position, List<Node> args) {
super.verify(context, position, args);

if (args.get(0).isConstant()) {
String resourceName = String.valueOf(args.get(0).getConstantValue());
if (args.getFirst().isConstant()) {
String resourceName = String.valueOf(args.getFirst().getConstantValue());
if (resources.resolve(resourceName).isEmpty()) {
context.warning(position, "Unknown resource: %s", resourceName);
}
Expand All @@ -62,7 +62,7 @@ public void verify(CompilationContext context, Position position, List<Node> arg

@Override
public boolean isConstant(CompilationContext context, List<Node> args) {
return args.get(0).isConstant();
return args.getFirst().isConstant();
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sirius/pasta/noodle/macros/ConfigMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ protected Class<?> getType() {

@Override
public void verify(CompilationContext context, Position position, List<Node> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0).getType(), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst().getType(), String.class)) {
throw new IllegalArgumentException("Expects a single string as argument.");
}

if (args.get(0).isConstant()) {
String configKey = (String) args.get(0).getConstantValue();
if (args.getFirst().isConstant()) {
String configKey = (String) args.getFirst().getConstantValue();
if (!Sirius.getSettings().has(configKey)) {
context.warning(position, "Unknown config value: '%s'", configKey);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sirius/pasta/noodle/macros/EnumValuesMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public Class<?> getType() {
@Override
public void verify(CompilationContext context, Position position, List<Node> args) {
if (args.size() != 1
|| !CompilationContext.isAssignableTo(args.get(0).getType(), Class.class)
|| !args.get(0)
|| !CompilationContext.isAssignableTo(args.getFirst().getType(), Class.class)
|| !args.getFirst()
.isConstant()
|| !((Class<?>) args.get(0).getConstantValue()).isEnum()) {
|| !((Class<?>) args.getFirst().getConstantValue()).isEnum()) {
throw new IllegalArgumentException("Expected an enum class as parameter.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected Class<?> getType() {

@Override
protected void verifyArguments(CompilationContext compilationContext, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sirius/pasta/noodle/macros/FormatDateMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public void verifyArguments(CompilationContext context, Position position, List<
throw new IllegalArgumentException("One parameter is expected");
}

if (!CompilationContext.isAssignableTo(args.get(0), Long.class)
&& !CompilationContext.isAssignableTo(args.get(0), Date.class)
&& !CompilationContext.isAssignableTo(args.get(0), TemporalAccessor.class)) {
if (!CompilationContext.isAssignableTo(args.getFirst(), Long.class)
&& !CompilationContext.isAssignableTo(args.getFirst(), Date.class)
&& !CompilationContext.isAssignableTo(args.getFirst(), TemporalAccessor.class)) {
throw new IllegalArgumentException("Illegal parameter type");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), Temporal.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), Temporal.class)) {
throw new IllegalArgumentException("One parameter of type Temporal is expected.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/pasta/noodle/macros/FormatMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 && !CompilationContext.isAssignableTo(args.get(0), Long.class)) {
if (args.size() != 1 && !CompilationContext.isAssignableTo(args.getFirst(), Long.class)) {
throw new IllegalArgumentException("One parameter is expected");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sirius/pasta/noodle/macros/HelperMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class HelperMacro extends BasicMacro {

@Override
public Class<?> getType(List<Node> args) {
if (!args.isEmpty() && args.get(0).isConstant() && Class.class.isAssignableFrom(args.get(0).getType())) {
return (Class<?>) args.get(0).getConstantValue();
if (!args.isEmpty() && args.getFirst().isConstant() && Class.class.isAssignableFrom(args.getFirst().getType())) {
return (Class<?>) args.getFirst().getConstantValue();
}

return super.getType(args);
Expand All @@ -47,7 +47,7 @@ protected Class<?> getType() {

@Override
public void verify(CompilationContext context, Position position, List<Node> args) {
if (args.size() != 1 || !args.get(0).isConstant() || !Class.class.isAssignableFrom(args.get(0).getType())) {
if (args.size() != 1 || !args.getFirst().isConstant() || !Class.class.isAssignableFrom(args.getFirst().getType())) {
throw new IllegalArgumentException("Expected a single constant class as argument.");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sirius/pasta/noodle/macros/I18nMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Object invoke(Environment environment, Object[] args) {
@Override
public boolean isConstant(CompilationContext context, List<Node> args) {
// An i18n macro is inherently not constant unless it is invoked for an empty string
return (args.size() == 1 || args.size() == 2) && args.get(0).isConstant() && Strings.isEmpty(args.get(0)
.getConstantValue());
return (args.size() == 1 || args.size() == 2) && args.getFirst().isConstant() && Strings.isEmpty(args.getFirst()
.getConstantValue());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand All @@ -67,19 +67,19 @@ public void verifyArguments(CompilationContext context, Position position, List<
public void verify(CompilationContext context, Position position, List<Node> args) {
super.verify(context, position, args);

if (args.get(0).isConstant()) {
String resourceName = String.valueOf(args.get(0).getConstantValue());
if (args.getFirst().isConstant()) {
String resourceName = String.valueOf(args.getFirst().getConstantValue());
if (resources.resolve(resourceName).isEmpty()) {
context.warning(position, "Unknown resource: %s", resourceName);
}
}

if (context.getSandboxMode() != SandboxMode.DISABLED) {
if (!args.get(0).isConstant()) {
if (!args.getFirst().isConstant()) {
raiseSandboxRestriction(context,
position,
"Only constant resource paths are supported due to sandbox restrictions.");
} else if (String.valueOf(args.get(0).getConstantValue()).endsWith(PASTA_SUFFIX)) {
} else if (String.valueOf(args.getFirst().getConstantValue()).endsWith(PASTA_SUFFIX)) {
raiseSandboxRestriction(context,
position,
"Only constant resources are supported due to sandbox restrictions.");
Expand Down Expand Up @@ -134,10 +134,10 @@ public String getDescription() {

@Override
public boolean isConstant(CompilationContext context, List<Node> args) {
if (!args.get(0).isConstant()) {
if (!args.getFirst().isConstant()) {
return false;
}

return !String.valueOf(args.get(0).getConstantValue()).endsWith(PASTA_SUFFIX);
return !String.valueOf(args.getFirst().getConstantValue()).endsWith(PASTA_SUFFIX);
}
}
8 changes: 4 additions & 4 deletions src/main/java/sirius/pasta/noodle/macros/InlineSvgMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected Class<?> getType() {

@Override
protected void verifyArguments(CompilationContext compilationContext, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand All @@ -47,8 +47,8 @@ protected void verifyArguments(CompilationContext compilationContext, Position p
public void verify(CompilationContext context, Position position, List<Node> args) {
super.verify(context, position, args);

if (args.get(0).isConstant()) {
String resourceName = String.valueOf(args.get(0).getConstantValue());
if (args.getFirst().isConstant()) {
String resourceName = String.valueOf(args.getFirst().getConstantValue());
if (resources.resolve(resourceName).isEmpty()) {
context.warning(position, "Unknown resource: %s", resourceName);
}
Expand All @@ -57,7 +57,7 @@ public void verify(CompilationContext context, Position position, List<Node> arg

@Override
public boolean isConstant(CompilationContext context, List<Node> args) {
return args.get(0).isConstant();
return args.getFirst().isConstant();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sirius/pasta/noodle/macros/JsonMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Class<?> getType() {

@Override
public void verifyArguments(CompilationContext context, Position position, List<Class<?>> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0), int.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst(), int.class)) {
throw new IllegalArgumentException("Expected exactly one argument as an int.");
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/sirius/pasta/noodle/macros/PartMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class PartMacro extends BasicMacro {

@Override
public Class<?> getType(List<Node> args) {
if (!args.isEmpty() && args.get(0).isConstant() && Class.class.isAssignableFrom(args.get(0).getType())) {
return (Class<?>) args.get(0).getConstantValue();
if (!args.isEmpty() && args.getFirst().isConstant() && Class.class.isAssignableFrom(args.getFirst().getType())) {
return (Class<?>) args.getFirst().getConstantValue();
}

return super.getType(args);
Expand All @@ -46,14 +46,14 @@ protected Class<?> getType() {

@Override
public void verify(CompilationContext context, Position position, List<Node> args) {
if (args.size() != 1 || !args.get(0).isConstant() || !Class.class.isAssignableFrom(args.get(0).getType())) {
if (args.size() != 1 || !args.getFirst().isConstant() || !Class.class.isAssignableFrom(args.getFirst().getType())) {
throw new IllegalArgumentException("Expected a single constant class as argument.");
}

if (Injector.context().getPart((Class<?>) args.get(0).getConstantValue()) == null) {
if (Injector.context().getPart((Class<?>) args.getFirst().getConstantValue()) == null) {
throw new IllegalArgumentException(Strings.apply(
"Unknown part requested: '%s'. Use 'Injector.context().getPart(Class) to fetch an optional part.",
args.get(0).getConstantValue()));
args.getFirst().getConstantValue()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public Class<?> getType() {

@Override
public void verify(CompilationContext context, Position position, List<Node> args) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.get(0).getType(), String.class)) {
if (args.size() != 1 || !CompilationContext.isAssignableTo(args.getFirst().getType(), String.class)) {
throw new IllegalArgumentException("Expected a single String as argument.");
}

Node expression = args.get(0);
Node expression = args.getFirst();
if (expression.isConstant()) {
String key = (String) expression.getConstantValue();
if (key == null || !key.startsWith("$")) {
Expand Down Expand Up @@ -71,11 +71,11 @@ public Object invoke(Environment environment, Object[] args) {
public boolean isConstant(CompilationContext context, List<Node> args) {
// This macro is inherently not constant unless it is invoked for an empty string
// or for a literal (which doesn't start with a $)
if (args.size() != 1 || !args.get(0).isConstant()) {
if (args.size() != 1 || !args.getFirst().isConstant()) {
return false;
}

Object value = args.get(0).getConstantValue();
Object value = args.getFirst().getConstantValue();
return Strings.isEmpty(value) || !value.toString().startsWith("$");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void verify(CompilationContext context, Position position, List<Node> arg
super.verify(context, position, args);

if (args.get(0).isConstant()) {
String resourceName = String.valueOf(args.get(0).getConstantValue());
String resourceName = String.valueOf(args.getFirst().getConstantValue());
if (resources.resolve(resourceName).isEmpty()) {
context.warning(position, "Unknown resource: %s", resourceName);
}
Expand Down
Loading

0 comments on commit dda73a3

Please sign in to comment.