有没有模仿图示操作者更容易的选择吗? [英] Is there an easier alternative to mimicking the splat operator?

查看:103
本文介绍了有没有模仿图示操作者更容易的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现它在Ruby中可用,但我承认它从我在Python所做的;的图示操作符。长话短说,我不知道是否有一个更简单的完成什么我目前,模仿图示运营商做什么样的方式。

I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does.

我做了其余的可以打电话,因为我意识到我有几个非常类似的一个中心的方法,他们都在做,除了一些小的东西是一样的。这里的方法签名:

I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature:

private String callScript(String scriptLocation, String... extraArgs) throws Exception {

我想至少需要一个参数( scriptLocation ),然后允许任意数量的额外的参数。我结束了这个做的是创建一个的ProcessBuilder 什么。我的愿望是做这样的事情:

I want to require at least one argument (the scriptLocation), and then allow any number of extra arguments. What I end up doing with this is creating a ProcessBuilder. My desire is to do something like this:

ProcessBuilder pb = new ProcessBuilder("something", scriptLocation, /* extraArgs */);

不过,当然,在的ProcessBuilder 的构造函数只接受喜欢的东西:

But of course, the ProcessBuilder constructor only accepts things like:


  • 列表<串GT;

  • 的String []

  • 字符串...

  • List<String>
  • String[]
  • String...

所以,我的做法显然是行不通的。

So my approach obviously wouldn't work.

我目前的解决方法,这罚款,据我所知的作品,是:

My current workaround, which works fine as far as I know, is:

List<String> finalArgs = new ArrayList<String>();
finalArgs.add("something");
finalArgs.add(scriptLocation);
finalArgs.addAll(Arrays.asList(extraArgs));
ProcessBuilder pb = new ProcessBuilder(finalArgs);

(是的,我明白我没有使用列表键,可以只让一个的String []

(是的,我知道我可以遍历 extraArgs 并将其添加到 finalArgs 单独的,而不是使用的addAll Arrays.asList()

(and yes, I understand I could loop through extraArgs and add them to finalArgs individually, instead of having to use addAll and Arrays.asList())

(是的,我知道我可以通过返回的某些参数与可变参数相结合,有效地完成我的工作区功能)

(and yes, I know I can make a function that effectively accomplishes my workaround by returning certain arguments combined with variable arguments)

所以我想最后这三句话之外,还有什么能做到这一点?

So I guess outside of these last three statements, is there anything that can achieve this?

推荐答案

这取决于你的简单的定义,但你可以写一个类利用Builder模式:

It depends on your definition of simpler, but you could write a class utilizing the Builder pattern:

public class MyProcessBuilder {
    List<String> args;

    MyProcessBuilder( String text, String location ) {
        args = new ArrayList<String>();
        args.add( text );
        args.add( location );
    }

    MyProcessBuilder arg( String arg ) {
        args.add( arg );
        return this;
    }

    MyProcessBuilder args( String... args ) {
        this.args.addAll( Arrays.asList( args ) );
        return this;
    }

    public ProcessBuilder go() {
        return new ProcessBuilder( args );
    }

    public static MyProcessBuilder callScript( String location ) {
        return new MyProcessBuilder( "something", location );
    }

    public static void main( String[] args ) {
        callScript( "/tmp/bla" ).arg( "something else" ).arg( "more" ).go();
    }
}

这篇关于有没有模仿图示操作者更容易的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆