我可以在 groovy 中的单个日期范围内调用 get(i) 或 getFrom() 吗?

发布于 2024-11-26 14:23:03 字数 1183 浏览 1 评论 0原文

我有一个需要 groovy 范围的方法,并且传递类似的内容没有问题

新日期(“01/01/1999”)..新日期(“01/01/1999”)

但我更喜欢传递单个日期(作为范围)

当我打印出来时,它看起来不错

范围范围=开始日期作为范围

这显示在控制台中

[1999 年 1 月 1 日星期五 00:00:00 CST]

但是现在当我尝试执行 .get(i) 或 .getFrom() 时,它失败了,说

groovy.lang.MissingMethodException: No signature of method: java.util.Date.getFrom() is applicable for argument types: () values: []
Possible solutions: getDate(), getDay(), getTime(), getYear(), before(java.util.Date), getAt(int)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.InvokerHelper$invokeMethod.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at Date_delegateProxy.getFr

Anybody has success attempts to Cast and use a single date as Range in groovy?

I have a method that requires a range in groovy and I've got no problem passing in something like

new Date("01/01/1999")..new Date("01/01/1999")

But I would much prefer to pass in a single date (as Range)

When I print this out it looks good

Range range = startDate as Range

This shows up in the console

[Fri Jan 01 00:00:00 CST 1999]

But now when I try to do a .get(i) or .getFrom() it fails saying

groovy.lang.MissingMethodException: No signature of method: java.util.Date.getFrom() is applicable for argument types: () values: []
Possible solutions: getDate(), getDay(), getTime(), getYear(), before(java.util.Date), getAt(int)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.InvokerHelper$invokeMethod.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at Date_delegateProxy.getFr

Anyone have success trying to cast and use a single date as Range in groovy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-03 14:23:03

在 Groovy 中,as 运算符使用您想要将其转换为参数的类来调用对象上的 asType 方法。 DefaultGroovyMethods 提供了我认为 Groovy 附带的所有实现据我所知,它们都没有转换为范围。

如果需要,您可以覆盖 asType 以支持 Range,但是我认为大多数人会认为滥用运算符重载和如此糟糕的做法,以至于我实际上不愿意提供一个示例。尽管如此,这应该可以满足您的要求。

// Save the original asType method so that it can be called by the overridden one
final f = Date.metaClass.getMetaMethod('asType', [Class] as Class[])

// Replace the default asType method for Date objects
Date.metaClass.asType = { final Class it ->
    // For ranges convert the date into a range with the date as both the start
    // and end. For other types, use the default implementation of asType
    return it == Range? (delegate..delegate) : f.invoke(delegate, it)
}

final start = new Date()
final end   = start

assert start..end == start as Range

In Groovy the as operator calls the asType method on the object with the class you want to convert it to as an argument. DefaultGroovyMethods provides all the implementations that ship with Groovy that I'm aware of, none of which convert to Range.

You can if needed override asType to support Range, however I think most people would consider that abuse of operator overloading and such poor practice that I'm actually hesitant to provide an example. None the less, this should do what you're asking.

// Save the original asType method so that it can be called by the overridden one
final f = Date.metaClass.getMetaMethod('asType', [Class] as Class[])

// Replace the default asType method for Date objects
Date.metaClass.asType = { final Class it ->
    // For ranges convert the date into a range with the date as both the start
    // and end. For other types, use the default implementation of asType
    return it == Range? (delegate..delegate) : f.invoke(delegate, it)
}

final start = new Date()
final end   = start

assert start..end == start as Range
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文