You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/01/05 14:19:54 UTC
[1/3] groovy git commit: Cache the text of GString for better
performance
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_6_X 289d9a5c3 -> 3c63bc4e9
Cache the text of GString for better performance
(cherry picked from commit 628a19a)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/474e3f78
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/474e3f78
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/474e3f78
Branch: refs/heads/GROOVY_2_6_X
Commit: 474e3f781c18588fdbf837bf001b730db26f9317
Parents: 289d9a5
Author: sunlan <su...@apache.org>
Authored: Fri Jan 5 20:36:18 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Fri Jan 5 22:19:31 2018 +0800
----------------------------------------------------------------------
src/main/groovy/groovy/lang/GString.java | 34 ++++++++++++++++++---------
1 file changed, 23 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/474e3f78/src/main/groovy/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/GString.java b/src/main/groovy/groovy/lang/GString.java
index cc13eef..5de910f 100644
--- a/src/main/groovy/groovy/lang/GString.java
+++ b/src/main/groovy/groovy/lang/GString.java
@@ -42,20 +42,26 @@ 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";
/**
* A GString containing a single empty String and no values.
*/
public static final GString EMPTY = new GString(new Object[0]) {
+ 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 String string;
private final Object[] values;
public GString(Object values) {
@@ -142,6 +148,10 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
@Override
public String toString() {
+ if (null != string) {
+ return string;
+ }
+
Writer buffer = new StringBuilderWriter(calcInitialCapacity());
try {
writeTo(buffer);
@@ -149,7 +159,8 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
catch (IOException e) {
throw new StringWriterIOException(e);
}
- return buffer.toString();
+
+ return (string = buffer.toString());
}
private int calcInitialCapacity() {
@@ -160,7 +171,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 +187,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 +215,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] });
}
}
}
[3/3] groovy git commit: GROOVY-8432: Weird behavior parsing dates
Posted by su...@apache.org.
GROOVY-8432: Weird behavior parsing dates
(cherry picked from commit 033acfb)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/3c63bc4e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/3c63bc4e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/3c63bc4e
Branch: refs/heads/GROOVY_2_6_X
Commit: 3c63bc4e9103677d6c8b1905a4e6e3048f0f92df
Parents: 7231579
Author: sunlan <su...@apache.org>
Authored: Fri Jan 5 21:42:55 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Fri Jan 5 22:19:45 2018 +0800
----------------------------------------------------------------------
.../groovy/runtime/DateGroovyMethods.java | 12 ++--
.../groovy/runtime/DateGroovyMethodsTest.java | 59 ++++++++++++++++++++
2 files changed, 65 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/3c63bc4e/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
index 1c30802..822bdc5 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DateGroovyMethods.java
@@ -316,7 +316,7 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
*/
public static Calendar next(Calendar self) {
Calendar result = (Calendar) self.clone();
- result.add(Calendar.DAY_OF_YEAR, 1);
+ result.add(Calendar.DATE, 1);
return result;
}
@@ -329,7 +329,7 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
*/
public static Calendar previous(Calendar self) {
Calendar result = (Calendar) self.clone();
- result.add(Calendar.DAY_OF_YEAR, -1);
+ result.add(Calendar.DATE, -1);
return result;
}
@@ -375,9 +375,9 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
* @since 1.0
*/
public static Date plus(Date self, int days) {
- Calendar calendar = (Calendar) Calendar.getInstance().clone();
+ Calendar calendar = Calendar.getInstance();
calendar.setTime(self);
- calendar.add(Calendar.DAY_OF_YEAR, days);
+ calendar.add(Calendar.DATE, days);
return calendar.getTime();
}
@@ -401,9 +401,9 @@ public class DateGroovyMethods extends DefaultGroovyMethodsSupport {
* @return the new Timestamp
*/
public static Timestamp plus(Timestamp self, int days) {
- Calendar calendar = (Calendar) Calendar.getInstance().clone();
+ Calendar calendar = Calendar.getInstance();
calendar.setTime(self);
- calendar.add(Calendar.DAY_OF_YEAR, days);
+ calendar.add(Calendar.DATE, days);
Timestamp ts = new Timestamp(calendar.getTime().getTime());
ts.setNanos(self.getNanos());
return ts;
http://git-wip-us.apache.org/repos/asf/groovy/blob/3c63bc4e/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java b/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
new file mode 100644
index 0000000..c34ec60
--- /dev/null
+++ b/src/test/java/org/codehaus/groovy/runtime/DateGroovyMethodsTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.codehaus.groovy.runtime;
+
+import org.junit.Test;
+
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+import static org.junit.Assert.assertEquals;
+
+public class DateGroovyMethodsTest {
+ @Test
+ public void minus() throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+ assertEquals("20171231", sdf.format(DateGroovyMethods.minus(sdf.parse("20180101"), 1)));
+ }
+
+ @Test
+ public void plus() throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+ assertEquals("20180101", sdf.format(DateGroovyMethods.plus(new Timestamp(sdf.parse("20171231").getTime()), 1)));
+ }
+
+ @Test
+ public void next() throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(sdf.parse("20171231"));
+ assertEquals("20180101", sdf.format(DateGroovyMethods.next(calendar).getTime()));
+ }
+
+ @Test
+ public void previous() throws ParseException {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(sdf.parse("20180101"));
+ assertEquals("20171231", sdf.format(DateGroovyMethods.previous(calendar).getTime()));
+ }
+}
[2/3] groovy git commit: Revert "Cache the text of GString for better
performance"
Posted by su...@apache.org.
Revert "Cache the text of GString for better performance"
(cherry picked from commit 5262442)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/7231579f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/7231579f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/7231579f
Branch: refs/heads/GROOVY_2_6_X
Commit: 7231579f2a5bef1697021acd5fee320064bb131e
Parents: 474e3f7
Author: sunlan <su...@apache.org>
Authored: Fri Jan 5 20:50:42 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Fri Jan 5 22:19:35 2018 +0800
----------------------------------------------------------------------
src/main/groovy/groovy/lang/GString.java | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/7231579f/src/main/groovy/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/GString.java b/src/main/groovy/groovy/lang/GString.java
index 5de910f..06ad8dd 100644
--- a/src/main/groovy/groovy/lang/GString.java
+++ b/src/main/groovy/groovy/lang/GString.java
@@ -61,7 +61,6 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
public static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
- private String string;
private final Object[] values;
public GString(Object values) {
@@ -148,10 +147,6 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
@Override
public String toString() {
- if (null != string) {
- return string;
- }
-
Writer buffer = new StringBuilderWriter(calcInitialCapacity());
try {
writeTo(buffer);
@@ -160,7 +155,7 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
throw new StringWriterIOException(e);
}
- return (string = buffer.toString());
+ return buffer.toString();
}
private int calcInitialCapacity() {