Last active
August 29, 2015 14:15
-
-
Save int128/1cecf05bab03f76cd8ea to your computer and use it in GitHub Desktop.
Method Overload with Map and Closure in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@groovy.transform.ToString() | |
class Args { | |
Collection items | |
Map settings | |
Closure closure | |
static Args parse(Object[] objects) { | |
def args = new Args() | |
def stack = objects as LinkedList | |
if (stack.last() instanceof Closure) { | |
args.closure = stack.last() | |
stack.removeLast() | |
} | |
if (stack.head() instanceof Map) { | |
args.settings = stack.head() | |
stack.remove() | |
} | |
if (stack.size() == 1 && stack.head() instanceof Collection) { | |
args.items = stack.head() | |
} else { | |
args.items = stack | |
} | |
args | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hoge(String arg) { | |
println "1: $arg" | |
} | |
def hoge(String arg, Closure callback) { | |
println "2: $arg, ${callback.toString()}" | |
} | |
def hoge(HashMap map, String arg) { | |
println "3: $arg, $map" | |
} | |
def hoge(HashMap map, String arg, Closure callback) { | |
println "4: $arg, $map, ${callback.toString()}" | |
} | |
def hoge(Collection<String> args) { | |
println "5: $args" | |
} | |
def hoge(Collection<String> args, Closure callback) { | |
println "6: $args, ${callback.toString()}" | |
} | |
def hoge(HashMap map, Collection<String> args) { | |
println "7: $args, $map" | |
} | |
def hoge(HashMap map, Collection<String> args, Closure callback) { | |
println "8: $args, $map, ${callback.toString()}" | |
} | |
def hoge(Object[] args) { | |
println "0: $args" | |
} | |
hoge 'aaa' | |
// => 1: aaa | |
hoge('aaa') { 100 } | |
// => 2: aaa, Script1$_run_closure1@1c874c0 | |
hoge 'aaa', dry: true, wet: false | |
// => 3: aaa, [dry:true, wet:false] | |
hoge('aaa', dry: true, wet: false) { 100 } | |
// => 4: aaa, [dry:true, wet:false], Script1$_run_closure2@e2119f | |
hoge(['aaa', 'bbb', 'ccc']) | |
// => 5: [aaa, bbb, ccc] | |
hoge(['aaa', 'bbb']) { 100 } | |
// => 6: [aaa, bbb], Script1$_run_closure3@715b44 | |
hoge(['aaa', 'bbb'], dry: true, wet: false) | |
// => 7: [aaa, bbb], [dry:true, wet:false] | |
hoge(['aaa', 'bbb'], dry: true, wet: false) { 100 } | |
// => 8: [aaa, bbb], [dry:true, wet:false], Script1$_run_closure4@14ed51 | |
hoge 'aaa', 'bbb', 'ccc' | |
// => 0: [aaa, bbb, ccc] | |
hoge 'aaa', 'bbb', dry: true, wet: false | |
// => 0: [[dry:true, wet:false], aaa, bbb] | |
hoge('aaa', 'bbb') { 100 } | |
// => 0: [aaa, bbb, Script1$_run_closure1@1b79d3c] | |
hoge('aaa', 'bbb', dry: true, wet: false) { 100 } | |
// => 0: [[dry:true, wet:false], aaa, bbb, Script1$_run_closure2@478e61] | |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hoge(Object[] args) { | |
println "0: $args" | |
} | |
hoge 'aaa' | |
// => 0: [aaa] | |
hoge 'aaa', dry: true, wet: false | |
// => 0: [[dry:true, wet:false], aaa] | |
hoge('aaa') { 100 } | |
// => 0: [aaa, Script1$_run_closure1@109e765] | |
hoge('aaa', dry: true, wet: false) { 100 } | |
// => 0: [[dry:true, wet:false], aaa, Script1$_run_closure2@78dd18] | |
hoge(['aaa', 'bbb', 'ccc']) | |
// => 0: [[aaa, bbb, ccc]] | |
hoge(['aaa', 'bbb'], dry: true, wet: false) | |
// => 0: [[dry:true, wet:false], [aaa, bbb]] | |
hoge(['aaa', 'bbb']) { 100 } | |
// => 0: [[aaa, bbb], Script1$_run_closure3@5d99ce] | |
hoge(['aaa', 'bbb'], dry: true, wet: false) { 100 } | |
// => 0: [[dry:true, wet:false], [aaa, bbb], Script1$_run_closure4@a24ac8] | |
hoge 'aaa', 'bbb', 'ccc' | |
// => 0: [aaa, bbb, ccc] | |
hoge 'aaa', 'bbb', dry: true, wet: false | |
// => 0: [[dry:true, wet:false], aaa, bbb] | |
hoge('aaa', 'bbb') { 100 } | |
// => 0: [aaa, bbb, Script1$_run_closure5@14e985a] | |
hoge('aaa', 'bbb', dry: true, wet: false) { 100 } | |
// => 0: [[dry:true, wet:false], aaa, bbb, Script1$_run_closure6@b37808] | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment