GROOVY-6333: Changes required regarding unaryPlus and bitwiseNegate operators for numbers
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 549c021..7270232 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -11133,6 +11133,17 @@
}
/**
+ * Bitwise NEGATE a Number.
+ *
+ * @param left a Number
+ * @return the bitwise NEGATE of the Number
+ * @since 2.2.0
+ */
+ public static Number bitwiseNegate(Number left) {
+ return NumberMath.bitwiseNegate(left);
+ }
+
+ /**
* Bitwise OR together two BitSets. Called when the '|' operator is used
* between two bit sets.
*
diff --git a/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java b/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
index ea7b413..ecc0e35 100644
--- a/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
+++ b/src/main/org/codehaus/groovy/runtime/typehandling/NumberMath.java
@@ -127,11 +127,15 @@
}
return getMath(left).rightShiftUnsignedImpl(left,right);
}
-
+
+ public static Number bitwiseNegate(Number left) {
+ return getMath(left).bitwiseNegateImpl(left);
+ }
+
public static Number unaryMinus(Number left) {
return getMath(left).unaryMinusImpl(left);
}
-
+
public static Number unaryPlus(Number left) {
return getMath(left).unaryPlusImpl(left);
}
@@ -222,11 +226,14 @@
protected abstract Number unaryMinusImpl(Number left);
protected abstract Number unaryPlusImpl(Number left);
+ protected Number bitwiseNegateImpl(Number left) {
+ throw createUnsupportedException("bitwiseNegate()", left);
+ }
protected Number orImpl(Number left, Number right) {
throw createUnsupportedException("or()", left);
}
-
+
protected Number andImpl(Number left, Number right) {
throw createUnsupportedException("and()", left);
}
diff --git a/src/test/groovy/operator/IntegerOperatorsTest.groovy b/src/test/groovy/operator/IntegerOperatorsTest.groovy
index fa0fac0..1f77bf8 100644
--- a/src/test/groovy/operator/IntegerOperatorsTest.groovy
+++ b/src/test/groovy/operator/IntegerOperatorsTest.groovy
@@ -21,6 +21,8 @@
x = 3
y = +x
assert y == 3
+ y = x.unaryPlus()
+ assert y == 3
}
void testCharacterPlus() {
@@ -64,6 +66,16 @@
x = 3
y = -x
assert y == -3
+ y = x.unaryMinus()
+ assert y == -3
+ }
+
+ void testBitwiseNegate() {
+ x = 3
+ y = ~x
+ assert y == -4
+ y = x.bitwiseNegate()
+ assert y == -4
}
void testCharacterMinus() {