If statement Groovy中if-else语句系列的压缩

If statement Groovy中if-else语句系列的压缩,if-statement,methods,groovy,If Statement,Methods,Groovy,我在Groovy中有一系列if-else语句: String invoke = params?.target AuxService aux = new AuxService() def output if(invoke?.equalsIgnoreCase("major")) { output = aux.major() } else if(invoke?.equalsIgnoreCase("minor")) { output = aux.minor() } else if(in

我在
Groovy
中有一系列
if-else
语句:

String invoke = params?.target
AuxService aux = new AuxService()
def output

if(invoke?.equalsIgnoreCase("major")) {
    output = aux.major()
}
else if(invoke?.equalsIgnoreCase("minor")) {
    output = aux.minor()
}
else if(invoke?.equalsIgnoreCase("repository")) {
    output = aux.repository()
}
...
else if(invoke?.equalsIgnoreCase("temp")) {
    output = aux.temp()
}
else {
    output = aux.propagate()
}
省略号包含另外14组
if-else
语句,总共19个。您可以看到,根据
invoke
的值,将从
AuxService
调用的方法。现在,我考虑以下方法来减少行数:

String invoke = params?.target()
AuxService aux = new AuxService()
def output = aux?."$invoke"() ?: aux.propagate()

但我认为第三行可能行不通,它看起来很不传统。我只是预感,我认为这条线容易出错。这是一个有效的代码,还是有更好的方法来压缩这些行?

首先,您的代码是Groovy中的avlid。但是,如果需要
equalsIgnoreCase
,简化后的代码将无法工作。如果params为null,则相同,因为invoke将为null。但我认为你的基本想法是对的。所以我要做的是制作一个映射(最终的静态某处),用大写的方法作为字符串键,用正确大小写中的真实方法作为字符串值。然后您可以使用它来确保不同情况下的正确性。空处理我将单独解决:

def methodsMap = ["MAJOR":"major",/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap[invoke?.toUpperCase()]
def output = methodName ? aux."$methodName"() : aux.propagate()
稍微不同的方法是在映射中使用闭包值。我个人觉得这有点过分,但它允许您做的不仅仅是简单的调用

def methodsMap = ["MAJOR":{it.major()},/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def stub = methodsMap[invoke?.toUpperCase()]
def output = stub==null ? stub(aux) : aux.propagate()
我曾考虑使用带有默认值的映射,但由于这将创建一个新条目,所以我决定不使用。这可能会导致内存问题。在Java8中,您可以使用Map#getOrDefault:

String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap.getOrDefault(invoke?.toUpperCase(), "propagate")
def output = aux."$methodName"()

首先,您的代码是Groovy中的avlid。但是,如果需要
equalsIgnoreCase
,简化后的代码将无法工作。如果params为null,则相同,因为invoke将为null。但我认为你的基本想法是对的。所以我要做的是制作一个映射(最终的静态某处),用大写的方法作为字符串键,用正确大小写中的真实方法作为字符串值。然后您可以使用它来确保不同情况下的正确性。空处理我将单独解决:

def methodsMap = ["MAJOR":"major",/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap[invoke?.toUpperCase()]
def output = methodName ? aux."$methodName"() : aux.propagate()
稍微不同的方法是在映射中使用闭包值。我个人觉得这有点过分,但它允许您做的不仅仅是简单的调用

def methodsMap = ["MAJOR":{it.major()},/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def stub = methodsMap[invoke?.toUpperCase()]
def output = stub==null ? stub(aux) : aux.propagate()
我曾考虑使用带有默认值的映射,但由于这将创建一个新条目,所以我决定不使用。这可能会导致内存问题。在Java8中,您可以使用Map#getOrDefault:

String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap.getOrDefault(invoke?.toUpperCase(), "propagate")
def output = aux."$methodName"()

首先,您的代码是Groovy中的avlid。但是,如果需要
equalsIgnoreCase
,简化后的代码将无法工作。如果params为null,则相同,因为invoke将为null。但我认为你的基本想法是对的。所以我要做的是制作一个映射(最终的静态某处),用大写的方法作为字符串键,用正确大小写中的真实方法作为字符串值。然后您可以使用它来确保不同情况下的正确性。空处理我将单独解决:

def methodsMap = ["MAJOR":"major",/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap[invoke?.toUpperCase()]
def output = methodName ? aux."$methodName"() : aux.propagate()
稍微不同的方法是在映射中使用闭包值。我个人觉得这有点过分,但它允许您做的不仅仅是简单的调用

def methodsMap = ["MAJOR":{it.major()},/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def stub = methodsMap[invoke?.toUpperCase()]
def output = stub==null ? stub(aux) : aux.propagate()
我曾考虑使用带有默认值的映射,但由于这将创建一个新条目,所以我决定不使用。这可能会导致内存问题。在Java8中,您可以使用Map#getOrDefault:

String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap.getOrDefault(invoke?.toUpperCase(), "propagate")
def output = aux."$methodName"()

首先,您的代码是Groovy中的avlid。但是,如果需要
equalsIgnoreCase
,简化后的代码将无法工作。如果params为null,则相同,因为invoke将为null。但我认为你的基本想法是对的。所以我要做的是制作一个映射(最终的静态某处),用大写的方法作为字符串键,用正确大小写中的真实方法作为字符串值。然后您可以使用它来确保不同情况下的正确性。空处理我将单独解决:

def methodsMap = ["MAJOR":"major",/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap[invoke?.toUpperCase()]
def output = methodName ? aux."$methodName"() : aux.propagate()
稍微不同的方法是在映射中使用闭包值。我个人觉得这有点过分,但它允许您做的不仅仅是简单的调用

def methodsMap = ["MAJOR":{it.major()},/* more mappings here */]
String invoke = params?.target()
AuxService aux = new AuxService()
def stub = methodsMap[invoke?.toUpperCase()]
def output = stub==null ? stub(aux) : aux.propagate()
我曾考虑使用带有默认值的映射,但由于这将创建一个新条目,所以我决定不使用。这可能会导致内存问题。在Java8中,您可以使用Map#getOrDefault:

String invoke = params?.target()
AuxService aux = new AuxService()
def methodName = methodsMap.getOrDefault(invoke?.toUpperCase(), "propagate")
def output = aux."$methodName"()

在使用之前只需测试
字符串调用
。请注意,
aux
不会为空,因此无需使用安全导航(
?。


注意:如果输入在类
AuxService

中不包含有效的方法,则此操作将失败。在使用它之前,只需测试
字符串调用。请注意,
aux
不会为空,因此无需使用安全导航(
?。


注意:如果输入在类
AuxService

中不包含有效的方法,则此操作将失败。在使用它之前,只需测试
字符串调用。请注意,
aux
不会为空,因此无需使用安全导航(
?。


注意:如果输入在类
AuxService

中不包含有效的方法,则此操作将失败。在使用它之前,只需测试
字符串调用。请注意,
aux
不会为空,因此无需使用安全导航(
?。


注意:如果输入在类
AuxService

中不包含有效的方法,则此操作将失败。Elvis运算符用于缩短等效的Java三元运算符表达式

比如说,

def nationality = (user.nationality!=null) ? user.nationality : "undefined"
可以使用Elvis操作符缩短到

def nationality = user.nationality ?: "undefined"
请注意,Elvis运算符计算“?”符号左侧的表达式。如果结果为非null,则立即返回结果,否则对“:”符号右侧的表达式求值并返回结果

这意味着,如果条件的计算结果为真,则不能使用Elvis运算符在“?”符号的右侧执行一些额外的逻辑。所以,三元表达式

user.nationality ? reportCitizen(user) : reportAlien(user)
不能(直接)使用Elvis运算符表示

回到最初的问题,Elvis操作符不能(直接)应用于检查对象上是否存在方法并在存在时调用它。所以

def output = aux?."$invoke"() ?: aux.propagate()
不会像e一样工作