You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by sr...@apache.org on 2016/09/04 11:39:28 UTC

spark git commit: [SPARK-17308] Improved the spark core code by replacing all pattern match on boolean value by if/else block.

Repository: spark
Updated Branches:
  refs/heads/master 6b156e2fc -> e75c162e9


[SPARK-17308] Improved the spark core code by replacing all pattern match on boolean value by if/else block.

## What changes were proposed in this pull request?
Improved the code quality of spark by replacing all pattern match on boolean value by if/else block.

## How was this patch tested?

By running the tests

Author: Shivansh <sh...@gmail.com>

Closes #14873 from shiv4nsh/SPARK-17308.


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

Branch: refs/heads/master
Commit: e75c162e9e510d74b07f28ccf6c7948ac317a7c6
Parents: 6b156e2
Author: Shivansh <sh...@gmail.com>
Authored: Sun Sep 4 12:39:26 2016 +0100
Committer: Sean Owen <so...@cloudera.com>
Committed: Sun Sep 4 12:39:26 2016 +0100

----------------------------------------------------------------------
 .../scala/org/apache/spark/deploy/Client.scala  | 41 ++++++++++----------
 .../spark/mllib/clustering/KMeansSuite.scala    |  9 ++---
 project/SparkBuild.scala                        |  7 ++--
 .../spark/sql/catalyst/trees/TreeNode.scala     |  7 ++--
 .../sql/catalyst/util/StringKeyHashMap.scala    |  7 ++--
 .../catalyst/analysis/AnalysisErrorSuite.scala  |  9 ++---
 .../streaming/api/java/JavaPairDStream.scala    |  7 ++--
 7 files changed, 44 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/core/src/main/scala/org/apache/spark/deploy/Client.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/org/apache/spark/deploy/Client.scala b/core/src/main/scala/org/apache/spark/deploy/Client.scala
index bf2dab6..ee276e1 100644
--- a/core/src/main/scala/org/apache/spark/deploy/Client.scala
+++ b/core/src/main/scala/org/apache/spark/deploy/Client.scala
@@ -124,27 +124,26 @@ private class ClientEndpoint(
     logInfo("... polling master for driver state")
     val statusResponse =
       activeMasterEndpoint.askWithRetry[DriverStatusResponse](RequestDriverStatus(driverId))
-    statusResponse.found match {
-      case false =>
-        logError(s"ERROR: Cluster master did not recognize $driverId")
-        System.exit(-1)
-      case true =>
-        logInfo(s"State of $driverId is ${statusResponse.state.get}")
-        // Worker node, if present
-        (statusResponse.workerId, statusResponse.workerHostPort, statusResponse.state) match {
-          case (Some(id), Some(hostPort), Some(DriverState.RUNNING)) =>
-            logInfo(s"Driver running on $hostPort ($id)")
-          case _ =>
-        }
-        // Exception, if present
-        statusResponse.exception match {
-          case Some(e) =>
-            logError(s"Exception from cluster was: $e")
-            e.printStackTrace()
-            System.exit(-1)
-          case _ =>
-            System.exit(0)
-        }
+    if (statusResponse.found) {
+      logInfo(s"State of $driverId is ${statusResponse.state.get}")
+      // Worker node, if present
+      (statusResponse.workerId, statusResponse.workerHostPort, statusResponse.state) match {
+        case (Some(id), Some(hostPort), Some(DriverState.RUNNING)) =>
+          logInfo(s"Driver running on $hostPort ($id)")
+        case _ =>
+      }
+      // Exception, if present
+      statusResponse.exception match {
+        case Some(e) =>
+          logError(s"Exception from cluster was: $e")
+          e.printStackTrace()
+          System.exit(-1)
+        case _ =>
+          System.exit(0)
+      }
+    } else {
+      logError(s"ERROR: Cluster master did not recognize $driverId")
+      System.exit(-1)
     }
   }
 

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala
----------------------------------------------------------------------
diff --git a/mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala
index 3003c62..2d35b31 100644
--- a/mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala
+++ b/mllib/src/test/scala/org/apache/spark/mllib/clustering/KMeansSuite.scala
@@ -304,11 +304,10 @@ class KMeansSuite extends SparkFunSuite with MLlibTestSparkContext {
 
 object KMeansSuite extends SparkFunSuite {
   def createModel(dim: Int, k: Int, isSparse: Boolean): KMeansModel = {
-    val singlePoint = isSparse match {
-      case true =>
-        Vectors.sparse(dim, Array.empty[Int], Array.empty[Double])
-      case _ =>
-        Vectors.dense(Array.fill[Double](dim)(0.0))
+    val singlePoint = if (isSparse) {
+      Vectors.sparse(dim, Array.empty[Int], Array.empty[Double])
+    } else {
+      Vectors.dense(Array.fill[Double](dim)(0.0))
     }
     new KMeansModel(Array.fill[Vector](k)(singlePoint))
   }

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/project/SparkBuild.scala
----------------------------------------------------------------------
diff --git a/project/SparkBuild.scala b/project/SparkBuild.scala
index 83a7c08..d164ead 100644
--- a/project/SparkBuild.scala
+++ b/project/SparkBuild.scala
@@ -212,9 +212,10 @@ object SparkBuild extends PomBuild {
     cachedFun(findFiles(scalaSource.in(config).value))
   }
 
-  private def findFiles(file: File): Set[File] = file.isDirectory match {
-    case true => file.listFiles().toSet.flatMap(findFiles) + file
-    case false => Set(file)
+  private def findFiles(file: File): Set[File] = if (file.isDirectory) {
+    file.listFiles().toSet.flatMap(findFiles) + file
+  } else {
+    Set(file)
   }
 
   def enableScalaStyle: Seq[sbt.Def.Setting[_]] = Seq(

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
index 24a2dc9..037f8cb 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala
@@ -103,9 +103,10 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product {
    * Find the first [[TreeNode]] that satisfies the condition specified by `f`.
    * The condition is recursively applied to this node and all of its children (pre-order).
    */
-  def find(f: BaseType => Boolean): Option[BaseType] = f(this) match {
-    case true => Some(this)
-    case false => children.foldLeft(Option.empty[BaseType]) { (l, r) => l.orElse(r.find(f)) }
+  def find(f: BaseType => Boolean): Option[BaseType] = if (f(this)) {
+    Some(this)
+  } else {
+    children.foldLeft(Option.empty[BaseType]) { (l, r) => l.orElse(r.find(f)) }
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringKeyHashMap.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringKeyHashMap.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringKeyHashMap.scala
index d5d151a..a7ac613 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringKeyHashMap.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StringKeyHashMap.scala
@@ -22,9 +22,10 @@ package org.apache.spark.sql.catalyst.util
  * sensitive or insensitive.
  */
 object StringKeyHashMap {
-  def apply[T](caseSensitive: Boolean): StringKeyHashMap[T] = caseSensitive match {
-    case false => new StringKeyHashMap[T](_.toLowerCase)
-    case true => new StringKeyHashMap[T](identity)
+  def apply[T](caseSensitive: Boolean): StringKeyHashMap[T] = if (caseSensitive) {
+    new StringKeyHashMap[T](identity)
+  } else {
+    new StringKeyHashMap[T](_.toLowerCase)
   }
 }
 

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
index e7c8615..21afe9f 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
@@ -414,11 +414,10 @@ class AnalysisErrorSuite extends AnalysisTest {
             AttributeReference("a", dataType)(exprId = ExprId(2)),
             AttributeReference("b", IntegerType)(exprId = ExprId(1))))
 
-      shouldSuccess match {
-        case true =>
-          assertAnalysisSuccess(plan, true)
-        case false =>
-          assertAnalysisError(plan, "expression `a` cannot be used as a grouping expression" :: Nil)
+      if (shouldSuccess) {
+        assertAnalysisSuccess(plan, true)
+      } else {
+        assertAnalysisError(plan, "expression `a` cannot be used as a grouping expression" :: Nil)
       }
     }
 

http://git-wip-us.apache.org/repos/asf/spark/blob/e75c162e/streaming/src/main/scala/org/apache/spark/streaming/api/java/JavaPairDStream.scala
----------------------------------------------------------------------
diff --git a/streaming/src/main/scala/org/apache/spark/streaming/api/java/JavaPairDStream.scala b/streaming/src/main/scala/org/apache/spark/streaming/api/java/JavaPairDStream.scala
index dec9831..da9ff85 100644
--- a/streaming/src/main/scala/org/apache/spark/streaming/api/java/JavaPairDStream.scala
+++ b/streaming/src/main/scala/org/apache/spark/streaming/api/java/JavaPairDStream.scala
@@ -471,9 +471,10 @@ class JavaPairDStream[K, V](val dstream: DStream[(K, V)])(
       val list: JList[V] = values.asJava
       val scalaState: Optional[S] = JavaUtils.optionToOptional(state)
       val result: Optional[S] = in.apply(list, scalaState)
-      result.isPresent match {
-        case true => Some(result.get())
-        case _ => None
+      if (result.isPresent) {
+        Some(result.get())
+      } else {
+        None
       }
     }
     scalaFunc


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org