Skip to content

Commit f50ff6d

Browse files
committed
Lombok annotation example
1 parent 0242cad commit f50ff6d

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.rajeshkawali.lombok;
2+
3+
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Singular;
6+
import lombok.ToString;
7+
import lombok.experimental.SuperBuilder;
8+
9+
import java.util.List;
10+
11+
@Data
12+
@SuperBuilder
13+
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
14+
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
15+
public class Actor extends ActorSuper {
16+
17+
@ToString.Exclude
18+
private String email;
19+
20+
@ToString.Include
21+
@EqualsAndHashCode.Include
22+
private String surname;
23+
}
24+
25+
@Data
26+
@SuperBuilder
27+
class ActorSuper {
28+
private String firstName;
29+
private String gender;
30+
31+
@Singular
32+
private List<String> tags;
33+
}
34+
/*
35+
The @Singular annotation is used together with Lombok @Builder annotation.
36+
If you annotate one of the collection valued field with @Singular annotation Lombok generates for the field :
37+
38+
1.One ‘adder’ method for adding single element to collection.
39+
2.Another ‘adder’ method for adding another collection to the collection, this name is plural for the field name .
40+
3.Clear method to clear collection elements.
41+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rajeshkawali.lombok;
2+
3+
import lombok.*;
4+
5+
@ToString(includeFieldNames = false) // includeFieldNames = false --> field name will not add while printing the object
6+
@EqualsAndHashCode
7+
@Getter
8+
@Setter
9+
//@RequiredArgsConstructor
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
@Builder
13+
public class Address {
14+
15+
public String city;
16+
17+
@EqualsAndHashCode.Exclude // We can exclude field from equal and hashcode methods.
18+
public String state;
19+
20+
@Builder.Default // We can provide default value to the field
21+
public String country = "India";
22+
23+
@ToString.Exclude
24+
public Long pin;
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.rajeshkawali.lombok;
2+
3+
4+
import lombok.Value;
5+
/*
6+
@Value annotation -->is shorthand for the annotation combination of
7+
@Getter
8+
@FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE)
9+
@AllArgsConstructor
10+
@ToString
11+
@EqualsAndHashCode .
12+
*/
13+
@Value
14+
//@Value(staticConstructor = "getInstance") //Constructor will be private, a static factory method is created to that other classes can use to create instances.
15+
public class ImmutableClass {
16+
private Long id;
17+
private String username;
18+
}
19+
20+
/*
21+
@Data @Value
22+
1.Lombok generated class is not final. 1.Lombok generated class is final.
23+
2.Generates required-args constructor. 2.Generates all-args constructor.
24+
3.Generates equals(), hashCode() and toString(). 3.Generates equals(), hashCode() and toString().
25+
4.Does not make fields final. 4.Makes all non-static fields final.
26+
27+
5.Generates setters and getters.
28+
No getters/setters are generated for
29+
static fields and no setters
30+
generate for final fields. 5.No setters generate for any filed as it makes all
31+
fields final except static fields, and obviously no getters
32+
generate for static fields.
33+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.rajeshkawali.lombok;
2+
3+
import lombok.Cleanup;
4+
import lombok.SneakyThrows;
5+
import lombok.Synchronized;
6+
7+
import java.io.*;
8+
9+
public class LombokCleanup {
10+
public static void main(String[] args) throws IOException {
11+
12+
@Cleanup // This will close all the connections/opened files before exit
13+
InputStream in = new FileInputStream(args[0]);
14+
15+
@Cleanup
16+
OutputStream out = new FileOutputStream(args[1]);
17+
byte[] b = new byte[10000];
18+
while (true) {
19+
int r = in.read(b);
20+
if (r == -1) break;
21+
out.write(b, 0, r);
22+
}
23+
24+
sneakyThrowsExample();
25+
}
26+
27+
//If we remove this annotation then we forced to handle it or use throws. (Unhandled exception: java.io.FileNotFoundException)
28+
//You can pass any number of exceptions to the @SneakyThrows annotation. If you pass no exceptions, you may throw any exception sneakily.
29+
@SneakyThrows // @SneakyThrows - can be used to sneakily throw checked exceptions without actually declaring this in your method's throws clause.
30+
public static String sneakyThrowsExample(){
31+
if(true){
32+
throw new FileNotFoundException("");
33+
}else {
34+
return "";
35+
}
36+
}
37+
38+
/*
39+
@Synchronized is a safer variant of the synchronized method modifier.
40+
Like synchronized, the annotation can be used on static and instance methods only.
41+
It operates similarly to the synchronized keyword, but it locks on different objects.
42+
The keyword locks on this, but the annotation locks on a field named $lock, which is private.
43+
*/
44+
@Synchronized
45+
public void foo() {
46+
System.out.println("bar");
47+
}
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.rajeshkawali.lombok;
2+
3+
4+
import lombok.extern.slf4j.Slf4j;
5+
import lombok.val;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* @author Rajesh_Kawali
12+
*
13+
* @see "https://projectlombok.org/features/"
14+
*/
15+
@Slf4j //@Slf4j - Logger to print on console
16+
public class LombokMain {
17+
public static void main(String[] args) {
18+
19+
val phone = List.of("9988776655","9944556677"); //Lombok also contain var -> var works exactly like val, except the local variable is not marked as final.
20+
//@Builder - Builder annotation uses the Builder pattern to construct the object
21+
User user = User.builder()
22+
.id(1L)
23+
.age(24)
24+
//.role("Sr.Software Engineer")
25+
.gender("Male")
26+
.firstName("Keshav")
27+
.lastName("Koli")
28+
.phone(phone)
29+
.address(Address.builder().city("Mumbai").state("Maharashtra").country("India").pin(400091L).build())
30+
.build();
31+
32+
log.info("User Details are: {}", user);
33+
log.info("-----------------------------------");
34+
Actor actor = Actor.builder().build();
35+
actor.setFirstName("Rajesh");
36+
actor.setSurname("Kawali");
37+
actor.setEmail("rajesh@gmail.com");
38+
actor.setGender("Male");
39+
actor.setTags(Arrays.asList("software developer","UI developer"));
40+
log.info("Actor Details are: {}", actor);
41+
log.info("-----------------------------------");
42+
Actor actor2 = Actor.builder().clearTags().build(); // clearTags() --> added by @Singular annotation
43+
log.info("Actor Details are: {}", actor);
44+
log.info("Actor two Details are: {}", actor2);
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.rajeshkawali.lombok;
2+
3+
import lombok.*;
4+
import lombok.experimental.NonFinal;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author Rajesh_Kawali
10+
*/
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
@Data // @Data annotation is shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor
14+
@Builder //Builder annotation uses the Builder pattern to construct the object
15+
//@Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld", toBuilder = true, access = AccessLevel.PRIVATE, setterPrefix = "set")
16+
public class User {
17+
18+
private Long id;
19+
20+
@NonNull //null-check for field,If null throw NullPointerException("param is marked non-null but is null").
21+
private String firstName;
22+
23+
private String lastName;
24+
25+
@Setter(AccessLevel.PACKAGE)
26+
private Integer age;
27+
28+
@Setter(AccessLevel.PUBLIC) // We can provide access level to the field
29+
private String gender;
30+
31+
@NonFinal // We can make final field/final class as non-final using @NonFinal.
32+
@Getter(lazy = true)// First time this getter is called, and cache it- It is useful when there is complex task perform once and cache it.
33+
private final String role = "Software Engineer";
34+
35+
private List<String> phone;
36+
37+
private Address address;
38+
}

0 commit comments

Comments
 (0)