You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2019/08/02 17:55:05 UTC
[sis] 02/03: Convenience method for repeating a character at the
end of a StringBuilder.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
commit 6baf41f37aa6d9ffb280f6f9339c5d7965cffb71
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Fri Aug 2 16:35:12 2019 +0200
Convenience method for repeating a character at the end of a StringBuilder.
---
.../sis/referencing/operation/matrix/Matrices.java | 5 ++--
.../main/java/org/apache/sis/io/TableAppender.java | 5 +++-
.../java/org/apache/sis/util/StringBuilders.java | 31 +++++++++++++++++++---
3 files changed, 34 insertions(+), 7 deletions(-)
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
index 2ec524f..c5770cd 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
@@ -29,6 +29,7 @@ import org.apache.sis.util.Static;
import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ComparisonMode;
import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.StringBuilders;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.math.DecimalFunctions;
import org.apache.sis.internal.util.Numerics;
@@ -1187,10 +1188,8 @@ public final class Matrices extends Static {
s += 2;
} else {
int n = Math.min(s, maximumPaddingZeros[flatIndex]);
+ StringBuilders.repeat(buffer, '0', n);
s -= n;
- while (--n >= 0) {
- buffer.append('0');
- }
}
buffer.append(CharSequences.spaces(s));
}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/io/TableAppender.java b/core/sis-utility/src/main/java/org/apache/sis/io/TableAppender.java
index aa33057..eb3a2bc 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/io/TableAppender.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/io/TableAppender.java
@@ -25,6 +25,7 @@ import java.io.UncheckedIOException;
import org.apache.sis.util.ArraysExt;
import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.StringBuilders;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.internal.util.X364;
@@ -868,7 +869,9 @@ public class TableAppender extends Appender implements Flushable {
* @param count number of repetition.
*/
private static void repeat(final Appendable out, final char car, int count) throws IOException {
- while (--count >= 0) {
+ if (out instanceof StringBuilder) {
+ StringBuilders.repeat((StringBuilder) out, car, count);
+ } else while (--count >= 0) {
out.append(car);
}
}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java b/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
index 501369c..b50cdcc 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/StringBuilders.java
@@ -34,7 +34,7 @@ import static java.lang.Character.*;
* the <cite>Basic Multilingual Plane</cite> (BMP).
*
* @author Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.0
*
* @see CharSequences
*
@@ -162,13 +162,38 @@ public final class StringBuilders extends Static {
}
/**
- * Inserts the given character <var>n</var> time at the given position.
+ * Appends the given character <var>n</var> times.
+ * This method does nothing if the given {@code count} is zero.
+ *
+ * @param buffer the buffer where to append the character.
+ * @param c the character to repeat.
+ * @param count number of times to repeat the given character.
+ * @throws NullPointerException if the given buffer is null.
+ * @throws IllegalArgumentException if the given count is negative.
+ *
+ * @since 1.0
+ */
+ public static void repeat(final StringBuilder buffer, final char c, final int count) {
+ ArgumentChecks.ensureNonNull("buffer", buffer);
+ switch (count) {
+ case 0: break;
+ case 1: buffer.append(c); break;
+ default: {
+ ArgumentChecks.ensurePositive("count", count);
+ buffer.append(c == ' ' ? CharSequences.spaces(count) : new Repeat(c, count));
+ break;
+ }
+ }
+ }
+
+ /**
+ * Inserts the given character <var>n</var> times at the given position.
* This method does nothing if the given {@code count} is zero.
*
* @param buffer the buffer where to insert the character.
* @param offset position where to insert the characters.
* @param c the character to repeat.
- * @param count number of time to repeat the given character.
+ * @param count number of times to repeat the given character.
* @throws NullPointerException if the given buffer is null.
* @throws IndexOutOfBoundsException if the given index is invalid.
* @throws IllegalArgumentException if the given count is negative.