Skip to content

Commit 3ae6b07

Browse files
authored
refactoring: Issue iluwatar#2377: The repository code has been refactored to use the recor… (iluwatar#2505)
* Issue iluwatar#2377: The repository code has been refactored to use the record class * Issue iluwatar#2377: Refactored according to the rules defined for the repo code * Issue iluwatar#2377: Refactored according to the rules defined for the repo code * Issue iluwatar#2377: Refactored according to the rules defined for the repo code
1 parent c0e1603 commit 3ae6b07

File tree

26 files changed

+244
-293
lines changed

26 files changed

+244
-293
lines changed

builder/src/main/java/com/iluwatar/builder/Hero.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,13 @@
2525
package com.iluwatar.builder;
2626

2727
/**
28-
* Hero, the class with many parameters.
28+
* Hero,the record class.
2929
*/
30-
public final class Hero {
3130

32-
private final Profession profession;
33-
private final String name;
34-
private final HairType hairType;
35-
private final HairColor hairColor;
36-
private final Armor armor;
37-
private final Weapon weapon;
31+
public record Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
3832

3933
private Hero(Builder builder) {
40-
this.profession = builder.profession;
41-
this.name = builder.name;
42-
this.hairColor = builder.hairColor;
43-
this.hairType = builder.hairType;
44-
this.weapon = builder.weapon;
45-
this.armor = builder.armor;
46-
}
47-
48-
public Profession getProfession() {
49-
return profession;
50-
}
51-
52-
public String getName() {
53-
return name;
54-
}
55-
56-
public HairType getHairType() {
57-
return hairType;
58-
}
59-
60-
public HairColor getHairColor() {
61-
return hairColor;
62-
}
63-
64-
public Armor getArmor() {
65-
return armor;
66-
}
67-
68-
public Weapon getWeapon() {
69-
return weapon;
34+
this(builder.profession, builder.name, builder.hairType, builder.hairColor, builder.armor, builder.weapon);
7035
}
7136

7237
@Override

builder/src/test/java/com/iluwatar/builder/HeroTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ void testBuildHero() {
6969

7070
assertNotNull(hero);
7171
assertNotNull(hero.toString());
72-
assertEquals(Profession.WARRIOR, hero.getProfession());
73-
assertEquals(heroName, hero.getName());
74-
assertEquals(Armor.CHAIN_MAIL, hero.getArmor());
75-
assertEquals(Weapon.SWORD, hero.getWeapon());
76-
assertEquals(HairType.LONG_CURLY, hero.getHairType());
77-
assertEquals(HairColor.BLOND, hero.getHairColor());
72+
assertEquals(Profession.WARRIOR, hero.profession());
73+
assertEquals(heroName, hero.name());
74+
assertEquals(Armor.CHAIN_MAIL, hero.armor());
75+
assertEquals(Weapon.SWORD, hero.weapon());
76+
assertEquals(HairType.LONG_CURLY, hero.hairType());
77+
assertEquals(HairColor.BLOND, hero.hairColor());
7878

7979
}
8080

intercepting-filter/src/test/java/com/iluwatar/intercepting/filter/TargetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license. Module intercepting-filter is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
33
*
44
* The MIT License
55
* Copyright © 2014-2022 Ilkka Seppälä

model-view-presenter/src/test/java/com/iluwatar/model/view/presenter/FileSelectorJframeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license. Module model-view-presenter is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
33
*
44
* The MIT License
55
* Copyright © 2014-2022 Ilkka Seppälä

monad/src/main/java/com/iluwatar/monad/App.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class App {
5454
*/
5555
public static void main(String[] args) {
5656
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
57-
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
58-
.validate(User::getName, name -> !name.isEmpty(), "name is empty")
59-
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
60-
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
57+
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
58+
.validate(User::name, name -> !name.isEmpty(), "name is empty")
59+
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
60+
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...").get()
6161
.toString());
6262
}
6363
}

monad/src/main/java/com/iluwatar/monad/User.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,13 @@
2525
package com.iluwatar.monad;
2626

2727
/**
28-
* User Definition.
28+
* Record class.
29+
*
30+
* @param name - name
31+
* @param age - age
32+
* @param sex - sex
33+
* @param email - email address
2934
*/
30-
public class User {
31-
32-
private final String name;
33-
private final int age;
34-
private final Sex sex;
35-
private final String email;
36-
37-
/**
38-
* Constructor.
39-
*
40-
* @param name - name
41-
* @param age - age
42-
* @param sex - sex
43-
* @param email - email address
44-
*/
45-
public User(String name, int age, Sex sex, String email) {
46-
this.name = name;
47-
this.age = age;
48-
this.sex = sex;
49-
this.email = email;
50-
}
51-
52-
public String getName() {
53-
return name;
54-
}
55-
56-
public int getAge() {
57-
return age;
58-
}
59-
60-
public Sex getSex() {
61-
return sex;
62-
}
63-
64-
public String getEmail() {
65-
return email;
66-
}
35+
public record User(String name, int age, Sex sex, String email) {
6736
}
37+

monad/src/test/java/com/iluwatar/monad/MonadTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void testForInvalidName() {
4141
assertThrows(
4242
IllegalStateException.class,
4343
() -> Validator.of(tom)
44-
.validate(User::getName, Objects::nonNull, "name cannot be null")
44+
.validate(User::name, Objects::nonNull, "name cannot be null")
4545
.get()
4646
);
4747
}
@@ -52,8 +52,8 @@ void testForInvalidAge() {
5252
assertThrows(
5353
IllegalStateException.class,
5454
() -> Validator.of(john)
55-
.validate(User::getName, Objects::nonNull, "name cannot be null")
56-
.validate(User::getAge, age -> age > 21, "user is underage")
55+
.validate(User::name, Objects::nonNull, "name cannot be null")
56+
.validate(User::age, age -> age > 21, "user is underage")
5757
.get()
5858
);
5959
}
@@ -62,10 +62,10 @@ void testForInvalidAge() {
6262
void testForValid() {
6363
var sarah = new User("Sarah", 42, Sex.FEMALE, "[email protected]");
6464
var validated = Validator.of(sarah)
65-
.validate(User::getName, Objects::nonNull, "name cannot be null")
66-
.validate(User::getAge, age -> age > 21, "user is underage")
67-
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
68-
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
65+
.validate(User::name, Objects::nonNull, "name cannot be null")
66+
.validate(User::age, age -> age > 21, "user is underage")
67+
.validate(User::sex, sex -> sex == Sex.FEMALE, "user is not female")
68+
.validate(User::email, email -> email.contains("@"), "email does not contain @ sign")
6969
.get();
7070
assertSame(validated, sarah);
7171
}

null-object/src/main/java/com/iluwatar/nullobject/NodeImpl.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,9 @@
2929
/**
3030
* Implementation for binary tree's normal nodes.
3131
*/
32-
@Slf4j
33-
public class NodeImpl implements Node {
34-
35-
private final String name;
36-
private final Node left;
37-
private final Node right;
38-
39-
/**
40-
* Constructor.
41-
*/
42-
public NodeImpl(String name, Node left, Node right) {
43-
this.name = name;
44-
this.left = left;
45-
this.right = right;
46-
}
47-
48-
@Override
49-
public int getTreeSize() {
50-
return 1 + left.getTreeSize() + right.getTreeSize();
51-
}
5232

33+
@Slf4j
34+
public record NodeImpl(String name, Node left, Node right) implements Node {
5335
@Override
5436
public Node getLeft() {
5537
return left;
@@ -64,7 +46,10 @@ public Node getRight() {
6446
public String getName() {
6547
return name;
6648
}
67-
49+
@Override
50+
public int getTreeSize() {
51+
return 1 + left.getTreeSize() + right.getTreeSize();
52+
}
6853
@Override
6954
public void walk() {
7055
LOGGER.info(name);

partial-response/src/main/java/com/iluwatar/partialresponse/Video.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,10 @@
2626

2727
/**
2828
* {@link Video} is a entity to serve from server.It contains all video related information.
29+
* Video is a record class.
2930
*/
30-
public class Video {
31-
private final Integer id;
32-
private final String title;
33-
private final Integer length;
34-
private final String description;
35-
private final String director;
36-
private final String language;
37-
38-
/**
39-
* Constructor.
40-
*
41-
* @param id video unique id
42-
* @param title video title
43-
* @param len video length in minutes
44-
* @param desc video description by publisher
45-
* @param director video director name
46-
* @param lang video language {private, public}
47-
*/
48-
public Video(Integer id, String title, Integer len, String desc, String director, String lang) {
49-
this.id = id;
50-
this.title = title;
51-
this.length = len;
52-
this.description = desc;
53-
this.director = director;
54-
this.language = lang;
55-
}
5631

32+
public record Video(Integer id, String title, Integer length, String description, String director, String language) {
5733
/**
5834
* ToString.
5935
*
@@ -62,12 +38,12 @@ public Video(Integer id, String title, Integer len, String desc, String director
6238
@Override
6339
public String toString() {
6440
return "{"
65-
+ "\"id\": " + id + ","
66-
+ "\"title\": \"" + title + "\","
67-
+ "\"length\": " + length + ","
68-
+ "\"description\": \"" + description + "\","
69-
+ "\"director\": \"" + director + "\","
70-
+ "\"language\": \"" + language + "\","
71-
+ "}";
41+
+ "\"id\": " + id + ","
42+
+ "\"title\": \"" + title + "\","
43+
+ "\"length\": " + length + ","
44+
+ "\"description\": \"" + description + "\","
45+
+ "\"director\": \"" + director + "\","
46+
+ "\"language\": \"" + language + "\","
47+
+ "}";
7248
}
7349
}

partial-response/src/main/java/com/iluwatar/partialresponse/VideoResource.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,14 @@
2727
import java.util.Map;
2828

2929
/**
30-
* The resource class which serves video information. This class act as server in the demo. Which
30+
* The resource record class which serves video information. This class act as server in the demo. Which
3131
* has all video details.
32+
*
33+
* @param fieldJsonMapper map object to json.
34+
* @param videos initialize resource with existing videos. Act as database.
3235
*/
33-
public class VideoResource {
34-
private final FieldJsonMapper fieldJsonMapper;
35-
private final Map<Integer, Video> videos;
36-
37-
/**
38-
* Constructor.
39-
*
40-
* @param fieldJsonMapper map object to json.
41-
* @param videos initialize resource with existing videos. Act as database.
42-
*/
43-
public VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
44-
this.fieldJsonMapper = fieldJsonMapper;
45-
this.videos = videos;
46-
}
4736

37+
public record VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
4838
/**
4939
* Get Details.
5040
*

0 commit comments

Comments
 (0)