Skip to content

Commit 06e6d7e

Browse files
Java Records
1 parent c4f87a9 commit 06e6d7e

File tree

8 files changed

+196
-66
lines changed

8 files changed

+196
-66
lines changed

pom.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
<artifactId>assertj-core</artifactId>
155155
<version>3.16.1</version>
156156
</dependency>
157+
<dependency>
158+
<groupId>io.soabase.record-builder</groupId>
159+
<artifactId>record-builder-core</artifactId>
160+
<version>34</version>
161+
</dependency>
157162
</dependencies>
158163
<build>
159164
<sourceDirectory>src/main/java</sourceDirectory>
@@ -179,10 +184,15 @@
179184
<artifactId>lombok</artifactId>
180185
<version>1.18.22</version>
181186
</path>
187+
<path>
188+
<groupId>io.soabase.record-builder</groupId>
189+
<artifactId>record-builder-processor</artifactId>
190+
<version>34</version>
191+
</path>
182192
</annotationProcessorPaths>
183193
</configuration>
184194
</plugin>
185-
<plugin>
195+
<!--<plugin>
186196
<groupId>org.apache.maven.plugins</groupId>
187197
<artifactId>maven-shade-plugin</artifactId>
188198
<version>3.2.0</version>
@@ -203,16 +213,15 @@
203213
</configuration>
204214
</execution>
205215
</executions>
206-
</plugin>
216+
</plugin>-->
207217
<plugin>
208218
<groupId>org.apache.maven.plugins</groupId>
209219
<artifactId>maven-jar-plugin</artifactId>
210220
<version>3.2.0</version>
211221
<configuration>
212222
<archive>
213223
<manifest>
214-
<mainClass>com.howtodoinjava.io.
215-
ReadFileFromResourcesUsingGetResourceAsStream
224+
<mainClass>com.howtodoinjava.io.ReadFileFromResourcesUsingGetResourceAsStream
216225
</mainClass>
217226
</manifest>
218227
</archive>

src/main/java/com/howtodoinjava/core/basic/EmployeeRecord.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.howtodoinjava.core.basic;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
8+
public class ForEachExamples {
9+
10+
public static void main(String[] args) {
11+
List<String> list = Arrays.asList("A","B","C","D");
12+
ArrayList<String> arrayList = new ArrayList<>(list);
13+
14+
//1
15+
list.forEach(System.out::println);
16+
17+
//2
18+
Consumer<String> action = x -> System.out.println(x.toLowerCase());
19+
list.forEach(action);
20+
21+
list.forEach(e -> System.out.println(e.toLowerCase()));
22+
23+
//3
24+
list.forEach(e -> {
25+
System.out.println(e.toLowerCase());
26+
//other statements
27+
});
28+
}
29+
30+
}

src/main/java/com/howtodoinjava/core/basic/RecordExample.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.howtodoinjava.core.basic.record;
2+
3+
import io.soabase.recordbuilder.core.RecordBuilder;
4+
5+
public class BuilderExample {
6+
7+
public static void main(String[] args) {
8+
9+
User user = UserBuilder.builder()
10+
.id(1l)
11+
.name("Lokesh")
12+
13+
.status(true)
14+
.build();
15+
16+
System.out.println(user);
17+
18+
User inactiveUser = UserBuilder.builder(user)
19+
20+
.status(true)
21+
.build();
22+
23+
System.out.println(inactiveUser);
24+
}
25+
}
26+
27+
28+
//@lombok.Builder
29+
@RecordBuilder
30+
record User(long id, String name, String email, boolean status) {
31+
32+
/* public static final class Builder {
33+
34+
long id;
35+
String name;
36+
String email;
37+
boolean status;
38+
39+
public Builder(long id, String name) {
40+
this.id = id;
41+
this.name = name;
42+
}
43+
44+
public Builder email(String email) {
45+
this.email = email;
46+
return this;
47+
}
48+
49+
public Builder status(boolean status) {
50+
this.status = status;
51+
return this;
52+
}
53+
54+
public User build() {
55+
return new User(id, name, email, status);
56+
}
57+
}*/
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.howtodoinjava.core.basic.record;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
public record Employee(Long id, String firstName, String lastName, String email, int age)
7+
implements Serializable {
8+
9+
static boolean minor;
10+
11+
public boolean isMinor() {
12+
return minor;
13+
}
14+
15+
public String fullName() {
16+
return firstName + " " + lastName;
17+
}
18+
19+
/*public Employee {
20+
if (age < 18) {
21+
//minor = true;
22+
throw new IllegalArgumentException("You cannot hire a minor person as employee");
23+
}
24+
}
25+
*/
26+
public Employee {
27+
Objects.requireNonNull(id);
28+
Objects.requireNonNull(email);
29+
30+
if (age < 18) {
31+
throw new IllegalArgumentException("You cannot hire a minor person as employee");
32+
}
33+
}
34+
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.howtodoinjava.core.basic.record;
2+
3+
public class GenericRecordExample {
4+
5+
public static void main(String[] args) {
6+
Container<Integer> intContainer = new Container<>(1, Integer.valueOf(1));
7+
Container<String> stringContainer = new Container<>(1, "1");
8+
9+
Integer intValue = intContainer.value();
10+
String strValue = stringContainer.value();
11+
12+
13+
}
14+
}
15+
16+
record Container<T>(int id, T value) {
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.howtodoinjava.core.basic.record;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
9+
public class RecordExample {
10+
11+
public static void main(String[] args) {
12+
Employee e = new Employee(1l, "Lokesh", "Gupta", "[email protected]", 38);
13+
14+
System.out.println(e.id());
15+
System.out.println(e.email());
16+
System.out.println(e);
17+
18+
/*writeToFile(e, "employee1");
19+
System.out.println(readFromFile("employee1"));*/
20+
21+
Employee employee = new Employee(1l, "Amit", "Gupta", "[email protected]", 17);
22+
System.out.println(employee.isMinor());
23+
System.out.println(employee.fullName());
24+
}
25+
26+
static void writeToFile(Employee obj, String path) {
27+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
28+
oos.writeObject(obj);
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
static Employee readFromFile(String path) {
35+
Employee result = null;
36+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
37+
result = (Employee) ois.readObject();
38+
} catch (ClassNotFoundException | IOException e) {
39+
e.printStackTrace();
40+
}
41+
return result;
42+
}
43+
}

0 commit comments

Comments
 (0)