U64|128|HashValue: create Builder classes
U64, U128, HashValue gain a Builder class that allows to perform
a series of operations on the values without re-allocating the object.
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValue.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValue.java
index a656dab..3030e3e 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValue.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValue.java
@@ -45,4 +45,58 @@
/** xor this value with another value value of the same type */
H xor(H other);
+
+ /** create and return a builder */
+ Builder<H> builder();
+
+ /** a mutator for HashValues. Allows perfomring a series of
+ * operations on a hashv value without the associated cost of object
+ * reallocation.
+ *
+ * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
+ *
+ * @param <H> - the hashvalue
+ */
+ public interface Builder<H> {
+ /** perform an arithmetic addition of this value and other. Wraps around on
+ * overflow of the defined word size.
+ *
+ * @param other
+ * @return this mutator
+ */
+ Builder<H> add(H other);
+
+ /**
+ * arithmetically substract the given 'other' value from the value stored in this mutator.
+ * around on overflow.
+ *
+ * @param other
+ * @return this mutator
+ */
+ Builder<H> subtract(H other);
+
+ /** bitwise invert the value stored in this mutator
+ *
+ * @return this mutator
+ */
+ Builder<H> invert();
+
+ /** or the value stored in this mutator with another value value of the same type
+ * @return this mutator
+ */
+ Builder<H> or(H other);
+
+ /** and the value stored in this mutator with another value value of the same type
+ * @return this mutator
+ */
+ Builder<H> and(H other);
+
+ /** xor the value stored in this mutator with another value value of the same type
+ * @return this mutator
+ */
+ Builder<H> xor(H other);
+
+ /** @return the hash value */
+ public H build();
+ }
}
\ No newline at end of file
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
index 0b4e0f7..ddf4faa 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
@@ -147,4 +147,76 @@
return HashValueUtils.prefixBits(this.raw1, numBits);
}
+ @Override
+ public HashValue.Builder<U128> builder() {
+ return new U128Builder(raw1, raw2);
+ }
+
+ static class U128Builder implements HashValue.Builder<U128> {
+ private long raw1, raw2;
+
+ public U128Builder(long raw1, long raw2) {
+ this.raw1 = raw1;
+ this.raw2 = raw2;
+ }
+
+ @Override
+ public Builder<U128> add(U128 other) {
+ raw2 += other.raw2;
+ raw1 += other.raw1;
+ if(UnsignedLongs.compare(raw2, other.raw2) < 0) {
+ // raw2 overflow
+ raw1+=1;
+ }
+ return this;
+ }
+
+ @Override
+ public Builder<U128> subtract(
+ U128 other) {
+ if(UnsignedLongs.compare(this.raw2, other.raw2) >= 0) {
+ raw2 -= other.raw2;
+ raw1 -= other.raw1;
+ } else {
+ // raw2 overflow
+ raw2 -= other.raw2;
+ raw1 = this.raw1 - other.raw1 - 1;
+ }
+ return this;
+ }
+
+ @Override
+ public Builder<U128> invert() {
+ raw1 = ~raw1;
+ raw2 = ~raw2;
+ return this;
+ }
+
+ @Override
+ public Builder<U128> or(U128 other) {
+ raw1 |= other.raw1;
+ raw2 |= other.raw2;
+ return this;
+ }
+
+ @Override
+ public Builder<U128> and(U128 other) {
+ raw1 &= other.raw1;
+ raw2 &= other.raw2;
+ return this;
+ }
+
+ @Override
+ public Builder<U128> xor(U128 other) {
+ raw1 ^= other.raw1;
+ raw2 ^= other.raw2;
+ return this;
+ }
+
+ @Override
+ public U128 build() {
+ return U128.of(raw1, raw2);
+ }
+ }
+
}
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U64.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U64.java
index dcd812d..f15544f 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U64.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U64.java
@@ -179,4 +179,59 @@
}
}
+ @Override
+ public HashValue.Builder<U64> builder() {
+ return new U64Builder(raw);
+ }
+
+ static class U64Builder implements Builder<U64> {
+ long raw;
+
+ public U64Builder(long raw) {
+ this.raw = raw;
+ }
+
+ @Override
+ public Builder<U64> add(U64 other) {
+ raw += other.raw;
+ return this;
+ }
+
+ @Override
+ public Builder<U64> subtract(
+ U64 other) {
+ raw -= other.raw;
+ return this;
+ }
+
+ @Override
+ public Builder<U64> invert() {
+ raw = ~raw;
+ return this;
+ }
+
+ @Override
+ public Builder<U64> or(U64 other) {
+ raw |= other.raw;
+ return this;
+ }
+
+ @Override
+ public Builder<U64> and(U64 other) {
+ raw &= other.raw;
+ return this;
+ }
+
+ @Override
+ public Builder<U64> xor(U64 other) {
+ raw ^= other.raw;
+ return this;
+ }
+
+ @Override
+ public U64 build() {
+ return U64.of(raw);
+ }
+ }
+
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
index 5393db9..81d9d7f 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
@@ -14,6 +14,7 @@
import org.hamcrest.Matchers;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.Before;
import org.junit.Test;
import com.google.common.hash.HashCode;
@@ -21,6 +22,9 @@
import com.google.common.hash.Hashing;
public class U128Test {
+ private Triple[] triples;
+
+
@Test
public void testPositiveRaws() {
assertThat(U128.of(0, 0).getMsb(), equalTo(0L));
@@ -161,8 +165,8 @@
}
}
- @Test
- public void testAddSubtract() {
+ @Before
+ public void setup() {
U128 u0_0 = U128.of(0, 0);
U128 u0_1 = U128.of(0, 1);
U128 u1_0 = U128.of(1, 0);
@@ -174,7 +178,7 @@
U128 u0_f = U128.of(0, -1L);
U128 uf_0 = U128.of(-1L, 0);
- Triple[] triples = new Triple[] {
+ triples = new Triple[] {
Triple.of(u0_0, u0_0, u0_0),
Triple.of(u0_0, u0_1, u0_1),
Triple.of(u0_0, u1_0, u1_0),
@@ -194,7 +198,10 @@
U128.of(0xedcb_a987_6543_210eL, 0xedcb_a987_6543_210fL),
U128.ZERO)
};
+ }
+ @Test
+ public void testAddSubtract() {
for(Triple t: triples) {
assertThat(t.msg("{0} + {1} = {2}"), t.a.add(t.b), equalTo(t.c));
assertThat(t.msg("{1} + {0} = {2}"), t.b.add(t.a), equalTo(t.c));
@@ -205,6 +212,17 @@
}
@Test
+ public void testAddSubtractBuilder() {
+ for(Triple t: triples) {
+ assertThat(t.msg("{0} + {1} = {2}"), t.a.builder().add(t.b).build(), equalTo(t.c));
+ assertThat(t.msg("{1} + {0} = {2}"), t.b.builder().add(t.a).build(), equalTo(t.c));
+
+ assertThat(t.msg("{2} - {0} = {1}"), t.c.builder().subtract(t.a).build(), equalTo(t.b));
+ assertThat(t.msg("{2} - {1} = {0}"), t.c.builder().subtract(t.b).build(), equalTo(t.a));
+ }
+ }
+
+ @Test
public void testCompare() {
U128 u0_0 = U128.of(0, 0);
U128 u0_1 = U128.of(0, 1);
@@ -242,4 +260,26 @@
}
}
+ @Test
+ public void testBitwiseOperators() {
+ U128 one = U128.of(0x5, 0x8);
+ U128 two = U128.of(0x7, 0x3);
+
+ assertThat(one.inverse(), equalTo(U128.of(0xfffffffffffffffaL, 0xfffffffffffffff7L)));
+ assertThat(one.and(two), equalTo(U128.of(0x5L, 0x0L)));
+ assertThat(one.or(two), equalTo(U128.of(0x7L, 0xbL)));
+ assertThat(one.xor(two), equalTo(U128.of(0x2L, 0xbL)));
+ }
+
+ @Test
+ public void testBitwiseOperatorsBuilder() {
+ U128 one = U128.of(0x5, 0x8);
+ U128 two = U128.of(0x7, 0x3);
+
+ assertThat(one.builder().invert().build(), equalTo(U128.of(0xfffffffffffffffaL, 0xfffffffffffffff7L)));
+ assertThat(one.builder().and(two).build(), equalTo(U128.of(0x5L, 0x0L)));
+ assertThat(one.builder().or(two).build(), equalTo(U128.of(0x7L, 0xbL)));
+ assertThat(one.builder().xor(two).build(), equalTo(U128.of(0x2L, 0xbL)));
+ }
+
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U64Test.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U64Test.java
index 7bd1c46..4066bf8 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U64Test.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U64Test.java
@@ -11,10 +11,13 @@
import java.math.BigInteger;
import java.text.MessageFormat;
+import org.junit.Before;
import org.junit.Test;
public class U64Test {
+ private Triple[] triples;
+
@Test
public void testPositiveRaws() {
for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
@@ -106,8 +109,8 @@
}
}
- @Test
- public void testAddSubtract() {
+ @Before
+ public void setup() {
U64 u0 = U64.of(0);
U64 u1 = U64.of(1);
@@ -117,7 +120,7 @@
U64 uf = U64.of(-1L);
- Triple[] triples = new Triple[] {
+ triples = new Triple[] {
Triple.of(u0, u0, u0),
Triple.of(u0, u1, u1),
@@ -134,7 +137,10 @@
U64.of(0xedcb_a987_6543_210fL),
U64.ZERO)
};
+ }
+ @Test
+ public void testAddSubtract() {
for(Triple t: triples) {
assertThat(t.msg("{0} + {1} = {2}"), t.a.add(t.b), equalTo(t.c));
assertThat(t.msg("{1} + {0} = {2}"), t.b.add(t.a), equalTo(t.c));
@@ -144,6 +150,17 @@
}
}
+ @Test
+ public void testAddSubtractBuilder() {
+ for(Triple t: triples) {
+ assertThat(t.msg("{0} + {1} = {2}"), t.a.builder().add(t.b).build(), equalTo(t.c));
+ assertThat(t.msg("{1} + {0} = {2}"), t.b.builder().add(t.a).build(), equalTo(t.c));
+
+ assertThat(t.msg("{2} - {0} = {1}"), t.c.builder().subtract(t.a).build(), equalTo(t.b));
+ assertThat(t.msg("{2} - {1} = {0}"), t.c.builder().subtract(t.b).build(), equalTo(t.a));
+ }
+ }
+
private void
checkInvalidKeyBitSize(U64 u, int prefixBit) {
try {
@@ -154,4 +171,25 @@
}
}
+ @Test
+ public void testBitwiseOperators() {
+ U64 notPi = U64.of(0x3141_5926_5358_9793L);
+ U64 notE = U64.of(0x2718_2818_8459_4523L);
+
+ assertThat(notPi.inverse(), equalTo(U64.of(0xcebe_a6d9_aca7_686cL)));
+ assertThat(notPi.and(notE), equalTo(U64.of(0x2100_0800_0058_0503L)));
+ assertThat(notPi.or(notE), equalTo(U64.of(0x3759_793e_d759_d7b3L)));
+ assertThat(notPi.xor(notE), equalTo(U64.of(0x1659_713e_d701_d2b0L)));
+ }
+
+ @Test
+ public void testBitwiseOperatorsBuilder() {
+ U64 notPi = U64.of(0x3141_5926_5358_9793L);
+ U64 notE = U64.of(0x2718_2818_8459_4523L);
+
+ assertThat(notPi.builder().invert().build(), equalTo(U64.of(0xcebe_a6d9_aca7_686cL)));
+ assertThat(notPi.builder().and(notE).build(), equalTo(U64.of(0x2100_0800_0058_0503L)));
+ assertThat(notPi.builder().or(notE).build(), equalTo(U64.of(0x3759_793e_d759_d7b3L)));
+ assertThat(notPi.builder().xor(notE).build(), equalTo(U64.of(0x1659_713e_d701_d2b0L)));
+ }
}