Skip to content

Commit

Permalink
2018.2 Groovy examples (Groovy 2.5 and 3.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
trishagee committed Jul 17, 2018
1 parent 9fcd459 commit 25e6aad
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.idea/workspace.xml
spring-tricks/target/
standard-java/target/
groovy/out
groovy/build
5 changes: 2 additions & 3 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions .idea/modules/groovy-demo_main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 43 additions & 3 deletions .idea/modules/groovy-demo_test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
plugins {
id 'groovy'
}
apply plugin: 'groovy'

group 'com.jetbrains'
version '1.0-SNAPSHOT'
sourceCompatibility = 8

repositories {
mavenCentral()
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile 'org.codehaus.groovy:groovy-all:2.5.0'
compile 'org.codehaus.groovy:groovy-all:3.0.0-alpha-3'
compile 'javax.xml.bind:jaxb-api:2.3.0' // needed by Groovy
testCompile 'org.junit.jupiter:junit-jupiter-api:5.2.0'


}
28 changes: 28 additions & 0 deletions groovy/src/main/groovy/DefaultMethods.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
interface Greetable {
String target()

default String salutation() {
'Greetings'
}

default String greet() {
"${salutation()}, ${target()}"
}
}

class Friend implements Greetable {
String name

@Override
String target() {
return name
}

@Override
String salutation() {
return 'Hi'
}
}

def friend = new Friend(name: 'Pat')
println friend.greet()
14 changes: 14 additions & 0 deletions groovy/src/main/groovy/ElvisAssignment.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import groovy.transform.ToString

@ToString
class Element {
String name
int atomicNumber
}

def he = new Element(name: 'Helium')
he.with {
name = name ?: 'Hydrogen' // existing Elvis operator
atomicNumber ?= 2 // new Elvis assignment shorthand
}
assert he.toString() == 'Element(Helium, 2)'
15 changes: 15 additions & 0 deletions groovy/src/main/groovy/IdentityEquality.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


class Creature { String type }

def houseCat = new Creature(type: 'cat')
def copyHouseCat = houseCat
def lion = new Creature(type: 'cat')

assert !houseCat.equals(lion) // Java logical equality
assert houseCat != lion // Groovy shorthand operator

assert houseCat.is(copyHouseCat) // Groovy identity
assert houseCat === copyHouseCat // operator shorthand
assert houseCat !== lion // negated operator shorthand

38 changes: 38 additions & 0 deletions groovy/src/main/groovy/Macro.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.expr.ClassExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.macro.transform.MacroClass

def classNameMacro = macro(true) { getClass().name }


def date = new ConstantExpression(new Date().toString())
def version = new ConstantExpression(GroovySystem.version)
def body = macro(true) {
"""\
Name: ${getClass().name}
Compiled: ${$v { date }}
Using Groovy version: ${$v { version }}""".stripIndent()
}



ClassNode buildInfoClass(ClassNode reference) {
def date = new ConstantExpression(new Date().toString())
def vers = new ConstantExpression(GroovySystem.version)
def name = new ClassExpression(reference)
ClassNode infoClass = new MacroClass() {
class DummyName {
java.lang.String getName() { $v{ name }.name }
java.lang.String getVersion() { $v{ vers } }
java.lang.String getCompiled() { $v{ date } }
}
}
infoClass.name = reference.name + 'Info'
return infoClass
}

//@Info
//class Foo {
//
//}
38 changes: 38 additions & 0 deletions groovy/src/test/groovy/Groovy3Test.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.stream.Stream

import static java.util.stream.Collectors.toList

class Groovy3Test extends GroovyTestCase {
void testJavaDoWhile() {
def count = 5
do {
println "count = $count"
} while (count-- > 0)
}

void testMethodReferences() {
// class::staticMethod
assert ['1', '2', '3'] == Stream.of(1, 2, 3)
.map(String::valueOf)
.collect(toList())

// class::instanceMethod
assert [1, 2, 3] == ['a', 'bb', 'ccc'].stream()
.map(String::length)
.collect(toList())
}

void testNotInAndNotInstanceOf() {
assert 45 !instanceof Date

assert 4 !in [1, 3, 5, 7]
}

void testSafeIndexing() {
String[] array = null
assert null == array ?[1] // return null for all index values
array ?[1] = 'c' // quietly ignore attempt to set value
assert array == null
}

}
9 changes: 9 additions & 0 deletions groovy/src/test/groovy/MacroTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class MacroTest {


def body = macro(true) { getClass().name }



}

17 changes: 17 additions & 0 deletions groovy/src/test/groovy/NamedParamsTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import groovy.transform.NamedParam
import groovy.transform.NamedVariant
import org.junit.jupiter.api.Test

class NamedParamsTest {

@NamedVariant
String fullName(String lastName, @NamedParam String firstName) {
"$firstName $lastName"
}

@Test
void testNamedParams() {
println fullName('Smith', firstName: 'Ash')
}
}

0 comments on commit 25e6aad

Please sign in to comment.