You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by mh...@apache.org on 2007/06/10 04:45:38 UTC
svn commit: r545831 - in /mina/trunk/core/src:
main/java/org/apache/mina/common/ByteBuffer.java
test/java/org/apache/mina/common/ByteBufferTest.java
Author: mheath
Date: Sat Jun 9 19:45:36 2007
New Revision: 545831
URL: http://svn.apache.org/viewvc?view=rev&rev=545831
Log:
Added EnumSet get/put methods to ByteBuffer.
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=545831&r1=545830&r2=545831
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java Sat Jun 9 19:45:36 2007
@@ -38,6 +38,7 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
+import java.util.EnumSet;
import org.apache.mina.common.support.ByteBufferHexDumper;
@@ -1933,6 +1934,123 @@
}
return this;
+ }
+
+ /**
+ * Reads a byte sized bit vector and converts it to an {@link EnumSet}.
+ *
+ * <p>Each bit is mapped to a value in the specified enum. The least significant
+ * bit maps to the first entry in the specified enum and each subsequent bit maps
+ * to each subsequent bit as mapped to the subsequent enum value.</p>
+ *
+ * @param <E> the enum type
+ * @param enumClass the enum class used to create the EnumSet
+ * @return the EnumSet representation of the bit vector
+ */
+ public <E extends Enum<E>> EnumSet<E> getEnumSet(Class<E> enumClass) {
+ return toEnumSet(enumClass, (long) get() & 0xFFL);
+ }
+
+ /**
+ * Reads a short sized bit vector and converts it to an {@link EnumSet}.
+ *
+ * @see #getEnumSet(Class)
+ * @param <E> the enum type
+ * @param enumClass the enum class used to create the EnumSet
+ * @return the EnumSet representation of the bit vector
+ */
+ public <E extends Enum<E>> EnumSet<E> getEnumSetShort(Class<E> enumClass) {
+ return toEnumSet(enumClass, ((long) getShort()) & 0xFFFFL);
+ }
+
+ /**
+ * Reads an int sized bit vector and converts it to an {@link EnumSet}.
+ *
+ * @see #getEnumSet(Class)
+ * @param <E> the enum type
+ * @param enumClass the enum class used to create the EnumSet
+ * @return the EnumSet representation of the bit vector
+ */
+ public <E extends Enum<E>> EnumSet<E> getEnumSetInt(Class<E> enumClass) {
+ return toEnumSet(enumClass, (long) getInt() & 0xFFFFFFFFL);
+ }
+
+ /**
+ * Reads a long sized bit vector and converts it to an {@link EnumSet}.
+ *
+ * @see #getEnumSet(Class)
+ * @param <E> the enum type
+ * @param enumClass the enum class used to create the EnumSet
+ * @return the EnumSet representation of the bit vector
+ */
+ public <E extends Enum<E>> EnumSet<E> getEnumSetLong(Class<E> enumClass) {
+ return toEnumSet(enumClass, getLong());
+ }
+
+ /**
+ * Utility method for converting a bit vector to an EnumSet.
+ */
+ private <E extends Enum<E>> EnumSet<E> toEnumSet(Class<E> clazz, long vector) {
+ EnumSet<E> set = EnumSet.noneOf(clazz);
+ long mask = 1;
+ for (E e : clazz.getEnumConstants()) {
+ if ((mask & vector) == mask) {
+ set.add(e);
+ }
+ mask <<= 1;
+ }
+ return set;
+ }
+
+ /**
+ * Writes the specified {@link EnumSet} to the buffer as a byte sized bit vector.
+ *
+ * @param <E> the enum type of the EnumSet
+ * @param set the enum set to write to the buffer
+ */
+ public <E extends Enum<E>> void putEnumSet(EnumSet<E> set) {
+ put((byte) toLong(set));
+ }
+
+ /**
+ * Writes the specified {@link EnumSet} to the buffer as a short sized bit vector.
+ *
+ * @param <E> the enum type of the EnumSet
+ * @param set the enum set to write to the buffer
+ */
+ public <E extends Enum<E>> void putEnumSetShort(EnumSet<E> set) {
+ putShort((short) toLong(set));
+ }
+
+ /**
+ * Writes the specified {@link EnumSet} to the buffer as an int sized bit vector.
+ *
+ * @param <E> the enum type of the EnumSet
+ * @param set the enum set to write to the buffer
+ */
+ public <E extends Enum<E>> void putEnumSetInt(EnumSet<E> set) {
+ putInt((int) toLong(set));
+ }
+
+ /**
+ * Writes the specified {@link EnumSet} to the buffer as a long sized bit vector.
+ *
+ * @param <E> the enum type of the EnumSet
+ * @param set the enum set to write to the buffer
+ */
+ public <E extends Enum<E>> void putEnumSetLong(EnumSet<E> set) {
+ putLong(toLong(set));
+ }
+
+ /**
+ * Utility method for converting an EnumSet to a bit vector.
+ */
+ private <E extends Enum<E>> long toLong(EnumSet<E> s) {
+ long vector = 0;
+ for (E e : s) {
+ vector |= 1L << e.ordinal();
+ }
+ return vector;
}
/**
Modified: mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=545831&r1=545830&r2=545831
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java (original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java Sat Jun 9 19:45:36 2007
@@ -28,6 +28,7 @@
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.Date;
+import java.util.EnumSet;
import java.util.List;
import junit.framework.Assert;
@@ -724,5 +725,189 @@
Assert.assertEquals( 2, buf.indexOf( ( byte ) 0x3 ) );
Assert.assertEquals( 3, buf.indexOf( ( byte ) 0x4 ) );
}
+ }
+
+ // We need an enum with 64 values
+ private static enum TestEnum {
+ E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16,
+ E17, E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32,
+ E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E77, E48,
+ E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, E63, E64
+ }
+
+ public void testPutEnumSet() {
+ ByteBuffer buf = ByteBuffer.allocate(8);
+
+ // Test empty set
+ buf.putEnumSet(EnumSet.noneOf(TestEnum.class));
+ buf.flip();
+ assertEquals(0, buf.get());
+
+ buf.clear();
+ buf.putEnumSetShort(EnumSet.noneOf(TestEnum.class));
+ buf.flip();
+ assertEquals(0, buf.getShort());
+
+ buf.clear();
+ buf.putEnumSetInt(EnumSet.noneOf(TestEnum.class));
+ buf.flip();
+ assertEquals(0, buf.getInt());
+
+ buf.clear();
+ buf.putEnumSetLong(EnumSet.noneOf(TestEnum.class));
+ buf.flip();
+ assertEquals(0, buf.getLong());
+
+ // Test complete set
+ buf.clear();
+ buf.putEnumSet(EnumSet.range(TestEnum.E1, TestEnum.E8));
+ buf.flip();
+ assertEquals((byte)-1, buf.get());
+
+ buf.clear();
+ buf.putEnumSetShort(EnumSet.range(TestEnum.E1, TestEnum.E16));
+ buf.flip();
+ assertEquals((short)-1, buf.getShort());
+
+ buf.clear();
+ buf.putEnumSetInt(EnumSet.range(TestEnum.E1, TestEnum.E32));
+ buf.flip();
+ assertEquals(-1, buf.getInt());
+
+ buf.clear();
+ buf.putEnumSetLong(EnumSet.allOf(TestEnum.class));
+ buf.flip();
+ assertEquals(-1L, buf.getLong());
+
+ // Test high bit set
+ buf.clear();
+ buf.putEnumSet(EnumSet.of(TestEnum.E8));
+ buf.flip();
+ assertEquals(Byte.MIN_VALUE, buf.get());
+
+ buf.clear();
+ buf.putEnumSetShort(EnumSet.of(TestEnum.E16));
+ buf.flip();
+ assertEquals(Short.MIN_VALUE, buf.getShort());
+
+ buf.clear();
+ buf.putEnumSetInt(EnumSet.of(TestEnum.E32));
+ buf.flip();
+ assertEquals(Integer.MIN_VALUE, buf.getInt());
+
+ buf.clear();
+ buf.putEnumSetLong(EnumSet.of(TestEnum.E64));
+ buf.flip();
+ assertEquals(Long.MIN_VALUE, buf.getLong());
+
+ // Test high low bits set
+ buf.clear();
+ buf.putEnumSet(EnumSet.of(TestEnum.E1, TestEnum.E8));
+ buf.flip();
+ assertEquals(Byte.MIN_VALUE + 1, buf.get());
+
+ buf.clear();
+ buf.putEnumSetShort(EnumSet.of(TestEnum.E1, TestEnum.E16));
+ buf.flip();
+ assertEquals(Short.MIN_VALUE + 1, buf.getShort());
+
+ buf.clear();
+ buf.putEnumSetInt(EnumSet.of(TestEnum.E1, TestEnum.E32));
+ buf.flip();
+ assertEquals(Integer.MIN_VALUE + 1, buf.getInt());
+
+ buf.clear();
+ buf.putEnumSetLong(EnumSet.of(TestEnum.E1, TestEnum.E64));
+ buf.flip();
+ assertEquals(Long.MIN_VALUE + 1, buf.getLong());
+ }
+
+ public void testGetEnumSet() {
+ ByteBuffer buf = ByteBuffer.allocate(8);
+
+ // Test empty set
+ buf.put((byte)0);
+ buf.flip();
+ assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ buf.putShort((short)0);
+ buf.flip();
+ assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ buf.putInt(0);
+ buf.flip();
+ assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ buf.putLong(0L);
+ buf.flip();
+ assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
+
+ // Test complete set
+ buf.clear();
+ buf.put((byte)-1);
+ buf.flip();
+ assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E8), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ buf.putShort((short)-1);
+ buf.flip();
+ assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
+
+ buf.clear();
+ buf.putInt(-1);
+ buf.flip();
+ assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
+
+ buf.clear();
+ buf.putLong(-1L);
+ buf.flip();
+ assertEquals(EnumSet.allOf(TestEnum.class), buf.getEnumSetLong(TestEnum.class));
+
+ // Test high bit set
+ buf.clear();
+ buf.put(Byte.MIN_VALUE);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E8), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ buf.putShort(Short.MIN_VALUE);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
+
+ buf.clear();
+ buf.putInt(Integer.MIN_VALUE);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
+
+ buf.clear();
+ buf.putLong(Long.MIN_VALUE);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E64), buf.getEnumSetLong(TestEnum.class));
+
+ // Test high low bits set
+ buf.clear();
+ byte b = Byte.MIN_VALUE + 1;
+ buf.put(b);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E8), buf.getEnumSet(TestEnum.class));
+
+ buf.clear();
+ short s = Short.MIN_VALUE + 1;
+ buf.putShort(s);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
+
+ buf.clear();
+ buf.putInt(Integer.MIN_VALUE + 1);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
+
+ buf.clear();
+ buf.putLong(Long.MIN_VALUE + 1);
+ buf.flip();
+ assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E64), buf.getEnumSetLong(TestEnum.class));
}
}