`
sillycat
  • 浏览: 2551634 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Grails(13)Let's Groovy from Java

 
阅读更多
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


分享到:
评论

相关推荐

    eclipse插件grails(groovy)

    Eclipse 插件 Grails(Groovy)是一个强大的开发工具,它使得在Eclipse环境中进行Groovy和Grails应用的开发变得更为便捷。Groovy是一种动态、面向对象的编程语言,而Grails则是一个基于Groovy的开源Web应用框架,...

    Groovy和Grails配置方法

    Groovy和Grails都是基于Java平台的,因此首先需要安装Java。本文档中的版本为JDK 1.6.10,可以从以下地址下载: - 下载地址:`...

    Grails&Groovy学习资料

    Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,提供了丰富的功能,而Groovy则是一种面向Java平台的动态编程语言,它的设计目标是提高开发者的生产力。 Groovy语言: Groovy是Java平台上的一个...

    Groovy轻松入门——Grails实战基础篇

    因为Grails是基于Groovy的,而Groovy又是在Java平台上运行的,所以Java环境是必须的。如果你还没有安装Java环境,请先安装JDK(Java Development Kit)。 接下来,我们将按照以下步骤来搭建Grails环境: 1. **下载...

    Java中使用Groovy的三种方式

    无论是作为嵌入式脚本快速实现功能,还是作为类库扩展Java项目,或者是在Grails或Spring Boot框架中使用,Groovy都能为Java开发者带来诸多便利。掌握Groovy的这些使用方式,能帮助开发者更好地适应现代Java开发环境...

    Grails+groovy 完整参考手册.7z

    在Grails中,开发人员可以利用Groovy的简洁语法和动态特性,同时享受到Java平台的稳定性和可扩展性。 **Groovy 语言基础** Groovy 是一种强大的、动态的、面向对象的编程语言,它与Java兼容并可以在Java虚拟机...

    Beginning Groovy and Grails, From Novice to Professional

    本书《Beginning Groovy and Grails, From Novice to Professional》由Christopher M. Judd、Joseph Faisal Nusairat 和 James Shingler共同编写,并得到了Grails项目负责人Graeme Rocher的前言推荐。本书主要面向...

    Groovy&Grails准备,收集的Groovy与Grails的书籍

    Groovy和Grails是两个密切相关的开源技术,主要用于构建现代、高效的Java平台应用程序。Groovy是一种动态、灵活的编程语言,它与Java高度兼容,但语法更为简洁,提供了更多的灵活性。而Grails则是一个基于Groovy的...

    Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008

    《Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008》这本书深入浅出地介绍了Groovy语言和Grails框架,旨在帮助初学者快速掌握这两项技术并转化为专业人士。Groovy和Grails是Java生态中的...

    Groovy&Grails - 指南.ppt

    与 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

    groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。

    grails和groovy的电子书-2

    文档首先提出为何选择Groovy和Grails的疑问,这引出了几个关键点:生产力、乐趣以及Groovy与Java的紧密关系。Groovy是Java的扩展,被称作“真正的Java 2”,它解决了一些Java开发者认为Java 2不足以应对真实世界问题...

    Groovy and Grails Recipes.pdf

    根据提供的文件信息,我们可以从《Groovy and Grails Recipes》一书中提炼出多个与Groovy语言及Grails框架相关的知识点。下面将详细阐述这些知识点。 ### Groovy编程语言 **1. Groovy简介** - **定义**:Groovy是...

    Grails超全操作手册(中文版)

    Grails超全中文操作手册,无论是大神还是初学者都很适用的操作文档。很全很详细,希望能帮到学习grails框架的你。

    Groovy and Grails Recipes(清晰PDF)

    From Java to Groovy** - **介绍**: 该章节讨论了从Java转向Groovy时的一些关键差异和相似之处。 - **核心知识点**: - Groovy的动态特性与Java的静态特性比较 - Groovy中的类型推断 - 使用Groovy增强现有的Java...

Global site tag (gtag.js) - Google Analytics