groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-8899: Add DGSM `first` and `last`(closes #828)
Date Fri, 23 Nov 2018 15:05:23 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 306644ded -> 43c75a23f


GROOVY-8899: Add DGSM `first` and `last`(closes #828)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/43c75a23
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/43c75a23
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/43c75a23

Branch: refs/heads/master
Commit: 43c75a23f43dc8968471927fd8a7d8119290d7ec
Parents: 306644d
Author: Daniel Sun <sunlan@apache.org>
Authored: Fri Nov 23 22:41:54 2018 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Fri Nov 23 23:04:34 2018 +0800

----------------------------------------------------------------------
 .../runtime/DefaultGroovyStaticMethods.java     | 23 +++++++++
 .../DefaultGroovyStaticMethodsTest.groovy       | 52 ++++++++++++++++++++
 .../runtime/DefaultGroovyStaticMethodsTest.java | 35 -------------
 3 files changed, 75 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/43c75a23/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
index 7d94771..0e80157 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
@@ -28,9 +28,12 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
+import java.util.Optional;
 import java.util.ResourceBundle;
 import java.util.TimeZone;
 import java.util.regex.Matcher;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
 
 /**
  * This class defines all the new static groovy methods which appear on normal
@@ -263,4 +266,24 @@ public class DefaultGroovyStaticMethods {
     return System.currentTimeMillis() / 1000;
   }
 
+    /**
+     * Returns a {@link Collector} that gets the first element.
+     *
+     * @return a {@link Collector} which implements the first operation
+     * @since 3.0.0
+     */
+    public static <T> Collector<T, ?, Optional<T>> first(Collectors self)
{
+        return Collectors.reducing((v1, v2) -> v1);
+    }
+
+    /**
+     * Returns a {@link Collector} that gets the last element.
+     *
+     * @return a {@link Collector} which implements the last operation
+     * @since 3.0.0
+     */
+    public static <T> Collector<T, ?, Optional<T>> last(Collectors self)
{
+        return Collectors.reducing((v1, v2) -> v2);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/43c75a23/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
new file mode 100644
index 0000000..daedb24
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy
@@ -0,0 +1,52 @@
+/*
+ *  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 java.util.stream.Collectors
+import java.util.stream.Stream
+
+/**
+ * Tests for DefaultGroovyStaticMethods
+ */
+class DefaultGroovyStaticMethodsTest extends GroovyTestCase {
+
+    void testCurrentTimeSeconds() {
+	    long timeMillis = System.currentTimeMillis()
+        long timeSeconds = System.currentTimeSeconds()
+        long timeMillis2 = System.currentTimeMillis()
+        assert timeMillis/1000 as int <= timeSeconds
+        assert timeMillis2/1000 as int >= timeSeconds
+    }
+
+    void testFirst() {
+        assert 2 == Stream.of(2, 3, 6, 5).collect(Collectors.first()).get()
+    }
+
+    void testLast() {
+        assert 5 == Stream.of(2, 3, 6, 5).collect(Collectors.last()).get()
+    }
+
+    void testFirstAndLast() {
+        Tuple2<Integer, Integer> firstAndLastTuple =
+                Stream.of(2, 3, 6, 5)
+                        .collect(Tuple.collectors(Collectors.first(), Collectors.last()))
+                        .map1(Optional::get).map2(Optional::get)
+        assert Tuple.tuple(2, 5) == firstAndLastTuple
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/43c75a23/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.java b/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.java
deleted file mode 100644
index dca8148..0000000
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- *  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 groovy.util.GroovyTestCase;
-
-/**
- * Tests for DefaultGroovyStaticMethods
- */
-public class DefaultGroovyStaticMethodsTest extends GroovyTestCase {
-
-    public void testCurrentTimeSeconds() {
-	long timeMillis = System.currentTimeMillis();
-        long timeSeconds = DefaultGroovyStaticMethods.currentTimeSeconds(null);
-        long timeMillis2 = System.currentTimeMillis();
-        assertTrue(timeMillis/1000 <= timeSeconds);
-        assertTrue(timeMillis2/1000 >= timeSeconds);
-    }
-}


Mime
View raw message