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 @Data @NoArgsConstructor @EqualsAndHashCode @ToString
public class Coordinates { public class Coordinates implements Comparable<Coordinates> {
/** /**
* Координата X типа float * Координата X типа float
@ -45,4 +45,13 @@ public class Coordinates {
throw new IllegalArgumentException("Поле y класса Coordinates должно быть больше -816"); throw new IllegalArgumentException("Поле y класса Coordinates должно быть больше -816");
this.y = y; 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 @Override
public int compareTo(Location other) { public int compareTo(Location other) {
return Comparator.comparing((Location loc) -> loc.name) return Comparator.comparing(Location::getName)
.thenComparing(Location::distance) .thenComparing(Location::distance)
.compare(this, other); .compare(this, other);
} }

View file

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