Skip to content

Commit 49165da

Browse files
committed
Initial import of java8 examples
0 parents  commit 49165da

18 files changed

Lines changed: 570 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*.iml
3+
target

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.codecentric</groupId>
8+
<artifactId>java8-examples</artifactId>
9+
<version>0.1-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.11</version>
16+
<scope>test</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.hamcrest</groupId>
20+
<artifactId>hamcrest-library</artifactId>
21+
<version>1.3</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package de.codecentric.java8examples;
2+
3+
import java.util.Calendar;
4+
import java.util.Date;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: bene
9+
* Date: 04.10.13
10+
* Time: 13:41
11+
* To change this template use File | Settings | File Templates.
12+
*/
13+
public class Person {
14+
15+
public enum Gender {
16+
MALE, FEMALE
17+
}
18+
19+
private final String name;
20+
private final String lastName;
21+
private final Date birthDay;
22+
private Gender gender;
23+
24+
public Person(String name, String lastName, Date birthDay, Gender gender) {
25+
this.name = name;
26+
this.lastName = lastName;
27+
this.birthDay = birthDay;
28+
this.gender = gender;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public String getLastName() {
36+
return lastName;
37+
}
38+
39+
public Date getBirthDay() {
40+
return birthDay;
41+
}
42+
43+
public Gender getGender() {
44+
return gender;
45+
}
46+
47+
public void setGender(Gender gender) {
48+
this.gender = gender;
49+
}
50+
51+
public int getAge() {
52+
Calendar now = Calendar.getInstance();
53+
Calendar birth = Calendar.getInstance();
54+
birth.setTime(getBirthDay());
55+
56+
int years = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
57+
if(now.get(Calendar.MONTH) > birth.get(Calendar.MONTH) ||
58+
now.get(Calendar.MONTH) == birth.get(Calendar.MONTH) && birth.get(Calendar.DATE) > now.get(Calendar.DATE)) {
59+
years--;
60+
}
61+
62+
return years;
63+
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
70+
Person person = (Person) o;
71+
72+
if (birthDay != null ? !birthDay.equals(person.birthDay) : person.birthDay != null) return false;
73+
if (gender != person.gender) return false;
74+
if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) return false;
75+
if (name != null ? !name.equals(person.name) : person.name != null) return false;
76+
77+
return true;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
int result = name != null ? name.hashCode() : 0;
83+
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
84+
result = 31 * result + (birthDay != null ? birthDay.hashCode() : 0);
85+
result = 31 * result + (gender != null ? gender.hashCode() : 0);
86+
return result;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return "Person{" +
92+
"name='" + name + '\'' +
93+
", lastName='" + lastName + '\'' +
94+
", birthDay=" + birthDay +
95+
", gender=" + gender +
96+
'}';
97+
}
98+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 14:59
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
abstract class AbstractGreetingService implements GreetingService {
11+
12+
@Override
13+
public abstract String greet();
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 15:55
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public interface AlternativeGreetingService {
11+
12+
default String greet() {
13+
return "Alternative Greeting!";
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 14:51
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class DefaultGreetingService implements GreetingService {
11+
// nothing to implement here...
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 15:01
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class DerivedGreetingService extends AbstractGreetingService {
11+
12+
@Override
13+
public String greet() {
14+
return "Salut le monde!";
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 14:57
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class ExtendedGreetingService implements GreetingService {
11+
12+
private String name;
13+
14+
public ExtendedGreetingService(String name) {
15+
this.name = name;
16+
}
17+
18+
@Override
19+
public String greet() {
20+
return "Hello " + name + "!";
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: bene
6+
* Date: 04.10.13
7+
* Time: 14:51
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public interface GreetingService {
11+
12+
default String greet() {
13+
return "Hello World!";
14+
}
15+
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package de.codecentric.java8examples.lambdas;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: bene
8+
* Date: 04.10.13
9+
* Time: 15:57
10+
* To change this template use File | Settings | File Templates.
11+
*/
12+
public class EventLambdas extends JFrame {
13+
14+
private int clicks = 0;
15+
16+
public EventLambdas() {
17+
JButton btn = new JButton("0");
18+
btn.setSize(50, 50);
19+
add(btn);
20+
21+
// lambda magic goes here:
22+
btn.addActionListener(e -> {
23+
btn.setText(Integer.toString(clicks++));
24+
});
25+
26+
setSize(100, 100);
27+
pack();
28+
setVisible(true);
29+
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
30+
}
31+
32+
public static void main(String[] args) {
33+
new EventLambdas();
34+
}
35+
}

0 commit comments

Comments
 (0)