Code clean-up for CheckStyle.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1393626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joerg Schaible 2012-10-03 17:17:40 +00:00
parent 28476afb7d
commit ca9cbf976e
2 changed files with 355 additions and 226 deletions

View File

@ -20,6 +20,7 @@ package org.apache.commons.lang3;
import java.util.UUID;
/**
* <p>
* Static methods to convert a type into another, with endianness and bit ordering awareness.
@ -288,42 +289,67 @@ public class Conversion {
* @throws NullPointerException if {@code src} is {@code null}
*/
public static char boolsToHexDigit(boolean[] src, int srcPos) {
if (src.length == 0)
if (src.length == 0) {
throw new IllegalArgumentException("Cannot convert empty array.");
}
if (src.length > srcPos + 3 && src[srcPos + 3]) {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src[srcPos]) return 'f';
else return 'e';
if (src[srcPos]) {
return 'f';
} else {
if (src[srcPos]) return 'd';
else return 'c';
return 'e';
}
} else {
if (src[srcPos]) {
return 'd';
} else {
return 'c';
}
}
} else {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src[srcPos]) return 'b';
else return 'a';
if (src[srcPos]) {
return 'b';
} else {
if (src[srcPos]) return '9';
else return '8';
return 'a';
}
} else {
if (src[srcPos]) {
return '9';
} else {
return '8';
}
}
}
} else {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src[srcPos]) return '7';
else return '6';
if (src[srcPos]) {
return '7';
} else {
if (src[srcPos]) return '5';
else return '4';
return '6';
}
} else {
if (src[srcPos]) {
return '5';
} else {
return '4';
}
}
} else {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src[srcPos]) return '3';
else return '2';
if (src[srcPos]) {
return '3';
} else {
if (src[srcPos]) return '1';
else return '0';
return '2';
}
} else {
if (src[srcPos]) {
return '1';
} else {
return '0';
}
}
}
}
@ -339,7 +365,8 @@ public class Conversion {
*
* @param src the boolean array to convert
* @return a hexadecimal digit representing the selected bits
* @throws IllegalArgumentException if {@code src} is empty, {@code src.length < 4} or {@code src.length > 8}
* @throws IllegalArgumentException if {@code src} is empty, {@code src.length < 4} or
* {@code src.length > 8}
* @throws NullPointerException if {@code src} is {@code null}
*/
public static char boolsToHexDigitM0_4bits(boolean[] src) {
@ -358,51 +385,78 @@ public class Conversion {
* @param src the boolean array to convert
* @param srcPos the position of the lsb to start the conversion
* @return a hexadecimal digit representing the selected bits
* @throws IllegalArgumentException if {@code src} is empty, {@code src.length > 8} or {@code src.length - srcPos < 4}
* @throws IllegalArgumentException if {@code src} is empty, {@code src.length > 8} or
* {@code src.length - srcPos < 4}
* @throws NullPointerException if {@code src} is {@code null}
*/
public static char boolsToHexDigitM0_4bits(boolean[] src, int srcPos) {
if (src.length > 8)
if (src.length > 8) {
throw new IllegalArgumentException("src.length>8: src.length=" + src.length);
if (src.length - srcPos < 4)
}
if (src.length - srcPos < 4) {
throw new IllegalArgumentException("src.length-srcPos<4: src.length="
+ src.length
+ ", srcPos="
+ srcPos);
}
if (src[srcPos + 3]) {
if (src[srcPos + 2]) {
if (src[srcPos + 1]) {
if (src[srcPos]) return 'f';
else return '7';
if (src[srcPos]) {
return 'f';
} else {
if (src[srcPos]) return 'b';
else return '3';
return '7';
}
} else {
if (src[srcPos]) {
return 'b';
} else {
return '3';
}
}
} else {
if (src[srcPos + 1]) {
if (src[srcPos]) return 'd';
else return '5';
if (src[srcPos]) {
return 'd';
} else {
if (src[srcPos]) return '9';
else return '1';
return '5';
}
} else {
if (src[srcPos]) {
return '9';
} else {
return '1';
}
}
}
} else {
if (src[srcPos + 2]) {
if (src[srcPos + 1]) {
if (src[srcPos]) return 'e';
else return '6';
if (src[srcPos]) {
return 'e';
} else {
if (src[srcPos]) return 'a';
else return '2';
return '6';
}
} else {
if (src[srcPos]) {
return 'a';
} else {
return '2';
}
}
} else {
if (src[srcPos + 1]) {
if (src[srcPos]) return 'c';
else return '4';
if (src[srcPos]) {
return 'c';
} else {
if (src[srcPos]) return '8';
else return '0';
return '4';
}
} else {
if (src[srcPos]) {
return '8';
} else {
return '0';
}
}
}
}
@ -444,8 +498,9 @@ public class Conversion {
* @throws NullPointerException if {@code src} is {@code null}
*/
public static char boolsBeM0ToHexDigit(boolean[] src, int srcPos) {
if (src.length == 0)
if (src.length == 0) {
throw new IllegalArgumentException("Cannot convert empty array.");
}
int beSrcPos = src.length - 1 - srcPos;
int srcLen = Math.min(4, beSrcPos + 1);
boolean[] paddedSrc = new boolean[4];
@ -455,37 +510,61 @@ public class Conversion {
if (src[srcPos]) {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 3 && src[srcPos + 3]) return 'f';
else return 'e';
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return 'f';
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) return 'd';
else return 'c';
return 'e';
}
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return 'd';
} else {
return 'c';
}
}
} else {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 3 && src[srcPos + 3]) return 'b';
else return 'a';
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return 'b';
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) return '9';
else return '8';
return 'a';
}
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return '9';
} else {
return '8';
}
}
}
} else {
if (src.length > srcPos + 1 && src[srcPos + 1]) {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 3 && src[srcPos + 3]) return '7';
else return '6';
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return '7';
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) return '5';
else return '4';
return '6';
}
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return '5';
} else {
return '4';
}
}
} else {
if (src.length > srcPos + 2 && src[srcPos + 2]) {
if (src.length > srcPos + 3 && src[srcPos + 3]) return '3';
else return '2';
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return '3';
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) return '1';
else return '0';
return '2';
}
} else {
if (src.length > srcPos + 3 && src[srcPos + 3]) {
return '1';
} else {
return '0';
}
}
}
}
@ -511,8 +590,9 @@ public class Conversion {
*/
public static char intToHexDigit(int nibble) {
char c = Character.forDigit(nibble, 16);
if (c == Character.MIN_VALUE)
if (c == Character.MIN_VALUE) {
throw new IllegalArgumentException("nibble value not between 0 and 15: " + nibble);
}
return c;
}
@ -589,11 +669,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nInts-1)*32+dstPos >= 64}
*/
public static long intsToLong(int[] src, int srcPos, long dstInit, int dstPos, int nInts) {
if (0 == nInts)
if (0 == nInts) {
return dstInit;
if ((nInts-1)*32+dstPos >= 64)
}
if ((nInts - 1) * 32 + dstPos >= 64) {
throw new IllegalArgumentException(
"(nInts-1)*32+dstPos is greather or equal to than 64");
}
long out = dstInit;
int shift = 0;
for (int i = 0; i < nInts; i++ ) {
@ -622,11 +704,13 @@ public class Conversion {
*/
public static long shortsToLong(short[] src, int srcPos, long dstInit, int dstPos,
int nShorts) {
if (0 == nShorts)
if (0 == nShorts) {
return dstInit;
if ((nShorts-1)*16+dstPos >= 64)
}
if ((nShorts - 1) * 16 + dstPos >= 64) {
throw new IllegalArgumentException(
"(nShorts-1)*16+dstPos is greather or equal to than 64");
}
long out = dstInit;
int shift = 0;
for (int i = 0; i < nShorts; i++ ) {
@ -654,11 +738,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nShorts-1)*16+dstPos >= 32}
*/
public static int shortsToInt(short[] src, int srcPos, int dstInit, int dstPos, int nShorts) {
if (0 == nShorts)
if (0 == nShorts) {
return dstInit;
if ((nShorts-1)*16+dstPos >= 32)
}
if ((nShorts - 1) * 16 + dstPos >= 32) {
throw new IllegalArgumentException(
"(nShorts-1)*16+dstPos is greather or equal to than 32");
}
int out = dstInit;
int shift = 0;
for (int i = 0; i < nShorts; i++ ) {
@ -686,11 +772,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nBytes-1)*8+dstPos >= 64}
*/
public static long bytesToLong(byte[] src, int srcPos, long dstInit, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dstInit;
if ((nBytes-1)*8+dstPos >= 64)
}
if ((nBytes - 1) * 8 + dstPos >= 64) {
throw new IllegalArgumentException(
"(nBytes-1)*8+dstPos is greather or equal to than 64");
}
long out = dstInit;
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
@ -718,11 +806,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nBytes-1)*8+dstPos >= 32}
*/
public static int bytesToInt(byte[] src, int srcPos, int dstInit, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dstInit;
if ((nBytes-1)*8+dstPos >= 32)
}
if ((nBytes - 1) * 8 + dstPos >= 32) {
throw new IllegalArgumentException(
"(nBytes-1)*8+dstPos is greather or equal to than 32");
}
int out = dstInit;
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
@ -751,11 +841,13 @@ public class Conversion {
*/
public static short bytesToShort(byte[] src, int srcPos, short dstInit, int dstPos,
int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dstInit;
if ((nBytes-1)*8+dstPos >= 16)
}
if ((nBytes - 1) * 8 + dstPos >= 16) {
throw new IllegalArgumentException(
"(nBytes-1)*8+dstPos is greather or equal to than 16");
}
short out = dstInit;
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
@ -783,11 +875,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nHexs-1)*4+dstPos >= 64}
*/
public static long hexsToLong(String src, int srcPos, long dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs-1)*4+dstPos >= 64)
}
if ((nHexs - 1) * 4 + dstPos >= 64) {
throw new IllegalArgumentException(
"(nHexs-1)*4+dstPos is greather or equal to than 64");
}
long out = dstInit;
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -815,11 +909,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nHexs-1)*4+dstPos >= 32}
*/
public static int hexsToInt(String src, int srcPos, int dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs-1)*4+dstPos >= 32)
}
if ((nHexs - 1) * 4 + dstPos >= 32) {
throw new IllegalArgumentException(
"(nHexs-1)*4+dstPos is greather or equal to than 32");
}
int out = dstInit;
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -847,11 +943,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nHexs-1)*4+dstPos >= 16}
*/
public static short hexsToShort(String src, int srcPos, short dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs-1)*4+dstPos >= 16)
}
if ((nHexs - 1) * 4 + dstPos >= 16) {
throw new IllegalArgumentException(
"(nHexs-1)*4+dstPos is greather or equal to than 16");
}
short out = dstInit;
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -879,11 +977,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code (nHexs-1)*4+dstPos >= 8}
*/
public static byte hexsToByte(String src, int srcPos, byte dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs-1)*4+dstPos >= 8)
}
if ((nHexs - 1) * 4 + dstPos >= 8) {
throw new IllegalArgumentException(
"(nHexs-1)*4+dstPos is greather or equal to than 8");
}
byte out = dstInit;
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -912,11 +1012,13 @@ public class Conversion {
*/
public static long boolsToLong(boolean[] src, int srcPos, long dstInit, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dstInit;
if (nBools-1+dstPos >= 64)
}
if (nBools - 1 + dstPos >= 64) {
throw new IllegalArgumentException(
"nBools-1+dstPos is greather or equal to than 64");
}
long out = dstInit;
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
@ -944,11 +1046,13 @@ public class Conversion {
* @throws IllegalArgumentException if {@code nBools-1+dstPos >= 32}
*/
public static int boolsToInt(boolean[] src, int srcPos, int dstInit, int dstPos, int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dstInit;
if (nBools-1+dstPos >= 32)
}
if (nBools - 1 + dstPos >= 32) {
throw new IllegalArgumentException(
"nBools-1+dstPos is greather or equal to than 32");
}
int out = dstInit;
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
@ -977,11 +1081,13 @@ public class Conversion {
*/
public static short boolsToShort(boolean[] src, int srcPos, short dstInit, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dstInit;
if (nBools-1+dstPos >= 16)
}
if (nBools - 1 + dstPos >= 16) {
throw new IllegalArgumentException(
"nBools-1+dstPos is greather or equal to than 16");
}
short out = dstInit;
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
@ -1010,11 +1116,12 @@ public class Conversion {
*/
public static byte boolsToByte(boolean[] src, int srcPos, byte dstInit, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dstInit;
if (nBools-1+dstPos >= 8)
throw new IllegalArgumentException(
"nBools-1+dstPos is greather or equal to than 8");
}
if (nBools - 1 + dstPos >= 8) {
throw new IllegalArgumentException("nBools-1+dstPos is greather or equal to than 8");
}
byte out = dstInit;
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
@ -1033,21 +1140,22 @@ public class Conversion {
* </p>
*
* @param src the long to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nInts the number of ints to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nInts the number of ints to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nInts-1)*32+srcPos >= 64}
*/
public static int[] longToInts(long src, int srcPos, int[] dst, int dstPos, int nInts) {
if (0 == nInts)
if (0 == nInts) {
return dst;
if ((nInts - 1) * 32 + srcPos >= 64)
}
if ((nInts - 1) * 32 + srcPos >= 64) {
throw new IllegalArgumentException(
"(nInts-1)*32+srcPos is greather or equal to than 64");
}
int shift = 0;
for (int i = 0; i < nInts; i++ ) {
shift = i * 32 + srcPos;
@ -1063,22 +1171,23 @@ public class Conversion {
* </p>
*
* @param src the long to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nShorts the number of shorts to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nShorts the number of shorts to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nShorts-1)*16+srcPos >= 64}
*/
public static short[] longToShorts(long src, int srcPos, short[] dst, int dstPos,
int nShorts) {
if (0 == nShorts)
if (0 == nShorts) {
return dst;
if ((nShorts - 1) * 16 + srcPos >= 64)
}
if ((nShorts - 1) * 16 + srcPos >= 64) {
throw new IllegalArgumentException(
"(nShorts-1)*16+srcPos is greather or equal to than 64");
}
int shift = 0;
for (int i = 0; i < nShorts; i++ ) {
shift = i * 16 + srcPos;
@ -1094,21 +1203,22 @@ public class Conversion {
* </p>
*
* @param src the int to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nShorts the number of shorts to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nShorts the number of shorts to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nShorts-1)*16+srcPos >= 32}
*/
public static short[] intToShorts(int src, int srcPos, short[] dst, int dstPos, int nShorts) {
if (0 == nShorts)
if (0 == nShorts) {
return dst;
if ((nShorts - 1) * 16 + srcPos >= 32)
}
if ((nShorts - 1) * 16 + srcPos >= 32) {
throw new IllegalArgumentException(
"(nShorts-1)*16+srcPos is greather or equal to than 32");
}
int shift = 0;
for (int i = 0; i < nShorts; i++ ) {
shift = i * 16 + srcPos;
@ -1124,21 +1234,22 @@ public class Conversion {
* </p>
*
* @param src the long to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nBytes-1)*8+srcPos >= 64}
*/
public static byte[] longToBytes(long src, int srcPos, byte[] dst, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dst;
if ((nBytes - 1) * 8 + srcPos >= 64)
}
if ((nBytes - 1) * 8 + srcPos >= 64) {
throw new IllegalArgumentException(
"(nBytes-1)*8+srcPos is greather or equal to than 64");
}
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
shift = i * 8 + srcPos;
@ -1154,21 +1265,22 @@ public class Conversion {
* </p>
*
* @param src the int to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nBytes-1)*8+srcPos >= 32}
*/
public static byte[] intToBytes(int src, int srcPos, byte[] dst, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dst;
if ((nBytes - 1) * 8 + srcPos >= 32)
}
if ((nBytes - 1) * 8 + srcPos >= 32) {
throw new IllegalArgumentException(
"(nBytes-1)*8+srcPos is greather or equal to than 32");
}
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
shift = i * 8 + srcPos;
@ -1184,21 +1296,22 @@ public class Conversion {
* </p>
*
* @param src the short to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nBytes-1)*8+srcPos >= 16}
*/
public static byte[] shortToBytes(short src, int srcPos, byte[] dst, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dst;
if ((nBytes - 1) * 8 + srcPos >= 16)
}
if ((nBytes - 1) * 8 + srcPos >= 16) {
throw new IllegalArgumentException(
"(nBytes-1)*8+srcPos is greather or equal to than 16");
}
int shift = 0;
for (int i = 0; i < nBytes; i++ ) {
shift = i * 8 + srcPos;
@ -1214,21 +1327,22 @@ public class Conversion {
* </p>
*
* @param src the long to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dstInit the initial value for the result String
* @param dstPos the position in {@code dst} where to copy the result
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nHexs-1)*4+srcPos >= 64}
*/
public static String longToHexs(long src, int srcPos, String dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs - 1) * 4 + srcPos >= 64)
}
if ((nHexs - 1) * 4 + srcPos >= 64) {
throw new IllegalArgumentException(
"(nHexs-1)*4+srcPos is greather or equal to than 64");
}
StringBuilder sb = new StringBuilder(dstInit);
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -1246,21 +1360,22 @@ public class Conversion {
* </p>
*
* @param src the int to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dstInit the initial value for the result String
* @param dstPos the position in {@code dst} where to copy the result
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nHexs-1)*4+srcPos >= 32}
*/
public static String intToHexs(int src, int srcPos, String dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs - 1) * 4 + srcPos >= 32)
}
if ((nHexs - 1) * 4 + srcPos >= 32) {
throw new IllegalArgumentException(
"(nHexs-1)*4+srcPos is greather or equal to than 32");
}
StringBuilder sb = new StringBuilder(dstInit);
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -1278,22 +1393,23 @@ public class Conversion {
* </p>
*
* @param src the short to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dstInit the initial value for the result String
* @param dstPos the position in {@code dst} where to copy the result
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nHexs-1)*4+srcPos >= 16}
*/
public static String shortToHexs(short src, int srcPos, String dstInit, int dstPos,
int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs - 1) * 4 + srcPos >= 16)
}
if ((nHexs - 1) * 4 + srcPos >= 16) {
throw new IllegalArgumentException(
"(nHexs-1)*4+srcPos is greather or equal to than 16");
}
StringBuilder sb = new StringBuilder(dstInit);
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -1311,21 +1427,22 @@ public class Conversion {
* </p>
*
* @param src the byte to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dstInit the initial value for the result String
* @param dstPos the position in {@code dst} where to copy the result
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nHexs the number of Chars to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code (nHexs-1)*4+srcPos >= 8}
*/
public static String byteToHexs(byte src, int srcPos, String dstInit, int dstPos, int nHexs) {
if (0 == nHexs)
if (0 == nHexs) {
return dstInit;
if ((nHexs - 1) * 4 + srcPos >= 8)
}
if ((nHexs - 1) * 4 + srcPos >= 8) {
throw new IllegalArgumentException(
"(nHexs-1)*4+srcPos is greather or equal to than 8");
}
StringBuilder sb = new StringBuilder(dstInit);
int shift = 0;
for (int i = 0; i < nHexs; i++ ) {
@ -1343,22 +1460,23 @@ public class Conversion {
* </p>
*
* @param src the long to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code nBools-1+srcPos >= 64}
*/
public static boolean[] longToBools(long src, int srcPos, boolean[] dst, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dst;
if (nBools - 1 + srcPos >= 64)
}
if (nBools - 1 + srcPos >= 64) {
throw new IllegalArgumentException(
"nBools-1+srcPos is greather or equal to than 64");
}
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
shift = i * 1 + srcPos;
@ -1374,22 +1492,23 @@ public class Conversion {
* </p>
*
* @param src the int to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code nBools-1+srcPos >= 32}
*/
public static boolean[] intToBools(int src, int srcPos, boolean[] dst, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dst;
if (nBools - 1 + srcPos >= 32)
}
if (nBools - 1 + srcPos >= 32) {
throw new IllegalArgumentException(
"nBools-1+srcPos is greather or equal to than 32");
}
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
shift = i * 1 + srcPos;
@ -1405,22 +1524,23 @@ public class Conversion {
* </p>
*
* @param src the short to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code nBools-1+srcPos >= 16}
*/
public static boolean[] shortToBools(short src, int srcPos, boolean[] dst, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dst;
if (nBools - 1 + srcPos >= 16)
}
if (nBools - 1 + srcPos >= 16) {
throw new IllegalArgumentException(
"nBools-1+srcPos is greather or equal to than 16");
}
int shift = 0;
assert ((nBools - 1) * 1 < 16 - srcPos);
for (int i = 0; i < nBools; i++ ) {
@ -1437,22 +1557,22 @@ public class Conversion {
* </p>
*
* @param src the byte to convert
* @param srcPos the position in {@code src}, in bits, from where to start the
* conversion
* @param srcPos the position in {@code src}, in bits, from where to start the conversion
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal
* to the width of the input (from srcPos to msb)
* @param nBools the number of booleans to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code nBools-1+srcPos >= 8}
*/
public static boolean[] byteToBools(byte src, int srcPos, boolean[] dst, int dstPos,
int nBools) {
if (0 == nBools)
if (0 == nBools) {
return dst;
if (nBools - 1 + srcPos >= 8)
throw new IllegalArgumentException(
"nBools-1+srcPos is greather or equal to than 8");
}
if (nBools - 1 + srcPos >= 8) {
throw new IllegalArgumentException("nBools-1+srcPos is greather or equal to than 8");
}
int shift = 0;
for (int i = 0; i < nBools; i++ ) {
shift = i * 1 + srcPos;
@ -1463,43 +1583,49 @@ public class Conversion {
/**
* <p>
* Converts UUID into an array of byte using the default (little endian, Lsb0) byte and
* bit ordering.
* Converts UUID into an array of byte using the default (little endian, Lsb0) byte and bit
* ordering.
* </p>
*
* @param src the UUID to convert
* @param dst the destination array
* @param dstPos the position in {@code dst} where to copy the result
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to
* the width of the input (from srcPos to msb)
* @param nBytes the number of bytes to copy to {@code dst}, must be smaller or equal to the
* width of the input (from srcPos to msb)
* @return {@code dst}
* @throws IllegalArgumentException if {@code nBytes > 16}
*/
public static byte[] uuidToBytes(UUID src, byte[] dst, int dstPos, int nBytes) {
if (0 == nBytes)
if (0 == nBytes) {
return dst;
if (nBytes > 16 )
}
if (nBytes > 16) {
throw new IllegalArgumentException("nBytes is greather than 16");
}
longToBytes(src.getMostSignificantBits(), 0, dst, dstPos, nBytes > 8 ? 8 : nBytes);
if (nBytes >= 8)
longToBytes(src.getLeastSignificantBits(), 0, dst, dstPos+8, nBytes-8);
if (nBytes >= 8) {
longToBytes(src.getLeastSignificantBits(), 0, dst, dstPos + 8, nBytes - 8);
}
return dst;
}
/**
* <p>
* Converts bytes fomr an array into a UUID using the default (little endian, Lsb0) byte and
* Converts bytes from an array into a UUID using the default (little endian, Lsb0) byte and
* bit ordering.
* </p>
*
* @param src the source byte array
* @param srcPos the position in {@code src} where to copy the result from
* @return a UUID
* @throws IllegalArgumentException if array does not contain at least 16 bytes beginning with {@code srcPos}
* @throws IllegalArgumentException if array does not contain at least 16 bytes beginning
* with {@code srcPos}
*/
public static UUID bytesToUuid(byte[] src, int srcPos) {
if (src.length-srcPos < 16 )
if (src.length - srcPos < 16) {
throw new IllegalArgumentException("Need at least 16 bytes for UUID");
return new UUID(bytesToLong(src, srcPos, 0, 0, 8), bytesToLong(src, srcPos+8, 0, 0, 8));
}
return new UUID(
bytesToLong(src, srcPos, 0, 0, 8), bytesToLong(src, srcPos + 8, 0, 0, 8));
}
}

View File

@ -18,8 +18,8 @@
*******************************************************************************/
package org.apache.commons.lang3;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.UUID;
@ -506,8 +506,11 @@ public class ConversionTest {
static String dbgPrint(boolean[] src) {
StringBuilder sb = new StringBuilder();
for (boolean e : src) {
if (e) sb.append("1,");
else sb.append("0,");
if (e) {
sb.append("1,");
} else {
sb.append("0,");
}
}
String out = sb.toString();
return out.substring(0, out.length() - 1);