You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@reef.apache.org by ju...@apache.org on 2014/11/13 23:49:35 UTC

incubator-reef git commit: Adressing [REEF-15] * Added a new module reef-utils. * Moved the `Optional` class there.

Repository: incubator-reef
Updated Branches:
  refs/heads/master 8026c8ad6 -> e42f901f3


Adressing [REEF-15]
  * Added a new module reef-utils.
  * Moved the `Optional` class there.

This closes #13
This is to set project for Reef-15 https://issues.apache.org/jira/i#browse/REEF-15
Author: Markus Weimer weimer@apache.org


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/e42f901f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/e42f901f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/e42f901f

Branch: refs/heads/master
Commit: e42f901f368a25789df18fa10b0568c985e1ad6e
Parents: 8026c8a
Author: Markus Weimer <we...@apache.org>
Authored: Thu Nov 13 11:14:56 2014 -0800
Committer: Julia Wang <jw...@yahoo.com>
Committed: Thu Nov 13 14:34:33 2014 -0800

----------------------------------------------------------------------
 pom.xml                                         |   1 +
 reef-common/pom.xml                             |   6 +
 .../java/org/apache/reef/util/Optional.java     | 128 -------------------
 .../java/org/apache/reef/util/OptionalTest.java |  99 --------------
 reef-utils/pom.xml                              |  45 +++++++
 .../java/org/apache/reef/util/Optional.java     | 128 +++++++++++++++++++
 .../java/org/apache/reef/util/package-info.java |  22 ++++
 .../java/org/apache/reef/util/OptionalTest.java |  99 ++++++++++++++
 8 files changed, 301 insertions(+), 227 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 8f3d931..f51fd7d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -529,6 +529,7 @@
         <module>reef-wake</module>
         <module>reef-webserver</module>
         <module>reef-utils-hadoop</module>
+        <module>reef-utils</module>
     </modules>
 
     <profiles>

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-common/pom.xml
----------------------------------------------------------------------
diff --git a/reef-common/pom.xml b/reef-common/pom.xml
index ef5ed3a..55cde7f 100644
--- a/reef-common/pom.xml
+++ b/reef-common/pom.xml
@@ -125,6 +125,12 @@
             <version>${project.version}</version>
         </dependency>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>reef-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
             <groupId>com.google.protobuf</groupId>
             <artifactId>protobuf-java</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-common/src/main/java/org/apache/reef/util/Optional.java
----------------------------------------------------------------------
diff --git a/reef-common/src/main/java/org/apache/reef/util/Optional.java b/reef-common/src/main/java/org/apache/reef/util/Optional.java
deleted file mode 100644
index 6dc1a6e..0000000
--- a/reef-common/src/main/java/org/apache/reef/util/Optional.java
+++ /dev/null
@@ -1,128 +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.apache.reef.util;
-
-import net.jcip.annotations.Immutable;
-import net.jcip.annotations.ThreadSafe;
-
-import java.io.Serializable;
-
-/**
- * Represents an optional value. Loosely based on
- * <a href="http://download.java.net/jdk8/docs/api/java/util/Optional.html"></a>The Java 8 version</a>, but filtered for
- * Java 7 compatibility.
- */
-@Immutable
-@ThreadSafe
-public final class Optional<T> implements Serializable {
-
-  private static final long serialVersionUID = 42L;
-
-  private final T value;
-  private final String valueStr;
-  private final int valueHash;
-
-  private Optional(final T value) {
-    this.value = value;
-    this.valueStr = "Optional:{" + value + '}';
-    this.valueHash = value.hashCode();
-  }
-
-  private Optional() {
-    this.value = null;
-    this.valueStr = "OptionalvNothing";
-    this.valueHash = 0;
-  }
-
-  /**
-   * @return An Optional with the given value.
-   * @throws NullPointerException if the value is null
-   */
-  public static <T> Optional<T> of(final T value) throws NullPointerException {
-    if (null == value) {
-      throw new NullPointerException("Passed a null value. Use ofNullable() instead");
-    }
-    return new Optional<>(value);
-  }
-
-  /**
-   * @return an Optional with no value.
-   */
-  public static <T> Optional<T> empty() {
-    return new Optional<>();
-  }
-
-  /**
-   * @return An optional representing the given value, or an empty Optional.
-   */
-  public static <T> Optional<T> ofNullable(final T value) {
-    if (null == value) {
-      return Optional.empty();
-    } else {
-      return Optional.of(value);
-    }
-  }
-
-  /**
-   * @return the value represented or null, if isPresent() is false.
-   */
-  public T get() {
-    return this.value;
-  }
-
-  /**
-   * @param other
-   * @return the value of this Optional or other, if no value exists.
-   */
-  public T orElse(final T other) {
-    if (isPresent()) {
-      return this.get();
-    } else {
-      return other;
-    }
-  }
-
-  /**
-   * @return true if there is a value, false otherwise.
-   */
-  public boolean isPresent() {
-    return null != this.value;
-  }
-
-  @Override
-  public boolean equals(final Object obj) {
-
-    if (this == obj) return true;
-
-    if (obj == null || getClass() != obj.getClass()) return false;
-
-    final Optional that = (Optional) obj;
-    return this.value == that.value || (this.value != null && this.value.equals(that.value));
-  }
-
-  @Override
-  public int hashCode() {
-    return this.valueHash;
-  }
-
-  @Override
-  public String toString() {
-    return this.valueStr;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-common/src/test/java/org/apache/reef/util/OptionalTest.java
----------------------------------------------------------------------
diff --git a/reef-common/src/test/java/org/apache/reef/util/OptionalTest.java b/reef-common/src/test/java/org/apache/reef/util/OptionalTest.java
deleted file mode 100644
index f643ab0..0000000
--- a/reef-common/src/test/java/org/apache/reef/util/OptionalTest.java
+++ /dev/null
@@ -1,99 +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.apache.reef.util;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.util.ArrayList;
-
-/**
- * Tests for Optional.
- */
-public class OptionalTest {
-
-  @Test
-  public void testEmpty() {
-    Assert.assertFalse("An empty Optional should return false to isPresent()",
-        Optional.empty().isPresent());
-  }
-
-  @Test
-  public void testOf() {
-    Assert.assertTrue("Optional.of() needs to return an Optional where isPresent() returns true",
-        Optional.of(2).isPresent());
-  }
-
-  @Test(expected = NullPointerException.class)
-  public void testOfNull() {
-    final Optional<Integer> o = Optional.of(null);
-  }
-
-  @Test
-  public void testOfRandom() {
-    final double value = Math.random();
-    final Optional<Double> o = Optional.of(value);
-    Assert.assertEquals(value, (double) o.get(), 1e-12);
-  }
-
-  @Test
-  public void testOfNullable() {
-    Assert.assertFalse(Optional.ofNullable(null).isPresent());
-    Assert.assertTrue(Optional.ofNullable(1).isPresent());
-    Assert.assertEquals(Optional.ofNullable(1).get(), Integer.valueOf(1));
-  }
-
-  @Test
-  public void testOrElse() {
-    Assert.assertEquals(Optional.empty().orElse(2), 2);
-    Assert.assertEquals(Optional.of(1).orElse(2), Integer.valueOf(1));
-  }
-
-  @Test
-  public void testEquals() {
-    Assert.assertEquals(Optional.empty(), Optional.empty());
-    Assert.assertEquals(Optional.empty(), Optional.ofNullable(null));
-    Assert.assertEquals(Optional.of(1), Optional.of(1));
-    Assert.assertEquals(Optional.of("one"), Optional.of("one"));
-    Assert.assertFalse(Optional.of("one").equals(Optional.of("two")));
-  }
-
-  @Test
-  public void testEqualsCornerCases() {
-
-    // We lose type coercion:
-    Assert.assertFalse(Optional.of(1L).equals(Optional.of(1)));
-    Assert.assertTrue(1L == 1);
-    Assert.assertTrue(new Integer(1) == 1L);
-
-    // .equals() isn't typesafe, so we lose compile-time type checking:
-    Assert.assertFalse(Optional.of(1L).equals(1));
-
-    Assert.assertFalse(Optional.empty().equals(null));
-    Assert.assertFalse(Optional.of(3).equals(3));
-    Assert.assertFalse(Optional.of("one").equals(1));
-
-    // Assert.assertFalse("one" == 1); // incompatible operands; does not compile.
-
-    Assert.assertFalse(Optional.of(new ArrayList<>()).equals(Optional.of(new Object[]{})));
-
-    // Incompatible operands; does not compile, though == between objects is almost always a typo:
-    // Assert.assertFalse(new java.util.ArrayList() == new java.awt.List());
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-utils/pom.xml
----------------------------------------------------------------------
diff --git a/reef-utils/pom.xml b/reef-utils/pom.xml
new file mode 100644
index 0000000..5e6bce2
--- /dev/null
+++ b/reef-utils/pom.xml
@@ -0,0 +1,45 @@
+<?xml version="1.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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.reef</groupId>
+        <artifactId>reef-project</artifactId>
+        <version>0.10-incubating-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>reef-utils</artifactId>
+    <name>REEF Utils</name>
+    <description>Utilities used across REEF modules.</description>
+
+    <!-- This module shouldn't have many dependencies to make sure it is broadly usable across reef subprojects -->
+    <dependencies>
+        <dependency>
+            <groupId>net.jcip</groupId>
+            <artifactId>jcip-annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-utils/src/main/java/org/apache/reef/util/Optional.java
----------------------------------------------------------------------
diff --git a/reef-utils/src/main/java/org/apache/reef/util/Optional.java b/reef-utils/src/main/java/org/apache/reef/util/Optional.java
new file mode 100644
index 0000000..6dc1a6e
--- /dev/null
+++ b/reef-utils/src/main/java/org/apache/reef/util/Optional.java
@@ -0,0 +1,128 @@
+/**
+ * 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.apache.reef.util;
+
+import net.jcip.annotations.Immutable;
+import net.jcip.annotations.ThreadSafe;
+
+import java.io.Serializable;
+
+/**
+ * Represents an optional value. Loosely based on
+ * <a href="http://download.java.net/jdk8/docs/api/java/util/Optional.html"></a>The Java 8 version</a>, but filtered for
+ * Java 7 compatibility.
+ */
+@Immutable
+@ThreadSafe
+public final class Optional<T> implements Serializable {
+
+  private static final long serialVersionUID = 42L;
+
+  private final T value;
+  private final String valueStr;
+  private final int valueHash;
+
+  private Optional(final T value) {
+    this.value = value;
+    this.valueStr = "Optional:{" + value + '}';
+    this.valueHash = value.hashCode();
+  }
+
+  private Optional() {
+    this.value = null;
+    this.valueStr = "OptionalvNothing";
+    this.valueHash = 0;
+  }
+
+  /**
+   * @return An Optional with the given value.
+   * @throws NullPointerException if the value is null
+   */
+  public static <T> Optional<T> of(final T value) throws NullPointerException {
+    if (null == value) {
+      throw new NullPointerException("Passed a null value. Use ofNullable() instead");
+    }
+    return new Optional<>(value);
+  }
+
+  /**
+   * @return an Optional with no value.
+   */
+  public static <T> Optional<T> empty() {
+    return new Optional<>();
+  }
+
+  /**
+   * @return An optional representing the given value, or an empty Optional.
+   */
+  public static <T> Optional<T> ofNullable(final T value) {
+    if (null == value) {
+      return Optional.empty();
+    } else {
+      return Optional.of(value);
+    }
+  }
+
+  /**
+   * @return the value represented or null, if isPresent() is false.
+   */
+  public T get() {
+    return this.value;
+  }
+
+  /**
+   * @param other
+   * @return the value of this Optional or other, if no value exists.
+   */
+  public T orElse(final T other) {
+    if (isPresent()) {
+      return this.get();
+    } else {
+      return other;
+    }
+  }
+
+  /**
+   * @return true if there is a value, false otherwise.
+   */
+  public boolean isPresent() {
+    return null != this.value;
+  }
+
+  @Override
+  public boolean equals(final Object obj) {
+
+    if (this == obj) return true;
+
+    if (obj == null || getClass() != obj.getClass()) return false;
+
+    final Optional that = (Optional) obj;
+    return this.value == that.value || (this.value != null && this.value.equals(that.value));
+  }
+
+  @Override
+  public int hashCode() {
+    return this.valueHash;
+  }
+
+  @Override
+  public String toString() {
+    return this.valueStr;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-utils/src/main/java/org/apache/reef/util/package-info.java
----------------------------------------------------------------------
diff --git a/reef-utils/src/main/java/org/apache/reef/util/package-info.java b/reef-utils/src/main/java/org/apache/reef/util/package-info.java
new file mode 100644
index 0000000..1058756
--- /dev/null
+++ b/reef-utils/src/main/java/org/apache/reef/util/package-info.java
@@ -0,0 +1,22 @@
+/**
+ * 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.
+ */
+/**
+ * Utilities used across reef modules. Subpackages are structred following the java.utils package.
+ */
+package org.apache.reef.util;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/e42f901f/reef-utils/src/test/java/org/apache/reef/util/OptionalTest.java
----------------------------------------------------------------------
diff --git a/reef-utils/src/test/java/org/apache/reef/util/OptionalTest.java b/reef-utils/src/test/java/org/apache/reef/util/OptionalTest.java
new file mode 100644
index 0000000..f643ab0
--- /dev/null
+++ b/reef-utils/src/test/java/org/apache/reef/util/OptionalTest.java
@@ -0,0 +1,99 @@
+/**
+ * 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.apache.reef.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+
+/**
+ * Tests for Optional.
+ */
+public class OptionalTest {
+
+  @Test
+  public void testEmpty() {
+    Assert.assertFalse("An empty Optional should return false to isPresent()",
+        Optional.empty().isPresent());
+  }
+
+  @Test
+  public void testOf() {
+    Assert.assertTrue("Optional.of() needs to return an Optional where isPresent() returns true",
+        Optional.of(2).isPresent());
+  }
+
+  @Test(expected = NullPointerException.class)
+  public void testOfNull() {
+    final Optional<Integer> o = Optional.of(null);
+  }
+
+  @Test
+  public void testOfRandom() {
+    final double value = Math.random();
+    final Optional<Double> o = Optional.of(value);
+    Assert.assertEquals(value, (double) o.get(), 1e-12);
+  }
+
+  @Test
+  public void testOfNullable() {
+    Assert.assertFalse(Optional.ofNullable(null).isPresent());
+    Assert.assertTrue(Optional.ofNullable(1).isPresent());
+    Assert.assertEquals(Optional.ofNullable(1).get(), Integer.valueOf(1));
+  }
+
+  @Test
+  public void testOrElse() {
+    Assert.assertEquals(Optional.empty().orElse(2), 2);
+    Assert.assertEquals(Optional.of(1).orElse(2), Integer.valueOf(1));
+  }
+
+  @Test
+  public void testEquals() {
+    Assert.assertEquals(Optional.empty(), Optional.empty());
+    Assert.assertEquals(Optional.empty(), Optional.ofNullable(null));
+    Assert.assertEquals(Optional.of(1), Optional.of(1));
+    Assert.assertEquals(Optional.of("one"), Optional.of("one"));
+    Assert.assertFalse(Optional.of("one").equals(Optional.of("two")));
+  }
+
+  @Test
+  public void testEqualsCornerCases() {
+
+    // We lose type coercion:
+    Assert.assertFalse(Optional.of(1L).equals(Optional.of(1)));
+    Assert.assertTrue(1L == 1);
+    Assert.assertTrue(new Integer(1) == 1L);
+
+    // .equals() isn't typesafe, so we lose compile-time type checking:
+    Assert.assertFalse(Optional.of(1L).equals(1));
+
+    Assert.assertFalse(Optional.empty().equals(null));
+    Assert.assertFalse(Optional.of(3).equals(3));
+    Assert.assertFalse(Optional.of("one").equals(1));
+
+    // Assert.assertFalse("one" == 1); // incompatible operands; does not compile.
+
+    Assert.assertFalse(Optional.of(new ArrayList<>()).equals(Optional.of(new Object[]{})));
+
+    // Incompatible operands; does not compile, though == between objects is almost always a typo:
+    // Assert.assertFalse(new java.util.ArrayList() == new java.awt.List());
+  }
+}