From 9667575aabfbf6fe503bc2e14e5ac7ea743949b0 Mon Sep 17 00:00:00 2001 From: Sven Gothel <sgothel@jausoft.com> Date: Wed, 10 Oct 2012 16:52:40 +0200 Subject: Buffers.toString(): Add optional format string for single element --- src/java/com/jogamp/common/nio/Buffers.java | 47 ++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/java/com/jogamp/common/nio') 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("]"); -- cgit v1.2.3