Return-Path: Delivered-To: apmail-mina-commits-archive@locus.apache.org Received: (qmail 863 invoked from network); 12 Jun 2007 02:07:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Jun 2007 02:07:51 -0000 Received: (qmail 75297 invoked by uid 500); 12 Jun 2007 02:07:55 -0000 Delivered-To: apmail-mina-commits-archive@mina.apache.org Received: (qmail 75247 invoked by uid 500); 12 Jun 2007 02:07:55 -0000 Mailing-List: contact commits-help@mina.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@mina.apache.org Delivered-To: mailing list commits@mina.apache.org Received: (qmail 75237 invoked by uid 99); 12 Jun 2007 02:07:55 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2007 19:07:55 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2007 19:07:50 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 7DEFF1A981A; Mon, 11 Jun 2007 19:07:30 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r546356 - /mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java Date: Tue, 12 Jun 2007 02:07:30 -0000 To: commits@mina.apache.org From: trustin@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070612020730.7DEFF1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: trustin Date: Mon Jun 11 19:07:29 2007 New Revision: 546356 URL: http://svn.apache.org/viewvc?view=rev&rev=546356 Log: * Applied invocation chaining to get/putEnum methods * Added random access version of get/putEnumSet methods Modified: mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.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=546356&r1=546355&r2=546356 ============================================================================== --- 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 Mon Jun 11 19:07:29 2007 @@ -1953,6 +1953,19 @@ } /** + * Reads a byte from the buffer and returns the correlating enum constant defined + * by the specified enum type. + * + * @param The enum type to return + * @param index the index from which the byte will be read + * @param enumClass The enum's class object + * @return + */ + public > E getEnum(int index, Class enumClass) { + return toEnum(enumClass, get(index)); + } + + /** * Reads a short from the buffer and returns the correlating enum constant defined * by the specified enum type. * @@ -1965,6 +1978,19 @@ } /** + * Reads a short from the buffer and returns the correlating enum constant defined + * by the specified enum type. + * + * @param The enum type to return + * @param index the index from which the bytes will be read + * @param enumClass The enum's class object + * @return + */ + public > E getEnumShort(int index, Class enumClass) { + return toEnum(enumClass, getShort(index)); + } + + /** * Reads an int from the buffer and returns the correlating enum constant defined * by the specified enum type. * @@ -1977,15 +2003,41 @@ } /** + * Reads an int from the buffer and returns the correlating enum constant defined + * by the specified enum type. + * + * @param The enum type to return + * @param index the index from which the bytes will be read + * @param enumClass The enum's class object + * @return + */ + public > E getEnumInt(int index, Class enumClass) { + return toEnum(enumClass, getInt(index)); + } + + /** * Writes an enum's ordinal value to the buffer as a byte. * * @param e The enum to write to the buffer */ - public void putEnum(Enum e) { + public ByteBuffer putEnum(Enum e) { + if (e.ordinal() > Byte.MAX_VALUE) { + throw new IllegalArgumentException(enumConversionErrorMessage(e, "byte")); + } + return put((byte)e.ordinal()); + } + + /** + * Writes an enum's ordinal value to the buffer as a byte. + * + * @param index The index at which the byte will be written + * @param e The enum to write to the buffer + */ + public ByteBuffer putEnum(int index, Enum e) { if (e.ordinal() > Byte.MAX_VALUE) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "byte")); } - put((byte)e.ordinal()); + return put(index, (byte)e.ordinal()); } /** @@ -1993,11 +2045,24 @@ * * @param e The enum to write to the buffer */ - public void putEnumShort(Enum e) { + public ByteBuffer putEnumShort(Enum e) { + if (e.ordinal() > Short.MAX_VALUE) { + throw new IllegalArgumentException(enumConversionErrorMessage(e, "short")); + } + return putShort((short)e.ordinal()); + } + + /** + * Writes an enum's ordinal value to the buffer as a short. + * + * @param index The index at which the bytes will be written + * @param e The enum to write to the buffer + */ + public ByteBuffer putEnumShort(int index, Enum e) { if (e.ordinal() > Short.MAX_VALUE) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "short")); } - putShort((short)e.ordinal()); + return putShort(index, (short)e.ordinal()); } /** @@ -2005,8 +2070,18 @@ * * @param e The enum to write to the buffer */ - public void putEnumInt(Enum e) { - putInt(e.ordinal()); + public ByteBuffer putEnumInt(Enum e) { + return putInt(e.ordinal()); + } + + /** + * Writes an enum's ordinal value to the buffer as an integer. + * + * @param index The index at which the bytes will be written + * @param e The enum to write to the buffer + */ + public ByteBuffer putEnumInt(int index, Enum e) { + return putInt(index, e.ordinal()); } private E toEnum(Class enumClass, int i) { @@ -2045,6 +2120,19 @@ } /** + * Reads a byte sized bit vector and converts it to an {@link EnumSet}. + * + * @see #getEnumSet(Class) + * @param the enum type + * @param index the index from which the byte will be read + * @param enumClass the enum class used to create the EnumSet + * @return the EnumSet representation of the bit vector + */ + public > EnumSet getEnumSet(int index, Class enumClass) { + return toEnumSet(enumClass, get(index) & BYTE_MASK); + } + + /** * Reads a short sized bit vector and converts it to an {@link EnumSet}. * * @see #getEnumSet(Class) @@ -2057,6 +2145,19 @@ } /** + * Reads a short sized bit vector and converts it to an {@link EnumSet}. + * + * @see #getEnumSet(Class) + * @param the enum type + * @param index the index from which the bytes will be read + * @param enumClass the enum class used to create the EnumSet + * @return the EnumSet representation of the bit vector + */ + public > EnumSet getEnumSetShort(int index, Class enumClass) { + return toEnumSet(enumClass, getShort(index) & SHORT_MASK); + } + + /** * Reads an int sized bit vector and converts it to an {@link EnumSet}. * * @see #getEnumSet(Class) @@ -2069,6 +2170,19 @@ } /** + * Reads an int sized bit vector and converts it to an {@link EnumSet}. + * + * @see #getEnumSet(Class) + * @param the enum type + * @param index the index from which the bytes will be read + * @param enumClass the enum class used to create the EnumSet + * @return the EnumSet representation of the bit vector + */ + public > EnumSet getEnumSetInt(int index, Class enumClass) { + return toEnumSet(enumClass, getInt(index) & INT_MASK); + } + + /** * Reads a long sized bit vector and converts it to an {@link EnumSet}. * * @see #getEnumSet(Class) @@ -2081,6 +2195,19 @@ } /** + * Reads a long sized bit vector and converts it to an {@link EnumSet}. + * + * @see #getEnumSet(Class) + * @param the enum type + * @param index the index from which the bytes will be read + * @param enumClass the enum class used to create the EnumSet + * @return the EnumSet representation of the bit vector + */ + public > EnumSet getEnumSetLong(int index, Class enumClass) { + return toEnumSet(enumClass, getLong(index)); + } + + /** * Utility method for converting a bit vector to an EnumSet. */ private > EnumSet toEnumSet(Class clazz, long vector) { @@ -2106,8 +2233,22 @@ if ((vector & ~BYTE_MASK) != 0) { throw new IllegalArgumentException("The enum set is too large to fit in a byte: " + set); } - put((byte) vector); - return this; + return put((byte) vector); + } + + /** + * Writes the specified {@link EnumSet} to the buffer as a byte sized bit vector. + * + * @param the enum type of the EnumSet + * @param index the index at which the byte will be written + * @param set the enum set to write to the buffer + */ + public > ByteBuffer putEnumSet(int index, EnumSet set) { + long vector = toLong(set); + if ((vector & ~BYTE_MASK) != 0) { + throw new IllegalArgumentException("The enum set is too large to fit in a byte: " + set); + } + return put(index, (byte) vector); } /** @@ -2121,8 +2262,22 @@ if ((vector & ~SHORT_MASK) != 0) { throw new IllegalArgumentException("The enum set is too large to fit in a short: " + set); } - putShort((short) vector); - return this; + return putShort((short) vector); + } + + /** + * Writes the specified {@link EnumSet} to the buffer as a short sized bit vector. + * + * @param the enum type of the EnumSet + * @param index the index at which the bytes will be written + * @param set the enum set to write to the buffer + */ + public > ByteBuffer putEnumSetShort(int index, EnumSet set) { + long vector = toLong(set); + if ((vector & ~SHORT_MASK) != 0) { + throw new IllegalArgumentException("The enum set is too large to fit in a short: " + set); + } + return putShort(index, (short) vector); } /** @@ -2136,8 +2291,22 @@ if ((vector & ~INT_MASK) != 0) { throw new IllegalArgumentException("The enum set is too large to fit in an int: " + set); } - putInt((int) vector); - return this; + return putInt((int) vector); + } + + /** + * Writes the specified {@link EnumSet} to the buffer as an int sized bit vector. + * + * @param the enum type of the EnumSet + * @param index the index at which the bytes will be written + * @param set the enum set to write to the buffer + */ + public > ByteBuffer putEnumSetInt(int index, EnumSet set) { + long vector = toLong(set); + if ((vector & ~INT_MASK) != 0) { + throw new IllegalArgumentException("The enum set is too large to fit in an int: " + set); + } + return putInt(index, (int) vector); } /** @@ -2147,8 +2316,18 @@ * @param set the enum set to write to the buffer */ public > ByteBuffer putEnumSetLong(EnumSet set) { - putLong(toLong(set)); - return this; + return putLong(toLong(set)); + } + + /** + * Writes the specified {@link EnumSet} to the buffer as a long sized bit vector. + * + * @param the enum type of the EnumSet + * @param index the index at which the bytes will be written + * @param set the enum set to write to the buffer + */ + public > ByteBuffer putEnumSetLong(int index, EnumSet set) { + return putLong(index, toLong(set)); } /**