From c9e34d0b5eb6aff1938ee5488c313beb5abc5d06 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 1 Nov 2016 16:12:27 +0000 Subject: [PATCH] JAL-98 simplified repeat() method --- src/jalview/util/Format.java | 22 ++++++++-------------- test/jalview/util/FormatTest.java | 8 ++++++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/jalview/util/Format.java b/src/jalview/util/Format.java index 20d228d..389afcd 100755 --- a/src/jalview/util/Format.java +++ b/src/jalview/util/Format.java @@ -26,6 +26,8 @@ */ package jalview.util; +import java.util.Arrays; + /** * DOCUMENT ME! * @@ -664,30 +666,22 @@ public class Format } /** - * DOCUMENT ME! + * Returns a string consisting of n repeats of character c * * @param c - * DOCUMENT ME! * @param n - * DOCUMENT ME! * - * @return DOCUMENT ME! + * @return */ - private static String repeat(char c, int n) + static String repeat(char c, int n) { if (n <= 0) { return ""; } - - StringBuffer s = new StringBuffer(n); - - for (int i = 0; i < n; i++) - { - s.append(c); - } - - return s.toString(); + char[] chars = new char[n]; + Arrays.fill(chars, c); + return new String(chars); } /** diff --git a/test/jalview/util/FormatTest.java b/test/jalview/util/FormatTest.java index 5e49142..2082963 100644 --- a/test/jalview/util/FormatTest.java +++ b/test/jalview/util/FormatTest.java @@ -57,4 +57,12 @@ public class FormatTest assertEquals(f.form(123.6f), "124."); assertEquals(f.form(129.6f), "130."); } + + @Test(groups = "Functional") + public void testRepeat() + { + assertEquals(Format.repeat('a', 3), "aaa"); + assertEquals(Format.repeat('b', 0), ""); + assertEquals(Format.repeat('c', -1), ""); + } } -- 1.7.10.2