From commits-return-5151-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Fri Jan 5 15:29:09 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 2BCAF180647 for ; Fri, 5 Jan 2018 15:29:09 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 1BEC7160C19; Fri, 5 Jan 2018 14:29:09 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 61126160C14 for ; Fri, 5 Jan 2018 15:29:08 +0100 (CET) Received: (qmail 99166 invoked by uid 500); 5 Jan 2018 14:29:07 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 99152 invoked by uid 99); 5 Jan 2018 14:29:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Jan 2018 14:29:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7B0BDDFDE6; Fri, 5 Jan 2018 14:29:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sunlan@apache.org To: commits@groovy.apache.org Date: Fri, 05 Jan 2018 14:29:07 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/2] groovy git commit: Trivial refactoring: Refine GString Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X 97921e567 -> f3f98e7b2 Trivial refactoring: Refine GString Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/7fb520f6 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/7fb520f6 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/7fb520f6 Branch: refs/heads/GROOVY_2_4_X Commit: 7fb520f664c24389276d1aaee633ffa28049d4af Parents: 97921e5 Author: sunlan Authored: Fri Jan 5 22:27:18 2018 +0800 Committer: sunlan Committed: Fri Jan 5 22:27:18 2018 +0800 ---------------------------------------------------------------------- src/main/groovy/lang/GString.java | 39 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/7fb520f6/src/main/groovy/lang/GString.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/GString.java b/src/main/groovy/lang/GString.java index 32ff7b0..10a4de9 100644 --- a/src/main/groovy/lang/GString.java +++ b/src/main/groovy/lang/GString.java @@ -42,21 +42,28 @@ import java.util.regex.Pattern; */ public abstract class GString extends GroovyObjectSupport implements Comparable, CharSequence, Writable, Buildable, Serializable { - static final long serialVersionUID = -2638020355892246323L; + private static final long serialVersionUID = -2638020355892246323L; + private static final String MKP = "mkp"; + private static final String YIELD = "yield"; + + public static final String[] EMPTY_STRING_ARRAY = new String[0]; + public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; /** * A GString containing a single empty String and no values. */ - public static final GString EMPTY = new GString(new Object[0]) { + public static final GString EMPTY = new GString(EMPTY_OBJECT_ARRAY) { + private static final long serialVersionUID = -7676746462783374250L; + @Override public String[] getStrings() { - return new String[]{""}; + return new String[]{ "" }; } }; - public static final String[] EMPTY_STRING_ARRAY = new String[0]; - public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; - private Object[] values; + + + private final Object[] values; public GString(Object values) { this.values = (Object[]) values; @@ -142,13 +149,14 @@ public abstract class GString extends GroovyObjectSupport implements Comparable, @Override public String toString() { - StringBuilderWriter buffer = new StringBuilderWriter(calcInitialCapacity()); + Writer buffer = new StringBuilderWriter(calcInitialCapacity()); try { writeTo(buffer); } catch (IOException e) { throw new StringWriterIOException(e); } + return buffer.toString(); } @@ -160,7 +168,7 @@ public abstract class GString extends GroovyObjectSupport implements Comparable, initialCapacity += string.length(); } - initialCapacity += values.length * Math.max(Math.ceil(initialCapacity / strings.length), 1); + initialCapacity += values.length * Math.max(initialCapacity / strings.length, 1); return Math.max((int) (initialCapacity * 1.2), 16); } @@ -176,14 +184,15 @@ public abstract class GString extends GroovyObjectSupport implements Comparable, if (value instanceof Closure) { final Closure c = (Closure) value; + int maximumNumberOfParameters = c.getMaximumNumberOfParameters(); - if (c.getMaximumNumberOfParameters() == 0) { + if (maximumNumberOfParameters == 0) { InvokerHelper.write(out, c.call()); - } else if (c.getMaximumNumberOfParameters() == 1) { + } else if (maximumNumberOfParameters == 1) { c.call(out); } else { throw new GroovyRuntimeException("Trying to evaluate a GString containing a Closure taking " - + c.getMaximumNumberOfParameters() + " parameters"); + + maximumNumberOfParameters + " parameters"); } } else { InvokerHelper.write(out, value); @@ -203,11 +212,11 @@ public abstract class GString extends GroovyObjectSupport implements Comparable, final int numberOfValues = values.length; for (int i = 0, size = s.length; i < size; i++) { - builder.getProperty("mkp"); - builder.invokeMethod("yield", new Object[]{s[i]}); + builder.getProperty(MKP); + builder.invokeMethod(YIELD, new Object[]{ s[i] }); if (i < numberOfValues) { - builder.getProperty("mkp"); - builder.invokeMethod("yield", new Object[]{values[i]}); + builder.getProperty(MKP); + builder.invokeMethod(YIELD, new Object[]{ values[i] }); } } }