Skip to content

Commit b1b5fe9

Browse files
committed
Polishing.
Remove unecessary toString calls. Make Cast.create public to align with other factory methods. Fix AbstractSegment.toString See #1066 Original pull request: #1071.
1 parent cf39fa2 commit b1b5fe9

File tree

9 files changed

+48
-13
lines changed

9 files changed

+48
-13
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/AbstractSegment.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18-
import java.util.Arrays;
19-
2018
import org.springframework.util.Assert;
2119
import org.springframework.util.StringUtils;
2220

@@ -70,7 +68,6 @@ public boolean equals(Object obj) {
7068

7169
@Override
7270
public String toString() {
73-
return StringUtils.collectionToDelimitedString(Arrays.asList(children), ", ", getClass().getSimpleName() + "(",
74-
")");
71+
return getClass().getSimpleName() + "(" + StringUtils.arrayToDelimitedString(children, ", ") + ")";
7572
}
7673
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/AliasedExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ public SqlIdentifier getAlias() {
5757
*/
5858
@Override
5959
public String toString() {
60-
return expression.toString() + " AS " + alias;
60+
return expression + " AS " + alias;
6161
}
6262
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Cast.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.util.Assert;
1919

2020
/**
21-
* Represents a CAST expression like {@code CAST(something AS JSON}.
21+
* Represents a {@code CAST} expression like {@code CAST(something AS JSON}.
2222
*
2323
* @author Jens Schauder
2424
* @since 2.3
@@ -39,13 +39,13 @@ private Cast(Expression expression, String targetType) {
3939
}
4040

4141
/**
42-
* Creates a new CAST expression.
42+
* Creates a new {@code CAST} expression.
4343
*
4444
* @param expression the expression to cast. Must not be {@literal null}.
4545
* @param targetType the type to cast to. Must not be {@literal null}.
46-
* @return guaranteed to be not {@literal null}.
46+
* @return the {@code CAST} for {@code expression} into {@code targetType}.
4747
*/
48-
static Expression create(Expression expression, String targetType) {
48+
public static Expression create(Expression expression, String targetType) {
4949
return new Cast(expression, targetType);
5050
}
5151

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Comparison.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ public Expression getRight() {
116116

117117
@Override
118118
public String toString() {
119-
return left.toString() + " " + comparator + " " + right.toString();
119+
return left + " " + comparator + " " + right;
120120
}
121121
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Expressions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static Expression asterisk(Table table) {
5555

5656
/**
5757
* @return a new {@link Cast} expression.
58+
* @since 2.3
5859
*/
5960
public static Expression cast(Expression expression, String targetType) {
6061
return Cast.create(expression, targetType);

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/Not.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public Condition not() {
4545
*/
4646
@Override
4747
public String toString() {
48-
return "NOT " + condition.toString();
48+
return "NOT " + condition;
4949
}
5050
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SubselectExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public class SubselectExpression extends AbstractSegment implements Expression {
3838
*/
3939
@Override
4040
public String toString() {
41-
return "(" + subselect.toString() + ")";
41+
return "(" + subselect + ")";
4242
}
4343
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.relational.core.sql;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* Unit tests for {@link AbstractSegment}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
class AbstractSegmentTests {
28+
29+
@Test // GH-1066
30+
void shouldReportToStringCorrectly() {
31+
32+
Table table = Table.create("foo");
33+
AbstractSegment segment = new AbstractTestSegment(table.column("col1"), table.column("col2"));
34+
35+
assertThat(segment).hasToString("AbstractTestSegment(foo.col1, foo.col2)");
36+
}
37+
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/AbstractTestSegment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Jens Schauder
2222
*/
23-
public class AbstractTestSegment extends AbstractSegment{
23+
public class AbstractTestSegment extends AbstractSegment {
2424
protected AbstractTestSegment(Segment... children) {
2525
super(children);
2626
}

0 commit comments

Comments
 (0)