//Java
import java.util.Date;
public class Employee {
private String firstName;
private String lastName;
private Date dateSince;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateSince() {
return dateSince;
}
public void setDateSince(Date dateSince) {
this.dateSince = dateSince;
}
}
//Groovy
class Employee {
String firstName
String lastName
Date dateSince
}
def e = new Employee(firstName:'Ivan', lastName:'Petrov')
@Immutable
import groovy.lang.Immutable;
@Immutable class Employee {
String firstName
String lastName
Date dateSince
}
import java.text.SimpleDateFormat
class Event {
@Delegate Date when
String title, url
}
def df = new SimpleDateFormat("yyyy/MM/dd")
def gr8conf = new Event(title: "GR8 Conference",
url: "http://www.gr8conf.org",
when: df.parse("2009/05/18"))
def javaOne = new Event(title: "JavaOne",
url: "http://java.sun.com/javaone/",
when: df.parse("2009/06/02"))
assert gr8conf.before(javaOne.when)
gr8conf.before(javaOne.when)
seems quite logical, while maintaining the purity of the code.
List getStaffWithMuchSalaryThan(List staff, double salary);
List getStaffYoungerThan(List staff, int age);
List getStaffByDepartment(List staff, Dept dept);
//
def staff = [
new Employee(firstName: 'A', salary:500, age:20, dept:'K'),
new Employee(firstName: 'B', salary:700, age:30, dept:'K'),
new Employee(firstName: 'C', salary:1000, age:25, dept:'A2') ]
//
def getStaffWithCriteria(staff, criteria) {
result = []
for(e in staff) {
if(criteria(e)) result.add(e)
}
result
}
println getStaffWithCriteria(staff, {e -> e.salary > 600}) // 600
println getStaffWithCriteria(staff, {e -> e.age < 27}) //, 27
println getStaffWithCriteria(staff, {e -> e.dept == 'K'}) // K
def list = ['a', 'b', 'c']
for(String s: list) {
System.out.println(s);
}
list.each {println it}
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " - " + list.get(i));
}
list.eachWithIndex {e, i -> println “${i} - ${e}”}
list.join(', ')
Source: https://habr.com/ru/post/110368/