commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: svn commit: r1391258 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java
Date Thu, 27 Sep 2012 23:01:35 GMT
On 27 September 2012 23:53,  <joehni@apache.org> wrote:
> Author: joehni
> Date: Thu Sep 27 22:53:46 2012
> New Revision: 1391258
>
> URL: http://svn.apache.org/viewvc?rev=1391258&view=rev
> Log:
> Use conversion tables for boolean arrays.

I think the previous code was much clearer.

At first sight it now looks as though the static arrays are shared
(which would not be safe) but in fact they are cloned.

> Modified:
>     commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java
>
> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java
> URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java?rev=1391258&r1=1391257&r2=1391258&view=diff
> ==============================================================================
> --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java
(original)
> +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Conversion.java
Thu Sep 27 22:53:46 2012
> @@ -18,7 +18,6 @@
>   *******************************************************************************/
>  package org.apache.commons.lang3;
>
> -
>  /**
>   * <p>
>   * Static methods to convert a type into another, with endianness and bit ordering awareness.
> @@ -56,6 +55,24 @@ package org.apache.commons.lang3;
>   */
>
>  public class Conversion {
> +
> +    private static final boolean[] BOOL_FFFF = {false, false, false, false};
> +    private static final boolean[] BOOL_TFFF = {true, false, false, false};
> +    private static final boolean[] BOOL_FTFF = {false, true, false, false};
> +    private static final boolean[] BOOL_TTFF = {true, true, false, false};
> +    private static final boolean[] BOOL_FFTF = {false, false, true, false};
> +    private static final boolean[] BOOL_TFTF = {true, false, true, false};
> +    private static final boolean[] BOOL_FTTF = {false, true, true, false};
> +    private static final boolean[] BOOL_TTTF = {true, true, true, false};
> +    private static final boolean[] BOOL_FFFT = {false, false, false, true};
> +    private static final boolean[] BOOL_TFFT = {true, false, false, true};
> +    private static final boolean[] BOOL_FTFT = {false, true, false, true};
> +    private static final boolean[] BOOL_TTFT = {true, true, false, true};
> +    private static final boolean[] BOOL_FFTT = {false, false, true, true};
> +    private static final boolean[] BOOL_TFTT = {true, false, true, true};
> +    private static final boolean[] BOOL_FTTT = {false, true, true, true};
> +    private static final boolean[] BOOL_TTTT = {true, true, true, true};
> +
>      /**
>       * <p>
>       * Converts a hexadecimal digit into an int using the default (Lsb0) bit ordering.
> @@ -184,50 +201,68 @@ public class Conversion {
>       * @return a boolean array with the binary representation of <code>hexDigit</code>
>       */
>      public static boolean[] hexDigitToBools(char hexDigit) {
> +        final boolean[] array;
>          switch (hexDigit) {
>          case '0':
> -            return new boolean[]{false, false, false, false};
> +            array = BOOL_FFFF;
> +            break;
>          case '1':
> -            return new boolean[]{true, false, false, false};
> +            array = BOOL_TFFF;
> +            break;
>          case '2':
> -            return new boolean[]{false, true, false, false};
> +            array = BOOL_FTFF;
> +            break;
>          case '3':
> -            return new boolean[]{true, true, false, false};
> +            array = BOOL_TTFF;
> +            break;
>          case '4':
> -            return new boolean[]{false, false, true, false};
> +            array = BOOL_FFTF;
> +            break;
>          case '5':
> -            return new boolean[]{true, false, true, false};
> +            array = BOOL_TFTF;
> +            break;
>          case '6':
> -            return new boolean[]{false, true, true, false};
> +            array = BOOL_FTTF;
> +            break;
>          case '7':
> -            return new boolean[]{true, true, true, false};
> +            array = BOOL_TTTF;
> +            break;
>          case '8':
> -            return new boolean[]{false, false, false, true};
> +            array = BOOL_FFFT;
> +            break;
>          case '9':
> -            return new boolean[]{true, false, false, true};
> +            array = BOOL_TFFT;
> +            break;
>          case 'a':// fall through
>          case 'A':
> -            return new boolean[]{false, true, false, true};
> +            array = BOOL_FTFT;
> +            break;
>          case 'b':// fall through
>          case 'B':
> -            return new boolean[]{true, true, false, true};
> +            array = BOOL_TTFT;
> +            break;
>          case 'c':// fall through
>          case 'C':
> -            return new boolean[]{false, false, true, true};
> +            array = BOOL_FFTT;
> +            break;
>          case 'd':// fall through
>          case 'D':
> -            return new boolean[]{true, false, true, true};
> +            array = BOOL_TFTT;
> +            break;
>          case 'e':// fall through
>          case 'E':
> -            return new boolean[]{false, true, true, true};
> +            array = BOOL_FTTT;
> +            break;
>          case 'f':// fall through
>          case 'F':
> -            return new boolean[]{true, true, true, true};
> +            array = BOOL_TTTT;
> +            break;
>          default:
>              throw new IllegalArgumentException("Cannot interpret '"
>                  + hexDigit
>                  + "' as a hexadecimal digit");
>          }
> +        return array.clone();
>      }
>
>      /**
> @@ -242,50 +277,68 @@ public class Conversion {
>       * @return a boolean array with the binary representation of <code>hexDigit</code>
>       */
>      public static boolean[] hexDigitM0ToBools(char hexDigit) {
> +        final boolean[] array;
>          switch (hexDigit) {
>          case '0':
> -            return new boolean[]{false, false, false, false};
> +            array = BOOL_FFFF;
> +            break;
>          case '1':
> -            return new boolean[]{false, false, false, true};
> +            array = BOOL_FFFT;
> +            break;
>          case '2':
> -            return new boolean[]{false, false, true, false};
> +            array = BOOL_FFTF;
> +            break;
>          case '3':
> -            return new boolean[]{false, false, true, true};
> +            array = BOOL_FFTT;
> +            break;
>          case '4':
> -            return new boolean[]{false, true, false, false};
> +            array = BOOL_FTFF;
> +            break;
>          case '5':
> -            return new boolean[]{false, true, false, true};
> +            array = BOOL_FTFT;
> +            break;
>          case '6':
> -            return new boolean[]{false, true, true, false};
> +            array = BOOL_FTTF;
> +            break;
>          case '7':
> -            return new boolean[]{false, true, true, true};
> +            array = BOOL_FTTT;
> +            break;
>          case '8':
> -            return new boolean[]{true, false, false, false};
> +            array = BOOL_TFFF;
> +            break;
>          case '9':
> -            return new boolean[]{true, false, false, true};
> +            array = BOOL_TFFT;
> +            break;
>          case 'a':// fall through
>          case 'A':
> -            return new boolean[]{true, false, true, false};
> +            array = BOOL_TFTF;
> +            break;
>          case 'b':// fall through
>          case 'B':
> -            return new boolean[]{true, false, true, true};
> +            array = BOOL_TFTT;
> +            break;
>          case 'c':// fall through
>          case 'C':
> -            return new boolean[]{true, true, false, false};
> +            array = BOOL_TTFF;
> +            break;
>          case 'd':// fall through
>          case 'D':
> -            return new boolean[]{true, true, false, true};
> +            array = BOOL_TTFT;
> +            break;
>          case 'e':// fall through
>          case 'E':
> -            return new boolean[]{true, true, true, false};
> +            array = BOOL_TTTF;
> +            break;
>          case 'f':// fall through
>          case 'F':
> -            return new boolean[]{true, true, true, true};
> +            array = BOOL_TTTT;
> +            break;
>          default:
>              throw new IllegalArgumentException("Cannot interpret '"
>                  + hexDigit
>                  + "' as a hexadecimal digit");
>          }
> +        return array.clone();
>      }
>
>      /**
> @@ -562,8 +615,7 @@ public class Conversion {
>          case 0xF:
>              return 'F';
>          default:
> -            throw new IllegalArgumentException("nibble value not between 0 and 15: "
> -                + nibble);
> +            throw new IllegalArgumentException("nibble value not between 0 and 15: "
+ nibble);
>          }
>      }
>
> @@ -619,8 +671,7 @@ public class Conversion {
>          case 0xF:
>              return 'F';
>          default:
> -            throw new IllegalArgumentException("nibble value not between 0 and 15: "
> -                + nibble);
> +            throw new IllegalArgumentException("nibble value not between 0 and 15: "
+ nibble);
>          }
>      }
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message