java_gen: introduced HashValue type
This is a contract for types that support bit-wise operations
to support calculating and manipulating hash values.
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashFunc.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashFunc.java
new file mode 100644
index 0000000..f7648d7
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashFunc.java
@@ -0,0 +1,14 @@
+package org.projectfloodlight.openflow.types;
+
+/** A hash function that can hash a {@link PrimitiveSinkable}.
+ *
+ * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
+ * @param <H> - the hash.
+ */
+public interface HashFunc<H extends HashValue<H>> {
+ /** Hash the PrimitivateSinkable with this hash function and return the result */
+ public H hash(PrimitiveSinkable v);
+
+ /** Provide a defined null-hash */
+ public H nullHash();
+}
\ No newline at end of file
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
new file mode 100644
index 0000000..1dd55d5
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValue.java
@@ -0,0 +1,54 @@
+package org.projectfloodlight.openflow.types;
+
+import javax.annotation.concurrent.Immutable;
+
+/** a hash value that supports bit-wise combinations, mainly to calculate hash values for
+ * reconciliation operations.
+ *
+ * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
+ *
+ * @param <H> - this type, for return type safety.
+ */
+@Immutable
+public interface HashValue<H extends HashValue<H>> {
+ /** return the "numBits" highest-order bits of the hash.
+ * @param numBits number of higest-order bits to return [0-32].
+ * @return a numberic value of the 0-32 highest-order bits.
+ */
+ int prefixBits(int numBits);
+
+ /** @return the bitwise inverse of this value */
+ H inverse();
+
+ /** or this value with another value value of the same type */
+ H or(H other);
+
+ /** and this value with another value value of the same type */
+ H and(H other);
+
+ /** xor this value with another value value of the same type */
+ H xor(H other);
+
+ /** calculate a combined hash value of this hash value (the <b>Key</b>) and the hash value
+ * specified as a parameter (the <b>Value</b>).
+ * <p>
+ * The value is constructed as follows:
+ * <ul>
+ * <li>the first keyBits bits are taken only from the Key
+ * <li>the other bits are taken from key xor value.
+ * </ul>
+ * The overall result looks like this:
+ * <pre>
+ * MSB LSB
+ * +---------+--------------+
+ * | key | key ^ value |
+ * +---------+--------------+
+ * |-keyBits-|
+ * </pre>
+ *
+ * @param value - hash value to be compared with this value (the key)
+ * @param keyBits number of prefix bits that are just taken from key
+ * @return the combined value.
+ */
+ H combineWithValue(H value, int keyBits);
+}
\ No newline at end of file
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValueUtils.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValueUtils.java
new file mode 100644
index 0000000..6780035
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/HashValueUtils.java
@@ -0,0 +1,17 @@
+package org.projectfloodlight.openflow.types;
+
+import com.google.common.base.Preconditions;
+
+public class HashValueUtils {
+ private HashValueUtils() { }
+
+ public static long combineWithValue(long key, long value, int keyBits) {
+ Preconditions.checkArgument(keyBits >= 0 && keyBits <= 64, "keyBits must be [0,64]");
+
+ int valueBits = 64 - keyBits;
+ long valueMask = valueBits == 64 ? 0xFFFFFFFFFFFFFFFFL : (1L << valueBits) - 1;
+
+ return key ^ (value & valueMask);
+ }
+
+}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/HashValueUtilsTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/HashValueUtilsTest.java
new file mode 100644
index 0000000..1fe36ac
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/HashValueUtilsTest.java
@@ -0,0 +1,23 @@
+package org.projectfloodlight.openflow.types;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class HashValueUtilsTest {
+ @Test
+ public void testBasic() {
+ long key = 0x1234_5678_1234_5678L;
+ long value = 0x8765_4321_8765_4321L;
+ long firstword = 0xFFFF_FFFF_0000_0000L;
+ long secondword = 0x0000_0000_FFFF_FFFFL;
+ long xor = key ^ value;
+
+ assertThat(HashValueUtils.combineWithValue(key, value, 0), equalTo(xor));
+ assertThat(HashValueUtils.combineWithValue(key, value, 64), equalTo(key));
+ assertThat(HashValueUtils.combineWithValue(key, value, 32), equalTo(key & firstword | xor & secondword ));
+ assertThat(HashValueUtils.combineWithValue(key, value, 8), equalTo(0x1251_1559_9551_1559L));
+ }
+
+}