Skip to content

Commit

Permalink
Issue iluwatar#2377: The repository code has been refactored to use t…
Browse files Browse the repository at this point in the history
…he record class
  • Loading branch information
MugheesQasim committed Apr 10, 2023
1 parent 4bea173 commit c32a019
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 297 deletions.
42 changes: 4 additions & 38 deletions builder/src/main/java/com/iluwatar/builder/Hero.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,14 @@
package com.iluwatar.builder;

/**
* Hero, the class with many parameters.
* Hero, the record class
*/
public final class Hero {

private final Profession profession;
private final String name;
private final HairType hairType;
private final HairColor hairColor;
private final Armor armor;
private final Weapon weapon;
public record Hero(Profession profession,String name,HairType hairType,HairColor hairColor,Armor armor,Weapon weapon)
{

private Hero(Builder builder) {
this.profession = builder.profession;
this.name = builder.name;
this.hairColor = builder.hairColor;
this.hairType = builder.hairType;
this.weapon = builder.weapon;
this.armor = builder.armor;
}

public Profession getProfession() {
return profession;
}

public String getName() {
return name;
}

public HairType getHairType() {
return hairType;
}

public HairColor getHairColor() {
return hairColor;
}

public Armor getArmor() {
return armor;
}

public Weapon getWeapon() {
return weapon;
this(builder.profession,builder.name,builder.hairType,builder.hairColor,builder.armor,builder.weapon);
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions builder/src/test/java/com/iluwatar/builder/HeroTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ void testBuildHero() {

assertNotNull(hero);
assertNotNull(hero.toString());
assertEquals(Profession.WARRIOR, hero.getProfession());
assertEquals(heroName, hero.getName());
assertEquals(Armor.CHAIN_MAIL, hero.getArmor());
assertEquals(Weapon.SWORD, hero.getWeapon());
assertEquals(HairType.LONG_CURLY, hero.getHairType());
assertEquals(HairColor.BLOND, hero.getHairColor());
assertEquals(Profession.WARRIOR, hero.profession());
assertEquals(heroName, hero.name());
assertEquals(Armor.CHAIN_MAIL, hero.armor());
assertEquals(Weapon.SWORD, hero.weapon());
assertEquals(HairType.LONG_CURLY, hero.hairType());
assertEquals(HairColor.BLOND, hero.hairColor());

}

Expand Down
8 changes: 4 additions & 4 deletions monad/src/main/java/com/iluwatar/monad/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public class App {
*/
public static void main(String[] args) {
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
.validate(User::getName, name -> !name.isEmpty(), "name is empty")
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
.validate(User::name, name -> !name.isEmpty(), "name is empty")
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...").get()
.toString());
}
}
47 changes: 11 additions & 36 deletions monad/src/main/java/com/iluwatar/monad/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,16 @@
/**
* User Definition.
*/
public class User {

private final String name;
private final int age;
private final Sex sex;
private final String email;

/**
* Constructor.
*
* @param name - name
* @param age - age
* @param sex - sex
* @param email - email address
*/
public User(String name, int age, Sex sex, String email) {
this.name = name;
this.age = age;
this.sex = sex;
this.email = email;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public Sex getSex() {
return sex;
}

public String getEmail() {
return email;
}
/**
* Record class
*
* @param name - name
* @param age - age
* @param sex - sex
* @param email - email address
*/
public record User(String name, int age, Sex sex, String email)
{
}

14 changes: 7 additions & 7 deletions monad/src/test/java/com/iluwatar/monad/MonadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testForInvalidName() {
assertThrows(
IllegalStateException.class,
() -> Validator.of(tom)
.validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::name, Objects::nonNull, "name cannot be null")
.get()
);
}
Expand All @@ -52,8 +52,8 @@ void testForInvalidAge() {
assertThrows(
IllegalStateException.class,
() -> Validator.of(john)
.validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underage")
.validate(User::name, Objects::nonNull, "name cannot be null")
.validate(User::age, age -> age > 21, "user is underage")
.get()
);
}
Expand All @@ -62,10 +62,10 @@ void testForInvalidAge() {
void testForValid() {
var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
var validated = Validator.of(sarah)
.validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underage")
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
.validate(User::name, Objects::nonNull, "name cannot be null")
.validate(User::age, age -> age > 21, "user is underage")
.validate(User::sex, sex -> sex == Sex.FEMALE, "user is not female")
.validate(User::email, email -> email.contains("@"), "email does not contain @ sign")
.get();
assertSame(validated, sarah);
}
Expand Down
28 changes: 7 additions & 21 deletions null-object/src/main/java/com/iluwatar/nullobject/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,10 @@
/**
* Implementation for binary tree's normal nodes.
*/
@Slf4j
public class NodeImpl implements Node {

private final String name;
private final Node left;
private final Node right;

/**
* Constructor.
*/
public NodeImpl(String name, Node left, Node right) {
this.name = name;
this.left = left;
this.right = right;
}

@Override
public int getTreeSize() {
return 1 + left.getTreeSize() + right.getTreeSize();
}

@Slf4j
public record NodeImpl(String name,Node left,Node right) implements Node
{
@Override
public Node getLeft() {
return left;
Expand All @@ -64,7 +47,10 @@ public Node getRight() {
public String getName() {
return name;
}

@Override
public int getTreeSize() {
return 1 + left.getTreeSize() + right.getTreeSize();
}
@Override
public void walk() {
LOGGER.info(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,20 @@

/**
* {@link Video} is a entity to serve from server.It contains all video related information.
* Video is a record class
*/
public class Video {
private final Integer id;
private final String title;
private final Integer length;
private final String description;
private final String director;
private final String language;

/**
* Constructor.
*
* @param id video unique id
* @param title video title
* @param len video length in minutes
* @param desc video description by publisher
* @param director video director name
* @param lang video language {private, public}
*/
public Video(Integer id, String title, Integer len, String desc, String director, String lang) {
this.id = id;
this.title = title;
this.length = len;
this.description = desc;
this.director = director;
this.language = lang;
}

/**
* ToString.
*
* @return json representaion of video
*/
@Override
public String toString() {
return "{"
+ "\"id\": " + id + ","
+ "\"title\": \"" + title + "\","
+ "\"length\": " + length + ","
+ "\"description\": \"" + description + "\","
+ "\"director\": \"" + director + "\","
+ "\"language\": \"" + language + "\","
+ "}";
}
public record Video(Integer id, String title, Integer length, String description, String director, String language)
{
@Override
public String toString() {
return "{"
+ "\"id\": " + id + ","
+ "\"title\": \"" + title + "\","
+ "\"length\": " + length + ","
+ "\"description\": \"" + description + "\","
+ "\"director\": \"" + director + "\","
+ "\"language\": \"" + language + "\","
+ "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,18 @@
import java.util.Map;

/**
* The resource class which serves video information. This class act as server in the demo. Which
* The resource record class which serves video information. This class act as server in the demo. Which
* has all video details.
*/
public class VideoResource {
private final FieldJsonMapper fieldJsonMapper;
private final Map<Integer, Video> videos;

/**
* Constructor.
*
* @param fieldJsonMapper map object to json.
* @param videos initialize resource with existing videos. Act as database.
*/
public VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
this.fieldJsonMapper = fieldJsonMapper;
this.videos = videos;
}
/**
*
* @param fieldJsonMapper map object to json.
* @param videos initialize resource with existing videos. Act as database.
*/

public record VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos)
{
/**
* Get Details.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPepper
public void mix() {
LOGGER
.info("Mixing the immutable stew we find: {} potatoes, {} carrots, {} meat and {} peppers",
data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers());
data.numPotatoes(), data.numCarrots(), data.numMeat(), data.numPeppers());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,11 @@
/**
* Stew ingredients.
*/
public class StewData {

private final int numPotatoes;
private final int numCarrots;
private final int numMeat;
private final int numPeppers;

/**
* Constructor.
*/
public StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers) {
this.numPotatoes = numPotatoes;
this.numCarrots = numCarrots;
this.numMeat = numMeat;
this.numPeppers = numPeppers;
}

public int getNumPotatoes() {
return numPotatoes;
}

public int getNumCarrots() {
return numCarrots;
}

public int getNumMeat() {
return numMeat;
}
/**
* Record class
*/

public int getNumPeppers() {
return numPeppers;
}
public record StewData(int numPotatoes, int numCarrots, int numMeat, int numPeppers)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Consumer(String name, ItemQueue queue) {
public void consume() throws InterruptedException {
var item = queue.take();
LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name,
item.getId(), item.getProducer());
item.id(), item.producer());

}
}

0 comments on commit c32a019

Please sign in to comment.