updated compareTo methods of data classes

This commit is contained in:
Egor 2022-02-21 21:25:47 +03:00
parent 7e758b7767
commit bf89415320
3 changed files with 20 additions and 8 deletions

View file

@ -9,7 +9,7 @@ import lombok.ToString;
* Класс данных координат
*/
@Data @NoArgsConstructor @EqualsAndHashCode @ToString
public class Coordinates {
public class Coordinates implements Comparable<Coordinates> {
/**
* Координата X типа float
@ -45,4 +45,13 @@ public class Coordinates {
throw new IllegalArgumentException("Поле y класса Coordinates должно быть больше -816");
this.y = y;
}
private double distance() {
return Math.sqrt(x * x + y * y);
}
@Override
public int compareTo(Coordinates other) {
return Double.compare(this.distance(), other.distance());
}
}

View file

@ -68,7 +68,7 @@ public class Location implements Comparable<Location> {
*/
@Override
public int compareTo(Location other) {
return Comparator.comparing((Location loc) -> loc.name)
return Comparator.comparing(Location::getName)
.thenComparing(Location::distance)
.compare(this, other);
}

View file

@ -171,12 +171,15 @@ public class Person implements Comparable<Person> {
*/
@Override
public int compareTo(Person other) {
return Comparator.comparing((Person p) -> p.name)
.thenComparing(p -> p.passportID)
.thenComparing(p -> p.height)
.thenComparing(p -> p.nationality)
.thenComparing(p -> p.location)
.thenComparing(p -> p.eyeColor)
return Comparator.comparing(Person::getName)
.thenComparing(Person::getPassportID)
.thenComparing(Person::getHeight)
.thenComparing(Person::getCreationDate)
.thenComparing(p -> p.getNationality().toString())
.thenComparing(Person::getLocation)
.thenComparing(Person::getCoordinates)
.thenComparing(p -> p.getEyeColor().toString())
.thenComparing(Person::getId)
.compare(this, other);
}
}