Skip to content

Commit

Permalink
[SONAR] cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nadment committed Jul 8, 2024
1 parent 6777812 commit ddcb679
Show file tree
Hide file tree
Showing 37 changed files with 214 additions and 209 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ http://www.apache.org/licenses/LICENSE-2.0
</parent>

<artifactId>hop-expression</artifactId>
<version>0.7.0-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hop Plugins Expression</name>
<url>http://hop.apache.org</url>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/apache/hop/expression/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import org.apache.hop.expression.type.Type;
Expand All @@ -28,11 +29,11 @@ public final class Array implements IExpression, Iterable<IExpression> {
/**
* Iterator implementation used to efficiently expose contents of an Array as read-only iterator.
*/
public class Iterator implements java.util.Iterator<IExpression> {
private class ArrayIterator implements java.util.Iterator<IExpression> {

private int index;

public Iterator() {
public ArrayIterator() {
index = 0;
}

Expand Down Expand Up @@ -114,8 +115,7 @@ public <E> E accept(IExpressionVisitor<E> visitor) {

@Override
public boolean equals(Object other) {
if (other instanceof Array) {
Array array = (Array) other;
if (other instanceof Array array) {
return Arrays.equals(values, array.values);
}
return false;
Expand All @@ -134,7 +134,7 @@ public String toString() {
}

@Override
public void unparse(StringWriter writer, int leftPrec, int rightPrec) {
public void unparse(final StringWriter writer, int leftPrec, int rightPrec) {
writer.append("ARRAY[");
this.unparseValues(writer);
writer.append(']');
Expand All @@ -145,7 +145,7 @@ public void unparse(StringWriter writer, int leftPrec, int rightPrec) {
*
* @param writer
*/
public void unparseValues(StringWriter writer) {
public void unparseValues(final StringWriter writer) {
boolean first = true;
for (IExpression expression : values) {
if (first) first = false;
Expand All @@ -170,7 +170,7 @@ public Stream<IExpression> stream() {

@Override
public java.util.Iterator<IExpression> iterator() {
return new Iterator();
return new ArrayIterator();
}

@Override
Expand Down Expand Up @@ -206,9 +206,9 @@ public boolean contains(IExpression element) {
* @see #contains(Object)
*/
public boolean containsAll(Collection<IExpression> collection) {
Iterator it = (Iterator) collection.iterator();
while (it.hasNext()) {
if (contains(it.next()) == false) {
Iterator<IExpression> iterator = collection.iterator();
while (iterator.hasNext()) {
if (!contains(iterator.next())) {
return false;
}
}
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/org/apache/hop/expression/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,7 @@ private IExpression parseFunction(Token token) throws ExpressionException {

// Function with custom syntax
switch (token.text()) {
case "CAST":
case "TRY_CAST":
case "CAST", "TRY_CAST":
return parseFunctionCast(token, function);
case "EXTRACT":
return parseFunctionExtract(token, function);
Expand Down Expand Up @@ -1448,17 +1447,7 @@ protected Token tokenize() throws ExpressionException {
throw new ExpressionException(start, ErrorCode.MISSING_END_DOUBLE_QUOTED_STRING);
}

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.': // Number without zero .1
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.': // Number without zero .1
{
int start = position;
char previous;
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/apache/hop/expression/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public Object getValue() {
return valueMeta.getString(row[ordinal]);
case IValueMeta.TYPE_INTEGER:
return valueMeta.getInteger(row[ordinal]);
case IValueMeta.TYPE_NUMBER:
case IValueMeta.TYPE_BIGNUMBER:
case IValueMeta.TYPE_NUMBER, IValueMeta.TYPE_BIGNUMBER:
return valueMeta.getBigNumber(row[ordinal]);
case ValueMetaJson.TYPE_JSON:
return valueMeta.getNativeDataType(row[ordinal]);
Expand Down Expand Up @@ -295,15 +294,13 @@ protected Type createDataType(final IValueMeta meta) {
switch (meta.getType()) {
case IValueMeta.TYPE_BOOLEAN:
return Types.BOOLEAN;
case IValueMeta.TYPE_DATE:
case IValueMeta.TYPE_TIMESTAMP:
case IValueMeta.TYPE_DATE, IValueMeta.TYPE_TIMESTAMP:
return Types.DATE;
case IValueMeta.TYPE_STRING:
return StringType.of(meta.getLength());
case IValueMeta.TYPE_INTEGER:
return IntegerType.of(meta.getLength());
case IValueMeta.TYPE_NUMBER:
case IValueMeta.TYPE_BIGNUMBER:
case IValueMeta.TYPE_NUMBER, IValueMeta.TYPE_BIGNUMBER:
return NumberType.of(meta.getLength(), meta.getPrecision());
case ValueMetaJson.TYPE_JSON:
return Types.JSON;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/apache/hop/expression/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,17 @@ protected Call normalizeSymmetricalPredicate(Call call) {

// Swap terms 1=A → A=1
if (left.is(Kind.LITERAL)) {
System.out.println("Swap literal to right");
return swap(call);
}
// Swap terms B=A → A=B
if (left.is(Kind.IDENTIFIER)
&& left.asIdentifier().getName().compareTo(right.asIdentifier().getName()) > 0) {
System.out.println("Swap identifier by name");
return swap(call);
}
}

// Normalize operator by moving the low-cost operand to the left
if (left.getCost() > right.getCost()) {
System.out.println("Swap term by cost");
return swap(call);
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/apache/hop/expression/operator/AddOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public IExpression compile(IExpressionContext context, Call call) throws Express
private static final class AddInteger extends AddOperator {
private static final AddOperator INSTANCE = new AddInteger();

private AddInteger() {
super();
}

@Override
public boolean isSymmetrical() {
return true;
Expand All @@ -130,6 +134,10 @@ private static final class AddNumber extends AddOperator {

private static final AddOperator INSTANCE = new AddNumber();

private AddNumber() {
super();
}

@Override
public boolean isSymmetrical() {
return true;
Expand All @@ -155,6 +163,10 @@ public Object eval(final IExpression[] operands) {
private static final class AddInterval extends AddOperator {
private static final AddOperator INSTANCE = new AddInterval();

private AddInterval() {
super();
}

@Override
public IExpression compile(IExpressionContext context, Call call) throws ExpressionException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** Binary operator, as in "x + y". */
public abstract class BinaryOperator extends Operator {

public BinaryOperator(
protected BinaryOperator(
String id,
String name,
int precedence,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public IExpression compile(IExpressionContext context, Call call) throws Express
private static final class EndsWithString extends EndsWithFunction {
public static final EndsWithFunction INSTANCE = new EndsWithString();

private EndsWithString() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String value = operands[0].getValue(String.class);
Expand All @@ -83,6 +87,10 @@ public Object eval(final IExpression[] operands) {
private static final class EndsWithBinary extends EndsWithFunction {
public static final EndsWithFunction INSTANCE = new EndsWithBinary();

private EndsWithBinary() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
byte[] value = operands[0].getValue(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Operator not() {
return Operators.IS_NOT_DISTINCT_FROM;
}

@Override
public Operator reverse() {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public IExpression compile(IExpressionContext context, Call call) throws Express

Type type = call.getOperand(0).getType();
if (type.is(TypeId.BINARY)) {
return new Call(LPadBinaryFunction.INSTANCE, call.getOperands());
return new Call(LPadBinary.INSTANCE, call.getOperands());
}

return new Call(LPadStringFunction.INSTANCE, call.getOperands());
return new Call(LPadString.INSTANCE, call.getOperands());
}

/** The function left-pads a string with another string, to a certain length. */
private static final class LPadStringFunction extends LPadFunction {
public static final LPadFunction INSTANCE = new LPadStringFunction();
private static final class LPadString extends LPadFunction {
public static final LPadFunction INSTANCE = new LPadString();

@Override
public Object eval(final IExpression[] operands) {
Expand Down Expand Up @@ -116,9 +116,9 @@ public Object eval(final IExpression[] operands) {
}

/** The function left-pads a binary with another binary, to a certain length. */
private static final class LPadBinaryFunction extends LPadFunction {
private static final class LPadBinary extends LPadFunction {

public static final LPadFunction INSTANCE = new LPadBinaryFunction();
public static final LPadFunction INSTANCE = new LPadBinary();

private static final byte[] DEFAULT = new byte[] {0x00};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public IExpression compile(IExpressionContext context, Call call) throws Express

// Binary first
if (type.is(TypeId.BINARY)) {
return new Call(BinaryLengthFunction.INSTANCE, call.getOperands());
return new Call(LengthBinary.INSTANCE, call.getOperands());
}
return new Call(StringLengthFunction.INSTANCE, call.getOperands());
return new Call(LengthString.INSTANCE, call.getOperands());
}

/** The function returns the number of characters of the specified string. */
private static final class StringLengthFunction extends LengthFunction {
public static final LengthFunction INSTANCE = new StringLengthFunction();
private static final class LengthString extends LengthFunction {
public static final LengthFunction INSTANCE = new LengthString();

@Override
public Object eval(final IExpression[] operands) {
Expand All @@ -66,8 +66,8 @@ public Object eval(final IExpression[] operands) {
}

/** The function returns the number of characters of the specified binary. */
private static final class BinaryLengthFunction extends LengthFunction {
public static final LengthFunction INSTANCE = new BinaryLengthFunction();
private static final class LengthBinary extends LengthFunction {
public static final LengthFunction INSTANCE = new LengthBinary();

@Override
public Object eval(final IExpression[] operands) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public IExpression compile(IExpressionContext context, Call call) throws Express

// field LIKE '%' → IFNULL(field,NULL,TRUE)
if ("%".equals(pattern)) {
// return new Call(Operators.EQUAL, value, value);
return new Call(Nvl2Function.INSTANCE, value, Literal.TRUE, Literal.NULL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void unparse(StringWriter writer, IExpression[] operands) {
private static final class NegateInteger extends NegateOperator {
private static final NegateOperator INSTANCE = new NegateInteger();

private NegateInteger() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Long value = operands[0].getValue(Long.class);
Expand All @@ -98,6 +102,10 @@ public Object eval(final IExpression[] operands) {
private static final class NegateNumber extends NegateOperator {
private static final NegateOperator INSTANCE = new NegateNumber();

private NegateNumber() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
BigDecimal value = operands[0].getValue(BigDecimal.class);
Expand All @@ -109,6 +117,10 @@ public Object eval(final IExpression[] operands) {
private static final class NegateInterval extends NegateOperator {
private static final NegateOperator INSTANCE = new NegateInterval();

private NegateInterval() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Interval interval = operands[0].getValue(Interval.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** Postfix unary operator, as in "x is null". */
public abstract class PostfixUnaryOperator extends Operator {

public PostfixUnaryOperator(
protected PostfixUnaryOperator(
String id,
int precedence,
boolean isLeftAssociative,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** Postfix unary operator, as in "x is null". */
public abstract class PrefixUnaryOperator extends Operator {

public PrefixUnaryOperator(
protected PrefixUnaryOperator(
String id,
String name,
int precedence,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Object eval(final IExpression[] operands) {
if (repeat == null) return null;

final int len = value.length();
final long longSize = (long) len * repeat;
final long longSize = len * repeat;
final int size = (int) longSize;
if (size != longSize) {
throw new ExpressionException("Result size too large: %s".formatted(longSize));
Expand Down
Loading

0 comments on commit ddcb679

Please sign in to comment.