CPD Results

The following document contains the results of PMD's CPD 4.1.

Duplications

File Line
org/peaseplate/internal/lang/command/AbstractCompareCommand.java 33
org/peaseplate/internal/lang/command/AbstractEqualityCommand.java 33
	public AbstractEqualityCommand(TemplateLocator locator, int line, int column, ICommand leftCommand, ICommand rightCommand) {
		super(locator, line, column, leftCommand, rightCommand);
	}

	public abstract String getSign();
	
	/**
	 * @see org.peaseplate.internal.lang.command.ICommand#call(BuildContext)
	 */
    public Object call(BuildContext context) throws TemplateRuntimeException {
		Object result = null;
		ConversionService conversionService = context.getEngine().getConversionService();
		Object left = callLeftCommand(context);
		Object right = callRightCommand(context);
		Class<?> leftType = (left != null) ? left.getClass() : Void.class;
		Class<?> rightType = (right != null) ? right.getClass() : Void.class;
		
		try {
			if (
				(leftType == StringBuilder.class) || (rightType == StringBuilder.class) ||
				(leftType == StringBuffer.class) || (rightType == StringBuffer.class) ||
				(leftType == String.class) || (rightType == String.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, String.class),
					conversionService.convert(right, String.class)
				);

			else if ((leftType == BigDecimal.class) || (rightType == BigDecimal.class))
				result = evaluateComparable(
					conversionService.convert(left, BigDecimal.class),
					conversionService.convert(right, BigDecimal.class)
				);
			
			else if ((leftType == BigInteger.class) || (rightType == BigInteger.class))
				result = evaluateComparable(
					conversionService.convert(left, BigInteger.class),
					conversionService.convert(right, BigInteger.class)
				);
			
			else if (
				(leftType == double.class) || (leftType == Double.class) ||
				(rightType == double.class) || (rightType == Double.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Double.class),
					conversionService.convert(right, Double.class)
				);
			
			else if (
				(leftType == float.class) || (leftType == Float.class) ||
				(rightType == float.class) || (rightType == Float.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Float.class),
					conversionService.convert(right, Float.class)
				);
			
			else if (
				(leftType == long.class) || (leftType == Long.class) ||
				(rightType == long.class) || (rightType == Long.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Long.class),
					conversionService.convert(right, Long.class)
				);
			
			else if (
				(leftType == int.class) || (leftType == Integer.class) ||
				(rightType == int.class) || (rightType == Integer.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Integer.class),
					conversionService.convert(right, Integer.class)
				);
			
			else if (
				(leftType == char.class) || (leftType == Character.class) ||
				(rightType == char.class) || (rightType == Character.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Character.class),
					conversionService.convert(right, Character.class)
				);
			
			else if (
				(leftType == short.class) || (leftType == Short.class) ||
				(rightType == short.class) || (rightType == Short.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Short.class),
					conversionService.convert(right, Short.class)
				);
			
			else if (
				(leftType == byte.class) || (leftType == Byte.class) ||
				(rightType == byte.class) || (rightType == Byte.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Byte.class),
					conversionService.convert(right, Byte.class)
				);
			
			else if (
				(leftType == boolean.class) || (leftType == Boolean.class) ||
				(rightType == boolean.class) || (rightType == Boolean.class)
			)
				result = evaluateComparable(
					conversionService.convert(left, Boolean.class),
					conversionService.convert(right, Boolean.class)
				);

			else if ((leftType == Void.class) && (rightType == Void.class))
				result = evaluateNull();

			else
				result = evaluateNonComparable(left, right);
		}
		catch (ConversionException e) {
			if (e.getValue() == left)
				throw new TemplateRuntimeException(
					getLeftCommand().getLocator(), getLeftCommand().getLine(), getLeftCommand().getColumn(),
					"Could not convert left operator to " + e.getType(), e
				);
	
			else if (e.getValue() == right)
				throw new TemplateRuntimeException(
					getRightCommand().getLocator(), getRightCommand().getLine(), getRightCommand().getColumn(),
					"Could not convert right operator to " + e.getType(), e
				);
	
			else 
				throw new TemplateRuntimeException(
					getLocator(), getLine(), getColumn(),
					"Could not convert operator to " + e.getType(), e
				);
		}

	    return result;
    }

	public Boolean evaluateNull() throws TemplateRuntimeException {
		throw new TemplateRuntimeException(
			getLocator(), getLine(), getColumn(),
			"Operation " + getSign() + " not supported on operands both null"
		);
	}
	
	public <TYPE> Boolean evaluateComparable(Comparable<TYPE> left, Comparable<TYPE> right) throws TemplateRuntimeException {
		Class<?> leftType = (left != null) ? left.getClass() : Void.class;
		Class<?> rightType = (right != null) ? right.getClass() : Void.class;

		throw new TemplateRuntimeException(
			getLocator(), getLine(), getColumn(),
			"Operation " + getSign() + " not supported on operands of type " + leftType + " and " + rightType
		);
	}
	
	public Boolean evaluateNonComparable(Object left, Object right) throws TemplateRuntimeException {
		Class<?> leftType = (left != null) ? left.getClass() : Void.class;
		Class<?> rightType = (right != null) ? right.getClass() : Void.class;

		throw new TemplateRuntimeException(
			getLocator(), getLine(), getColumn(),
			"Operation " + getSign() + " not supported on operands of type " + leftType + " and " + rightType
		);
	}
	
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "(" + getLeftCommand() + " " + getSign() + " " + getRightCommand() + ")";
	}
	
}