- 浏览: 2551884 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Grails(13)Let's Groovy from Java
Tutorial 3 Classes and Objects
Objects are collections of related code and data.
A class is a higher level description of an object.
Tutorial 4 Regular expressions basics
Regular Expressions
Regular expressions are the Swiss Army knife of text processing.
"potato" ==~ /potato/ //true
==~ is almost like ==, but it is not saying that they are equal, but check if they are match.
And regular expression is enclosed in /'s.
println "potato" ==~ /potatoe?s?/ //true, ? means maybe.
(ie|ei) means (ie) or (ei).
[abcd] //means one of the 4
[abcd]? //means one of the 4 or empty
[^abcd] //not a, b, c, d
Regular Expression Operators
println "potato" ==~ /^p.+/ //^ without [] means begin with character p
println "potato" ==~ /.+o$/ //$ at the end means end with o
Tutorial 5 Capturing regex groups
Capture groups
One of the most useful features of Groovy is the ability to use regular expressions to 'capture' data out of a regular expression.
cardnumber = "carl, kiko, 100 12 15 10"
cardRegular = /([a-zA-Z]+), ([a-zA-Z]+), ([0-9]{3}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2})/
matcher = ( cardnumber =~ cardRegular )
if (matcher.matches()) {
println(matcher.getCount()); //1
println(matcher[0][1]) //carl
println(matcher[0][5]) //15
println(matcher[0][0]) //carl, kiko, 100 12 15 10
}
There are 2 dimension in matcher, we match only once, so there is only one matcher[0]
Non-matching Groups
Here comes an example both working with closure and regular expression.
names = [
"Carl Luo",
"YiYi Kang"
]
printClosure = {
matcher = (it =~ /(.*)(?: .*)* (.*)/);
if(matcher.matches()){
println(matcher[0][2] + "," + matcher[0][1])
}
}
names.each(printClosure);
// Luo Carl
// Kang YiYi
?: is very import here, that means that don't capture the middle group.
Replacement
We do that in Java on java.util.regex.Matcher.
content = "Carl does not like sleep. " +
"Carl likes to play basketball.";
matcher = (content =~ /Carl/);
content = matcher.replaceAll("Sillycat");
println content // just change all the names to Sillycat
Reluctant Operators
The operators ?, +, and * are by default "greedy".
Simply add ? after operators to make them reluctant, *?, +? and ??.
Tutorial 6 Groovy SQL
Groovy SQL
language = "Groovy"
println "You should use ${language}"
Groovy interprets anything inside ${} as a groovy expression.
Performing a simple query
sql.eachRow("select * from tableName"){println "$it.id -- ${it.firstName} --" }
Retrieving a single value from DB
row = sql.firstRow('select columnA, columnB from tableName')
println "Row: columnA = ${row.columnA} and column = ${row.columnB}"
Doing more complex queries
firstName = 'first'
lastName = 'last'
sql.execute("insert into people (firstName, lastName) values (${firstName}, ${lastName})")
sql.execute('delete from word where word_id = ?' , [5])
Other Tips
Groovy return the last statement.
def getPersons(){
def persons = [] //empty list
sql.eachRow("select * from Person"){
//persons << it.toRowResult()
Person p = new Person( it.toRowResult() )
persons << p
}
persons
}
Differences from Java
Default imports
java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*
Common gotchas
1. == means equals
2. in is keyword
3. for (int i = 0;i< len; i++) {….} ====> for (i in 0..len-1) {…}
for (i in 0..<len) { …. }
len.times { …. }
Things to be aware of
1. semicolons are optional
2. return keyword is optional
3. this keyword means this class in static methods.
4. methods and classes are public by default
5. There is no compile errors before runtime.
Uncommon Gotchas
References:
http://groovy.codehaus.org/Getting+Started+Guide
http://groovy.codehaus.org/Tutorial+3+-+Classes+and+Objects
http://groovy.codehaus.org/Tutorial+4+-+Regular+expressions+basics
http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL
http://groovy.codehaus.org/groovy-jdk/
http://groovy.codehaus.org/Differences+from+Java
http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers
Tutorial 3 Classes and Objects
Objects are collections of related code and data.
A class is a higher level description of an object.
Tutorial 4 Regular expressions basics
Regular Expressions
Regular expressions are the Swiss Army knife of text processing.
"potato" ==~ /potato/ //true
==~ is almost like ==, but it is not saying that they are equal, but check if they are match.
And regular expression is enclosed in /'s.
println "potato" ==~ /potatoe?s?/ //true, ? means maybe.
(ie|ei) means (ie) or (ei).
[abcd] //means one of the 4
[abcd]? //means one of the 4 or empty
[^abcd] //not a, b, c, d
Regular Expression Operators
println "potato" ==~ /^p.+/ //^ without [] means begin with character p
println "potato" ==~ /.+o$/ //$ at the end means end with o
Tutorial 5 Capturing regex groups
Capture groups
One of the most useful features of Groovy is the ability to use regular expressions to 'capture' data out of a regular expression.
cardnumber = "carl, kiko, 100 12 15 10"
cardRegular = /([a-zA-Z]+), ([a-zA-Z]+), ([0-9]{3}) ([0-9]{2}) ([0-9]{2}) ([0-9]{2})/
matcher = ( cardnumber =~ cardRegular )
if (matcher.matches()) {
println(matcher.getCount()); //1
println(matcher[0][1]) //carl
println(matcher[0][5]) //15
println(matcher[0][0]) //carl, kiko, 100 12 15 10
}
There are 2 dimension in matcher, we match only once, so there is only one matcher[0]
Non-matching Groups
Here comes an example both working with closure and regular expression.
names = [
"Carl Luo",
"YiYi Kang"
]
printClosure = {
matcher = (it =~ /(.*)(?: .*)* (.*)/);
if(matcher.matches()){
println(matcher[0][2] + "," + matcher[0][1])
}
}
names.each(printClosure);
// Luo Carl
// Kang YiYi
?: is very import here, that means that don't capture the middle group.
Replacement
We do that in Java on java.util.regex.Matcher.
content = "Carl does not like sleep. " +
"Carl likes to play basketball.";
matcher = (content =~ /Carl/);
content = matcher.replaceAll("Sillycat");
println content // just change all the names to Sillycat
Reluctant Operators
The operators ?, +, and * are by default "greedy".
Simply add ? after operators to make them reluctant, *?, +? and ??.
Tutorial 6 Groovy SQL
Groovy SQL
language = "Groovy"
println "You should use ${language}"
Groovy interprets anything inside ${} as a groovy expression.
Performing a simple query
sql.eachRow("select * from tableName"){println "$it.id -- ${it.firstName} --" }
Retrieving a single value from DB
row = sql.firstRow('select columnA, columnB from tableName')
println "Row: columnA = ${row.columnA} and column = ${row.columnB}"
Doing more complex queries
firstName = 'first'
lastName = 'last'
sql.execute("insert into people (firstName, lastName) values (${firstName}, ${lastName})")
sql.execute('delete from word where word_id = ?' , [5])
Other Tips
Groovy return the last statement.
def getPersons(){
def persons = [] //empty list
sql.eachRow("select * from Person"){
//persons << it.toRowResult()
Person p = new Person( it.toRowResult() )
persons << p
}
persons
}
Differences from Java
Default imports
java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*
Common gotchas
1. == means equals
2. in is keyword
3. for (int i = 0;i< len; i++) {….} ====> for (i in 0..len-1) {…}
for (i in 0..<len) { …. }
len.times { …. }
Things to be aware of
1. semicolons are optional
2. return keyword is optional
3. this keyword means this class in static methods.
4. methods and classes are public by default
5. There is no compile errors before runtime.
Uncommon Gotchas
References:
http://groovy.codehaus.org/Getting+Started+Guide
http://groovy.codehaus.org/Tutorial+3+-+Classes+and+Objects
http://groovy.codehaus.org/Tutorial+4+-+Regular+expressions+basics
http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL
http://groovy.codehaus.org/groovy-jdk/
http://groovy.codehaus.org/Differences+from+Java
http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 247GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 451GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 265Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
Eclipse 插件 Grails(Groovy)是一个强大的开发工具,它使得在Eclipse环境中进行Groovy和Grails应用的开发变得更为便捷。Groovy是一种动态、面向对象的编程语言,而Grails则是一个基于Groovy的开源Web应用框架,...
Groovy和Grails都是基于Java平台的,因此首先需要安装Java。本文档中的版本为JDK 1.6.10,可以从以下地址下载: - 下载地址:`...
Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,提供了丰富的功能,而Groovy则是一种面向Java平台的动态编程语言,它的设计目标是提高开发者的生产力。 Groovy语言: Groovy是Java平台上的一个...
因为Grails是基于Groovy的,而Groovy又是在Java平台上运行的,所以Java环境是必须的。如果你还没有安装Java环境,请先安装JDK(Java Development Kit)。 接下来,我们将按照以下步骤来搭建Grails环境: 1. **下载...
无论是作为嵌入式脚本快速实现功能,还是作为类库扩展Java项目,或者是在Grails或Spring Boot框架中使用,Groovy都能为Java开发者带来诸多便利。掌握Groovy的这些使用方式,能帮助开发者更好地适应现代Java开发环境...
在Grails中,开发人员可以利用Groovy的简洁语法和动态特性,同时享受到Java平台的稳定性和可扩展性。 **Groovy 语言基础** Groovy 是一种强大的、动态的、面向对象的编程语言,它与Java兼容并可以在Java虚拟机...
本书《Beginning Groovy and Grails, From Novice to Professional》由Christopher M. Judd、Joseph Faisal Nusairat 和 James Shingler共同编写,并得到了Grails项目负责人Graeme Rocher的前言推荐。本书主要面向...
Groovy和Grails是两个密切相关的开源技术,主要用于构建现代、高效的Java平台应用程序。Groovy是一种动态、灵活的编程语言,它与Java高度兼容,但语法更为简洁,提供了更多的灵活性。而Grails则是一个基于Groovy的...
《Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008》这本书深入浅出地介绍了Groovy语言和Grails框架,旨在帮助初学者快速掌握这两项技术并转化为专业人士。Groovy和Grails是Java生态中的...
与 Java 相比,Groovy 更加简洁,而与 Ruby on Rails 相比,Grails 更加适合已经在 Java 生态系统中工作的开发者,因为它可以无缝集成现有的 Java 库和技术栈。通过使用 Grails,开发者能够利用 Groovy 的优势,如 ...
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。
文档首先提出为何选择Groovy和Grails的疑问,这引出了几个关键点:生产力、乐趣以及Groovy与Java的紧密关系。Groovy是Java的扩展,被称作“真正的Java 2”,它解决了一些Java开发者认为Java 2不足以应对真实世界问题...
根据提供的文件信息,我们可以从《Groovy and Grails Recipes》一书中提炼出多个与Groovy语言及Grails框架相关的知识点。下面将详细阐述这些知识点。 ### Groovy编程语言 **1. Groovy简介** - **定义**:Groovy是...
Grails超全中文操作手册,无论是大神还是初学者都很适用的操作文档。很全很详细,希望能帮到学习grails框架的你。
From Java to Groovy** - **介绍**: 该章节讨论了从Java转向Groovy时的一些关键差异和相似之处。 - **核心知识点**: - Groovy的动态特性与Java的静态特性比较 - Groovy中的类型推断 - 使用Groovy增强现有的Java...