在Groovy中获取地图的方法
在Config.groovy中:
In Config.groovy:
Query {
parameterMap {
time.'1328' = ['T1']
time.'1329' = ['T2']
templates.'T1' = ['X','Y','Z']
templates.'T2' = ['Z']
}
}
在QuartzSchedulerJob I中想要使用 templateId
的 T1
和 T2
作为键在 parameterMap
中获取相应的值,但它返回 null
和 [:]
,分别为 parameterMap.templates.get( $ {v})
,但我想获取 ['X','
和 T1
和Y','Z'] ['Z']
T2
In QuartzSchedulerJob I want to use T1
and T2
which are templateId
as key to get the corresponding values in parameterMap
but it returns me null
and [:]
respectively, parameterMap.templates.get("${v}")
but I want to get ['X','Y','Z']
and ['Z']
respectively for T1
and T2
def execute() {
def currentTime = new Date().format('HHmm')
grailsApplication.config.Query.parameterMap.time.each { k, v ->
if (currentTime=="${k}") {
println "${k} " + currentTime
String templateId = "${v}".replaceAll("\\[", "").replaceAll("\\]","")
println grailsApplication.config.Query.parameterMap.templates.get("${v}") //this prints null
println grailsApplication.config.Query.parameterMap.templates.getAt("${v}") //this prints [:]
}
}
}
$ {k}
是一个怪异的表达式,在Groovy代码中我经常看到它。我很想知道是谁和/或什么(书/博客/ ???)使人们使用了这种语法。我经常在打算返回类字段作为值的 toString
方法中看到它,例如
"${k}"
is a weird expression, and I see it a lot in Groovy code. I would love to know who and/or what (book/blog/???) somehow got people using that syntax. I see it quite often in toString
methods that intend to return a class field as the value, e.g.
private String name
...
String toString() {
"${name}"
}
为什么不只返回变量?
String toString() {
name
}
您正在执行此操作在这里,制作一个 GString
$ {k}
仅包含一个嵌入式变量,而没有其他字符,然后在将它与 currentTime
(可以工作,但远非最佳或明智的方法)进行比较并使用此 GString $ c时,将其隐式转换为String $ c>在调用
中的潜在键code> getAt ,想知道为什么这个奇怪的小 get
和 Map GString
中的值非常接近作为地图键(例如Tim指出时间
地图的值是单元素 List
s,而 templates
映射的键是 String 这些
List
s中的单个元素code> s也不会自动转换成您想要的样子。
You're doing this here, making a GString
"${k}"
with only a single embedded variable and no other characters, and then implicitly converting it to a String when comparing it to currentTime
(which works, but is far from optimal or sensible) and using this GString
to generate (in a very funky way) to a potential key in a Map
when calling get
and getAt
, and wondering why the value inside this weird little GString
, which is very close to being a map key (as Tim points out the values of the time
map are single-element List
s, and the keys of the templates
map are String
s which are the single elements of these List
s) isn't also automagically converted into what you wanted it to have been.
Groovy进行了大量隐式转换,但是 Map
中 get
方法的签名>是 Object get(Object key)
(从技术上讲,它有一个通用的返回类型,但这在这里不相关),因此Groovy不会为您做任何转换-您将 Object
作为参数传递给采用 Object
的方法。完全没有理由期望它会被转换为String,或者转换为任何键的任何类型,或者转换为任何东西。
Groovy does lots of implicit conversions, but the signature of the get
method in Map
is Object get(Object key)
(technically it's got a generic return type, but that's not relevant here) so Groovy isn't going to do any conversion for you - you're passing an Object
as a parameter to a method that takes Object
. There's no reason at all to expect that it would be converted to a String, or to any of the types of any of the keys, or to anything at all.
loc伤害了我的大脑:
This loc hurts my brain:
String templateId = "${v}".replaceAll("\\[", "").replaceAll("\\]","")
您正在列出 v
,将其与其他内容一起嵌入 GString
,强制Groovy将 GString
转换为 String
,以便它可以调用 replaceAll
,以删除 AbstractCollection toString
形式的开头和结尾的方括号/ code>,以提取 v
中的单个 String
元素。
You're taking the list v
, converting it to a String
by embedding it with nothing else in a GString
, coercing Groovy to convert the GString
to a String
so it can call replaceAll
on it, to remove the brackets that are at the beginning and end of the toString
form of AbstractCollection
, to extract the single String
element in v
.
如何更直接一些:
String templateId = v[0]
及其在清理后的 execute
版本中的使用:
and its use in a cleaned up version of execute
:
def execute() {
String currentTime = new Date().format('HHmm')
grailsApplication.config.Query.parameterMap.time.each { k, v ->
if (currentTime == k) {
println "$k $currentTime"
String templateId = v[0]
println config.Query.parameterMap.templates[templateId] // get and getAt would also work
}
}
}