More Related Content Similar to Groovy and Java Comparison for Beginners Similar to Groovy and Java Comparison for Beginners (20) More from Kiyotaka Oku (20) Groovy and Java Comparison for Beginners17. String
println 'ls -la /opt/local'.execute().text
total 8
drwxr-xr-x 12 root admin 408 11 23 19:32 .
drwxr-xr-x 3 root admin 102 11 7 14:34 ..
drwxr-xr-x 5 root wheel 170 11 24 13:02 Library
drwxr-xr-x 686 root admin 23324 11 27 18:06 bin
drwxr-xr-x 25 root admin 850 11 24 17:01 etc
drwxr-xr-x 177 root admin 6018 11 24 17:01 include
drwxr-xr-x 988 root admin 33592 11 24 17:01 lib
drwxr-xr-x 22 root admin 748 11 24 16:17 libexec
lrwxr-xr-x 1 65534 wheel 9 11 23 18:42 man -> share/man
drwxr-xr-x 5 root admin 170 11 24 13:37 sbin
drwxr-xr-x 76 root admin 2584 11 27 18:06 share
drwxr-xr-x 8 root admin 272 11 24 13:02 var
18. import
import java.lang
import java.math
import java.io
import java.net
import java.util
import groovy.lang
import groovy.util
19. Java
import org.apache.poi.hssf.usermodel.*
def workBook = new HSSFWorkbook(new File('./foo.xls')
workBook.newInputStream()).sheets.each { sheet ->
sheet.firstRowNum.upto(sheet.lastRowNum) {
sheet.getRow(it).with { row ->
row.firstCellNum.upto(row.lastCellNum - 1) {
println row.getCell(it).stringCellValue
}
}
}
}
21. Ant
def ant = new AntBuilder()
ant.unzip(src: 'xxx.zip', dest:'dest')
ant.mail(mailhost:'hostname', subject:'hello',
charset:'utf-8',
user:user,
password:password) {
from address:'xxx@example.com'
to address:'kiy0taka333@gmail.com'
message 'Hello World!'
}
24. // Groovy
$ time groovy -e "println 'Hello'"
Hello
real!0m1.292s
user!0m1.283s
sys! 0m0.192s
// GroovyServ
$ time groovyclient -e "println 'Hello'"
Hello
real!0m0.036s
user!0m0.001s
sys! 0m0.003s
26. Swing(Java)
contentPane.add(button);
package sample;
setDefaultCloseOperation(EXIT_ON_CLOSE);
import java.awt.Container; pack();
import java.awt.GridLayout; setVisible(true);
import java.awt.event.ActionEvent; }
import java.awt.event.ActionListener;
public static void main(String[] args) {
import javax.swing.JButton; SwingUtilities.invokeLater(new Runnable() {
import javax.swing.JFrame; public void run() {
import javax.swing.JLabel; new Hello();
import javax.swing.JTextArea; }
import javax.swing.SwingUtilities; });
}
public class Hello extends JFrame { }
public Hello() {
super("Hello");
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 1));
JLabel label = new JLabel("Label");
contentPane.add(label);
JTextArea textArea = new JTextArea("Text Area");
textArea.setColumns(20);
textArea.setRows(2);
contentPane.add(textArea);
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
...
}
});
34. show = { println it }
square_root = { Math.sqrt(it) }
def please(action) {
[the: { what ->
[of: { n -> action(what(n)) }]
}]
}
please(show).the(square_root).of(100)
35. show = { println it }
square_root = { Math.sqrt(it) }
def please(action) {
[the: { what ->
[of: { n -> action(what(n)) }]
}]
}
please show the square_root of 100
36. Object.metaClass. =
Object.metaClass. =
{ clos -> clos(delegate) }
= { it }
= { println it }
= { Math.sqrt(it) }
100
41. import groovy.json.* def slurper = new JsonSlurper()
def json = slurper.parseText(text)
def text = '''[ assert json == [
["aaa", "bbb", "ccc"], ["aaa", "bbb", "ccc"],
{ [
"key1" : "value1", "key1" : "value1",
"key2" : "value2", "key2" : "value2",
"key3" : "value3", "key3" : "value3",
"key4" : "" "key4" : ""
}, ],
["ddd", "eee", "fff"] ["ddd", "eee", "fff"]
] ]
'''
42. import groovy.json.*
def json = new JsonBuilder()
json (
["aaa", "bbb", "ccc"],
[
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : ""
],
["ddd", "eee", "fff"]
)
println json
45. def plus2 = { it + 2 }
def times3 = { it * 3 }
def times3plus2 = plus2 << times3
assert times3plus2(3) == 11
assert times3plus2(4) == plus2(times3(4))
def plus2times3 = times3 << plus2
assert plus2times3(3) == 15
assert plus2times3(5) == times3(plus2(5))
assert times3plus2(3) == (times3 >> plus2)(3)
49. @Log @Commons @Log4j @Slf4j
@Field @PackageScope
@AutoClone @AutoExternalizable
@ThreadInterrupt @TimedInterrupt
@ConditionalInterrupt @ToString
@EqualsAndHashCode @TupleConstructor
@Canonical @InheritConstructors
@WithReadLock @WithWriteLock
@ListenerList
52. create-app generate-all
create-controller generate-controller
create-domain-class generate-views
create-script package
create-service run-app
create-tag-lib run-war
create-unit-test test-app
66. datastore blobstore
memcache oauth
urlFetch namespace
mail capabilities
images channel
users files
user backends
defaultQueue lifecycle
queues localMode
xmpp app
67. Gaelyk:Datastore
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*
import static com.google.appengine.api.datastore.Query.FilterOperator.*
import static com.google.appengine.api.datastore.Query.SortDirection.*
def query = new Query('person').with {
addFilter 'age', GREATER_THAN_OR_EQUAL, 30
addSort 'name', ASCENDING
}
def persons = datastore.prepare(query).asList(withLimit(21))
69. Gaelyk:TaskQueue
defaultQueue << [
countdownMillis: 1000, url: "/task/dailyEmail",
taskName: "Send daily email newsletter",
method: 'PUT', params: [date: '20090914'],
payload: content
]
79. $ gradle clean build
:clean
:compileJava
:compileGroovy
:processResources
:classes
:jar
:assemble
:compileTestJava
:compileTestGroovy
:processTestResources
:testClasses
:test
:check
:build
BUILD SUCCESSFUL
Total time: 13.766 secs
80. task checksum << {
fileList('../antLoadfileResources').each {File file ->
ant.checksum(file: file, property: "cs_$file.name")
println "$file.name Checksum: ${ant.properties["cs_$file.name"]}"
}
}
task loadfile << {
fileList('../antLoadfileResources').each {File file ->
ant.loadfile(srcFile: file, property: file.name)
println "I'm fond of $file.name"
}
}
File[] fileList(String dir) {
file(dir).listFiles({file -> file.isFile() } as FileFilter).sort()
}