-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecords.java
More file actions
169 lines (149 loc) · 5.3 KB
/
Records.java
File metadata and controls
169 lines (149 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.example.records;
import java.util.*;
import java.util.stream.Collectors;
import java.io.Serializable;
// import org.jetbrains.annotations.NotNull;
public class Records {
public static void testSimpleRecord() {
record Person(String name, int age) {}
Person p = new Person("Alice", 30);
System.out.println(p.name() + ", " + p.age());
}
public static void testCustomConstructor() {
record Email(String value) {
public Email {
if (!value.contains("@"))
throw new IllegalArgumentException("Invalid email: " + value);
}
}
Email e = new Email("user@example.com");
System.out.println(e);
}
public static void testAdditionalMethods() {
record Point(int x, int y) {
public int manhattanDistance() { return Math.abs(x) + Math.abs(y); }
}
Point p = new Point(5, -3);
System.out.println(p.manhattanDistance());
}
public static void testCustomToString() {
record User(String username) {
@Override public String toString() { return "User: " + username; }
}
User u = new User("anuj");
System.out.println(u);
}
public static void testMethodUsage() {
record Config(String env, int retries) {}
class App {
Config getConfig() { return new Config("prod", 3); }
void printConfig(Config cfg) { System.out.println(cfg); }
}
App app = new App();
Config c = app.getConfig();
app.printConfig(c);
}
// public static void testAnnotations() {
// record Product(@NotNull String id, double price) {}
// Product p = new Product("A123", 99.99);
// System.out.println(p);
// }
public static void testNestedRecords() {
record Department(String name) {}
record Employee(String name, Department dept) {}
Department d = new Department("HR");
Employee e = new Employee("Bob", d);
System.out.println(e);
}
public static void testVarUsage() {
record Item(String name, int price) {}
List<Item> items = List.of(new Item("Pen", 10), new Item("Book", 100));
for (var item : items) {
System.out.println(item.name() + " - " + item.price());
}
}
public static void testInnerClass() {
record Coordinates(int x, int y) {}
class Map {
class Marker {
Coordinates location;
Marker(int x, int y) { this.location = new Coordinates(x, y); }
}
}
Map.Marker m = new Map().new Marker(1, 2);
System.out.println(m.location);
}
public static void testLambdas() {
record Task(String name, int priority) {}
List<Task> tasks = List.of(new Task("Clean", 4), new Task("Deploy", 8));
List<String> result = tasks.stream()
.filter(t -> t.priority() > 5)
.map(Task::name)
.collect(Collectors.toList());
System.out.println(result);
}
public static void testGenerics() {
record Wrapper<T>(T value) { public T get() { return value; } }
Wrapper<Integer> w = new Wrapper<>(42);
System.out.println(w.get());
}
public static void testInterfaces() {
record UserRecord(String name) implements Comparable<UserRecord>, Serializable {
@Override public int compareTo(UserRecord other) { return name.compareTo(other.name); }
}
UserRecord u1 = new UserRecord("Alice");
UserRecord u2 = new UserRecord("Bob");
System.out.println(u1.compareTo(u2));
}
public static void testOverload() {
record User(String id) {}
class Logger {
void log(User u) { System.out.println("User: " + u); }
void log(String s) { System.out.println("String: " + s); }
}
Logger l = new Logger();
l.log(new User("U1"));
l.log("message");
}
public static void testCollections() {
record Key(String name, int version) {}
Map<Key, String> map = Map.of(new Key("alpha", 1), "value");
System.out.println(map);
}
public static void testTryWithResources() {
record Resource(String name) implements AutoCloseable {
public void close() { System.out.println("Closed " + name); }
}
try (Resource r = new Resource("r1")) {
System.out.println("Using " + r.name());
}
}
public static void testControlFlow() {
record Person(String name, int age) {}
Person p1 = new Person("Alice", 30);
Person p2 = new Person("Alice", 30);
if (p1.equals(p2)) {
System.out.println("Same person");
}
System.out.println(p1.hashCode());
System.out.println(p1.toString());
}
public static void main(String[] args) {
testSimpleRecord();
testCustomConstructor();
testAdditionalMethods();
testCustomToString();
testMethodUsage();
// testAnnotations();
testNestedRecords();
testVarUsage();
testInnerClass();
testLambdas();
testGenerics();
testInterfaces();
testOverload();
testCollections();
testTryWithResources();
testControlFlow();
}
}