diff options
author | Sven Gothel <[email protected]> | 2012-10-10 16:52:40 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-10-10 16:52:40 +0200 |
commit | 9667575aabfbf6fe503bc2e14e5ac7ea743949b0 (patch) | |
tree | 194938fe9eb1eaa27ab45c2d89a06076f0f169cd /src | |
parent | fd8402ce90041aa506fdaddeedf7bbe09c56d788 (diff) |
Buffers.toString(): Add optional format string for single element
Diffstat (limited to 'src')
-rwxr-xr-x | src/java/com/jogamp/common/nio/Buffers.java | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index e4e20ef..5b5f83b 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -877,10 +877,13 @@ public class Buffers { /** * Appends Buffer details inclusive data to a StringBuilder instance. * @param sb optional pass through StringBuilder + * @param f optional format string of one element, i.e. "%10.5f" for {@link FloatBuffer}, see {@link java.util.Formatter}, + * or <code>null</code> for unformatted output. + * <b>Note:</b> Caller is responsible to match the format string w/ the data type as expected in the given buffer. * @param buffer Any valid Buffer instance * @return the modified StringBuilder containing the Buffer details */ - public static StringBuilder toString(StringBuilder sb, Buffer buffer) { + public static StringBuilder toString(StringBuilder sb, String f, Buffer buffer) { if(null == sb) { sb = new StringBuilder(); } @@ -893,43 +896,71 @@ public class Buffers { final ByteBuffer b = (ByteBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof FloatBuffer) { final FloatBuffer b = (FloatBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof IntBuffer) { final IntBuffer b = (IntBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof ShortBuffer) { final ShortBuffer b = (ShortBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof DoubleBuffer) { final DoubleBuffer b = (DoubleBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof LongBuffer) { final LongBuffer b = (LongBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } else if (buffer instanceof CharBuffer) { final CharBuffer b = (CharBuffer)buffer; for(int i=0; i<b.limit(); i++) { if(0<i) { sb.append(", "); } - sb.append(b.get(i)); + if(null == f) { + sb.append(b.get(i)); + } else { + sb.append(String.format(f, b.get(i))); + } } } sb.append("]"); |