hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chl...@apache.org
Subject [hama] branch componentization updated: Refactor Setting for consistency of instance creation.
Date Sun, 17 Mar 2019 16:38:25 GMT
This is an automated email from the ASF dual-hosted git repository.

chl501 pushed a commit to branch componentization
in repository https://gitbox.apache.org/repos/asf/hama.git


The following commit(s) were added to refs/heads/componentization by this push:
     new 62e7ff9  Refactor Setting for consistency of instance creation.
62e7ff9 is described below

commit 62e7ff9b0f45f1b2383ff804943c70b2d3a77aaa
Author: Chiahung Lin <chl501@apache.org>
AuthorDate: Sun Mar 17 17:36:54 2019 +0100

    Refactor Setting for consistency of instance creation.
---
 commons.v2/build.sbt                               |  5 ++++-
 .../main/scala/org/apache/hama/conf/Setting.scala  | 26 +++++++++-------------
 .../scala/org/apache/hama/conf/SettingSpec.scala   |  8 +++----
 3 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/commons.v2/build.sbt b/commons.v2/build.sbt
index 2d7fe2f..ba4584f 100644
--- a/commons.v2/build.sbt
+++ b/commons.v2/build.sbt
@@ -17,8 +17,11 @@
  */
 name := "commons.v2"
 
+scalacOptions += "-Ypartial-unification"
+
 libraryDependencies ++= Seq (
   "com.typesafe" % "config" % "1.3.3",
   "org.slf4j" % "slf4j-api" % "1.7.25",
-  "org.slf4j" % "slf4j-simple" % "1.7.25"
+  "org.slf4j" % "slf4j-simple" % "1.7.25",
+  "org.typelevel" %% "cats-core" % "1.6.0"
 )
diff --git a/commons.v2/src/main/scala/org/apache/hama/conf/Setting.scala b/commons.v2/src/main/scala/org/apache/hama/conf/Setting.scala
index 8f64b00..aa82ffd 100644
--- a/commons.v2/src/main/scala/org/apache/hama/conf/Setting.scala
+++ b/commons.v2/src/main/scala/org/apache/hama/conf/Setting.scala
@@ -22,36 +22,32 @@ import com.typesafe.config.ConfigFactory
 import com.typesafe.config.ConfigValue
 import com.typesafe.config.ConfigValueFactory
 import scala.util.Try
+import cats.data.Reader
 
 trait Setting {
 
-  def set[T](key: String, value: T): DefaultSetting 
+  def set[T](key: String, value: T): Setting
 
   def get[T](key: String): Option[T] 
 
 }
-
 object Setting {
 
   val hama = "hama"
 
-  def create(name: String = hama): Setting = DefaultSetting (
-    ConfigFactory.load(name)
-  )
+  def typesafe = Typesafe(ConfigFactory.load(hama))
 
-  // TODO: hadoop configuration to  
-  // def from(configuration: Configuration): Setting = ???
+  case class Typesafe(config: Config) extends Setting {
 
-}
+    override def set[T](key: String, value: T): Setting =  
+      Typesafe(config.withValue(key, ConfigValueFactory.fromAnyRef(value)))
 
-protected[conf] case class DefaultSetting(config: Config) extends Setting {
+    override def get[T](key: String): Option[T] = 
+      Try(config.getAnyRef(key).asInstanceOf[T]).toOption
 
-  override def set[T](key: String, value: T): DefaultSetting = DefaultSetting (
-    config.withValue(key, ConfigValueFactory.fromAnyRef(value))
-  )
+  }
 
-  override def get[T](key: String): Option[T] = 
-    Try(config.getAnyRef(key).asInstanceOf[T]).toOption
+  // TODO: hadoop configuration to  
+  // def from(configuration: Configuration): Setting = ???
 
 }
-
diff --git a/commons.v2/src/test/scala/org/apache/hama/conf/SettingSpec.scala b/commons.v2/src/test/scala/org/apache/hama/conf/SettingSpec.scala
index 1f622dd..56e6322 100644
--- a/commons.v2/src/test/scala/org/apache/hama/conf/SettingSpec.scala
+++ b/commons.v2/src/test/scala/org/apache/hama/conf/SettingSpec.scala
@@ -22,10 +22,10 @@ import org.scalatest._
 class SettingSpec extends FlatSpec with Matchers {
 
   "Setting" should "get/ set value" in {
-    val setting = Setting.create()
-    val newSetting = setting.set[String]("db.host", "host1").
-                             set[Int]("db.port", 1234).
-                             set[Double]("cpu.load", 1.2d)
+    val typesafe = Setting.typesafe
+    val newSetting = typesafe.set[String]("db.host", "host1").
+                              set[Int]("db.port", 1234).
+                              set[Double]("cpu.load", 1.2d)
     assert(Some("host1") == newSetting.get("db.host"))
     assert(Some(1234) == newSetting.get("db.port"))
     assert(Some(1.2d) == newSetting.get("cpu.load"))


Mime
View raw message