Skip to content

Commit

Permalink
Replace String.format with String concat
Browse files Browse the repository at this point in the history
Replaces simple String.format usages in non-exceptional code paths
with simple string concatenations where applicable.
  • Loading branch information
pettyjamesm authored and martint committed Mar 27, 2023
1 parent f6a8e1c commit 4f8985c
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.client.ClientStandardTypes.ROW;
import static io.trino.client.ClientStandardTypes.VARCHAR;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -104,20 +103,18 @@ public String toString()
@Deprecated
private String rowToString()
{
String fields = arguments.stream()
if (arguments.isEmpty()) {
return "row";
}
return arguments.stream()
.map(ClientTypeSignatureParameter::getNamedTypeSignature)
.map(parameter -> {
if (parameter.getName().isPresent()) {
return format("%s %s", parameter.getName().get(), parameter.getTypeSignature());
return parameter.getName().get() + " " + parameter.getTypeSignature();
}
return parameter.getTypeSignature().toString();
})
.collect(joining(","));

if (fields.isEmpty()) {
return "row";
}
return format("row(%s)", fields);
.collect(joining(",", "row(", ")"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class NamedClientTypeSignature
Expand Down Expand Up @@ -73,7 +72,7 @@ public boolean equals(Object o)
public String toString()
{
if (fieldName.isPresent()) {
return format("%s %s", fieldName.get(), typeSignature);
return fieldName.get() + " " + typeSignature;
}
return typeSignature.toString();
}
Expand Down
3 changes: 1 addition & 2 deletions client/trino-client/src/main/java/io/trino/client/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -64,7 +63,7 @@ public String toString()
return fields.stream()
.map(field -> {
if (field.getName().isPresent()) {
return format("%s=%s", field.getName().get(), field.getValue());
return field.getName().get() + "=" + field.getValue();
}
return String.valueOf(field.getValue());
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.Objects;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class Warning
Expand Down Expand Up @@ -70,7 +69,7 @@ public int hashCode()
@Override
public String toString()
{
return format("%s, %s", warningCode, message);
return warningCode + ", " + message;
}

public static final class Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private Request authenticate(Request request)
String principal = makeServicePrincipal(servicePrincipalPattern, remoteServiceName, hostName, useCanonicalHostname);
byte[] token = generateToken(principal);

String credential = format("%s %s", NEGOTIATE, Base64.getEncoder().encodeToString(token));
String credential = NEGOTIATE + " " + Base64.getEncoder().encodeToString(token);
return request.newBuilder()
.header(AUTHORIZATION, credential)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,9 @@ else if (node.getArguments().size() > 127) {
for (int i = 0; i < argumentTypes.size(); i++) {
Expression expression = node.getArguments().get(i);
Type expectedType = signature.getArgumentTypes().get(i);
requireNonNull(expectedType, format("Type '%s' not found", signature.getArgumentTypes().get(i)));
if (expectedType == null) {
throw new NullPointerException(format("Type '%s' not found", signature.getArgumentTypes().get(i)));
}
if (node.isDistinct() && !expectedType.isComparable()) {
throw semanticException(TYPE_MISMATCH, node, "DISTINCT can only be applied to comparable types (actual: %s)", expectedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.bytecode.expression.BytecodeExpressions.constantFalse;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class AndCodeGenerator
Expand Down Expand Up @@ -58,7 +57,7 @@ public BytecodeNode generateExpression(BytecodeGeneratorContext generator)
RowExpression term = terms.get(i);
block.append(generator.generate(term));

IfStatement ifWasNull = new IfStatement(format("if term %s wasNull...", i))
IfStatement ifWasNull = new IfStatement("if term " + i + " wasNull...")
.condition(wasNull);

ifWasNull.ifTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static BytecodeNode handleNullValue(

isNull.pushJavaDefault(returnType);
String loadDefaultComment;
loadDefaultComment = format("loadJavaDefault(%s)", returnType.getName());
loadDefaultComment = "loadJavaDefault(" + returnType.getName() + ")";

isNull.gotoLabel(label);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.bytecode.expression.BytecodeExpressions.constantFalse;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class OrCodeGenerator
Expand Down Expand Up @@ -57,7 +56,7 @@ public BytecodeNode generateExpression(BytecodeGeneratorContext generator)
RowExpression term = terms.get(i);
block.append(generator.generate(term));

IfStatement ifWasNull = new IfStatement(format("if term %s wasNull...", i))
IfStatement ifWasNull = new IfStatement("if term " + i + " wasNull...")
.condition(wasNull);

ifWasNull.ifTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ public Plan plan(Analysis analysis, Stage stage, boolean collectPlanStatistics)
if (stage.ordinal() >= OPTIMIZED.ordinal()) {
for (PlanOptimizer optimizer : planOptimizers) {
root = optimizer.optimize(root, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator, warningCollector, tableStatsProvider);
requireNonNull(root, format("%s returned a null plan", optimizer.getClass().getName()));
if (root == null) {
throw new NullPointerException(optimizer.getClass().getName() + " returned a null plan");
}

if (LOG.isDebugEnabled()) {
LOG.debug("%s:\n%s", optimizer.getClass().getName(), PlanPrinter.textLogicalPlan(
Expand Down Expand Up @@ -545,9 +547,7 @@ private RelationPlan getInsertPlan(

private Expression createNullNotAllowedFailExpression(String columnName, Type type)
{
return new Cast(failFunction(metadata, session, CONSTRAINT_VIOLATION, format(
"NULL value not allowed for NOT NULL column: %s",
columnName)), toSqlType(type));
return new Cast(failFunction(metadata, session, CONSTRAINT_VIOLATION, "NULL value not allowed for NOT NULL column: " + columnName), toSqlType(type));
}

private static Function<Expression, Expression> failIfPredicateIsNotMet(Metadata metadata, Session session, ErrorCodeSupplier errorCode, String errorMessage)
Expand Down
12 changes: 3 additions & 9 deletions core/trino-main/src/main/java/io/trino/type/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.type;

import com.google.common.base.Joiner;
import io.trino.spi.TrinoException;
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.CharType;
Expand All @@ -28,14 +27,11 @@
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;

import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.type.StandardTypes.ARRAY;
import static io.trino.spi.type.StandardTypes.MAP;
import static io.trino.spi.type.StandardTypes.ROW;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;

public final class TypeUtils
{
Expand Down Expand Up @@ -107,16 +103,14 @@ private static String getDisplayLabelForLegacyClients(Type type)

private static String getRowDisplayLabelForLegacyClients(RowType type)
{
List<String> fields = type.getFields().stream()
return type.getFields().stream()
.map(field -> {
String typeDisplayName = getDisplayLabelForLegacyClients(field.getType());
if (field.getName().isPresent()) {
return field.getName().get() + ' ' + typeDisplayName;
}
return typeDisplayName;
})
.collect(toImmutableList());

return format("%s(%s)", ROW, Joiner.on(", ").join(fields));
.collect(joining(", ", ROW + "(", ")"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
import static io.trino.sql.ExpressionFormatter.formatStringLiteral;
import static io.trino.sql.ExpressionFormatter.formatWindowSpecification;
import static io.trino.sql.RowPatternFormatter.formatPattern;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -1343,10 +1342,9 @@ protected Void visitCreateTableAsSelect(CreateTableAsSelect node, Integer indent
builder.append(formatName(node.getName()));

node.getColumnAliases().ifPresent(columnAliases -> {
String columnList = columnAliases.stream()
builder.append(columnAliases.stream()
.map(SqlFormatter::formatName)
.collect(joining(", "));
builder.append(format("( %s )", columnList));
.collect(joining(", ", "( ", " )")));
});

node.getComment().ifPresent(comment -> builder
Expand Down Expand Up @@ -1469,7 +1467,7 @@ private static String formatPrincipal(PrincipalSpecification principal)
return principal.getName().toString();
case USER:
case ROLE:
return format("%s %s", type.name(), principal.getName());
return type.name() + " " + principal.getName();
}
throw new IllegalArgumentException("Unsupported principal type: " + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3468,7 +3468,9 @@ else if (isHexDigit(ch)) {
}
else {
char currentCodePoint = (char) codePoint;
check(!Character.isSurrogate(currentCodePoint), format("Invalid escaped character: %s. Escaped character is a surrogate. Use '\\+123456' instead.", currentEscapedCode), context);
if (Character.isSurrogate(currentCodePoint)) {
throw parseError(format("Invalid escaped character: %s. Escaped character is a surrogate. Use '\\+123456' instead.", currentEscapedCode), context);
}
unicodeStringBuilder.append(currentCodePoint);
}
state = UnicodeDecodeState.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class PathElement
Expand Down Expand Up @@ -98,7 +97,7 @@ public int hashCode()
public String toString()
{
if (catalog.isPresent()) {
return format("%s.%s", catalog.get(), schema);
return catalog.get() + "." + schema;
}
return schema.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class UpdateAssignment
Expand Down Expand Up @@ -90,6 +89,6 @@ public int hashCode()
@Override
public String toString()
{
return format("%s = %s", name, value);
return name + " = " + value;
}
}

0 comments on commit 4f8985c

Please sign in to comment.