Java的静态工厂类,类似于Scala。

huangapple go评论78阅读模式
英文:

Java Static Factory Class like Scala

问题

Sure, here's the translated Java code for your Scala code:

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

class Writer {
    private static Map<String, String> options = new HashMap<>();

    public static Optional<String> a() {
        return Optional.ofNullable(options.get("a"));
    }

    public static Optional<String> b() {
        return Optional.ofNullable(options.get("b"));
    }

    public static Optional<String> c() {
        return Optional.ofNullable(options.get("c"));
    }

    public static Writer option(String key, String value) {
        options.put(key, value);
        return Writer.this;
    }

    public static void write() {
        System.out.println(a().orElse("Z"));
        System.out.println(b().orElse("Z"));
        System.out.println(c().orElse("Z"));
    }
}

public class Run {
    public static void main(String[] args) {
        Writer.write();

        Writer.option("a", "A")
              .option("b", "B")
              .option("c", "C")
              .write();
    }
}

Please note that the translation from Scala to Java might not be a straightforward one-to-one mapping, as the languages have different syntax and features. In this translation, I've used Optional for handling the absence of values, and I've adjusted the method and field access to Java conventions.

英文:

I have written a sample code in Scala and I need to convert to Java, but I am kinda confused how I can have this pattern in Java,

In Scala I have:

object Writer {
val options = new scala.collection.mutable.HashMap[String, String]
def a: Option[String] = options.get(&quot;a&quot;)
def b: Option[String] = options.get(&quot;b&quot;)
def c: Option[String] = options.get(&quot;c&quot;)
def option(key: String, value: String): Writer.type = {
options += (key -&gt; value)
this
}
def write() ={
println(a.getOrElse(&quot;Z&quot;))
println(b.getOrElse(&quot;Z&quot;))
println(c.getOrElse(&quot;Z&quot;))
}
}

Which I can run with or without passing the option(s):

object Run {
def main(args: Array[String]): Unit = {
Writer
.write()
Writer
.option(&quot;a&quot;, &quot;A&quot;)
.option(&quot;b&quot;, &quot;B&quot;)
.option(&quot;c&quot;, &quot;C&quot;)
.write()
}
}

How can I convert this to Java?

答案1

得分: 2

Scala的对象就像Java中的单例对象:

import scala.collection.mutable.HashMap

object Writer {
  private val options: HashMap[String, String] = new HashMap[String, String]()

  def a(): Option[String] = {
    options.get("a")
  }

  def b(): Option[String] = {
    options.get("b")
  }

  def c(): Option[String] = {
    options.get("c")
  }

  def option(key: String, value: String): this.type = {
    options(key) = value
    this
  }

  def write(): Unit = {
    println(a().getOrElse("Z"))
    println(b().getOrElse("Z"))
    println(c().getOrElse("Z"))
  }

  def main(args: Array[String]): Unit = {
    Writer
      .option("a", "A")
      .option("b", "B")
      .option("c", "C")
      .write()

    Writer.write() // 与上面的输出相同
  }
}

请注意,Scala中的单例对象使用object关键字定义,其内部成员和方法可以通过点符号访问,类似于Java中的静态成员和方法。

英文:

Scala object is like singleton object in Java:

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class Writer {
private Writer(){}
private static Writer instance;
public static Writer getInstance() {
if (instance == null) {
instance = new Writer();
}
return instance;
}
public Map&lt;String, String&gt; options = new HashMap&lt;&gt;();
public Optional&lt;String&gt; a() {
return Optional.ofNullable(options.get(&quot;a&quot;));
}
public Optional&lt;String&gt; b() {
return Optional.ofNullable(options.get(&quot;b&quot;));
}
public Optional&lt;String&gt; c() {
return Optional.ofNullable(options.get(&quot;c&quot;));
}
public Writer option(String key, String value) {
options.put(key, value);
return this;
}
public void write() {
System.out.println(a().orElse(&quot;Z&quot;));
System.out.println(b().orElse(&quot;Z&quot;));
System.out.println(c().orElse(&quot;Z&quot;));
}
public static void main(String[] args) {
Writer.getInstance()
.option(&quot;a&quot;, &quot;A&quot;)
.option(&quot;b&quot;, &quot;B&quot;)
.option(&quot;c&quot;, &quot;C&quot;)
.write();
Writer.getInstance().write(); // same output as above
}
}

huangapple
  • 本文由 发表于 2020年4月8日 12:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61093385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定