Skip to content

Commit

Permalink
Merge pull request #6 from bifidok/hw4
Browse files Browse the repository at this point in the history
Hw4
  • Loading branch information
bifidok authored Nov 14, 2023
2 parents 59f0cb7 + d69ea91 commit 9c7be54
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/edu/hw4/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edu.hw4;

public record Animal(
String name,
Type type,
Sex sex,
int age,
int height,
int weight,
boolean bites
) {
private static final int DOG_AND_CAT_PAWS_COUNT = 4;
private static final int BIRD_PAWS_COUNT = 2;
private static final int FISH_PAWS_COUNT = 0;
private static final int SPIDER_PAWS_COUNT = 8;

enum Type {
CAT, DOG, BIRD, FISH, SPIDER
}

enum Sex {
M, F
}

public int paws() {
return switch (type) {
case CAT, DOG -> DOG_AND_CAT_PAWS_COUNT;
case BIRD -> BIRD_PAWS_COUNT;
case FISH -> FISH_PAWS_COUNT;
case SPIDER -> SPIDER_PAWS_COUNT;
};
}
}
171 changes: 171 additions & 0 deletions src/main/java/edu/hw4/AnimalRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package edu.hw4;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AnimalRepository {
private final static int HUNDRED_CENTIMETERS = 100;

private AnimalRepository() {

}

public static List<Animal> sortByHeight(List<Animal> list) {
return list.stream()
.sorted(Comparator.comparingInt(Animal::height))
.collect(Collectors.toList());
}

public static List<Animal> firstKSortedByWeight(List<Animal> list, int k) {
return list.stream()
.sorted(((animal1, animal2) -> animal2.weight() - animal1.weight()))
.limit(k)
.collect(Collectors.toList());
}

public static Map<Animal.Type, Integer> getTypesCount(List<Animal> list) {
return list.stream()
.collect(Collectors.toMap(Animal::type, animal -> 1, Math::addExact));
}

public static Animal getByLongestName(List<Animal> list) {
return list.stream()
.max(Comparator.comparingInt(animal -> animal.name().length())).orElse(null);
}

public static Animal.Sex getGreaterSex(List<Animal> list) {
return list.stream()
.collect(Collectors.groupingBy(Animal::sex, Collectors.counting()))
.entrySet().stream()
.max(Comparator.comparingLong(Map.Entry::getValue))
.map(Map.Entry::getKey).orElse(null);
}

public static Map<Animal.Type, Animal> getHeavierAnimalForEachType(List<Animal> list) {
return list.stream()
.collect(Collectors.toMap(Animal::type, animal -> animal,
BinaryOperator.maxBy(Comparator.comparing(Animal::weight))
));
}

public static Animal getKOldest(List<Animal> list, int k) {
var sortedList = list.stream()
.sorted(Comparator.comparingInt(Animal::age).reversed())
.limit(k + 1)
.toList();
return k > sortedList.size() ? null : sortedList.get(k - 1);
}

public static Optional<Animal> getHeavierBelowK(List<Animal> list, int k) {
return list.stream()
.filter(animal -> animal.weight() < k)
.max(Comparator.comparingInt(Animal::weight));
}

public static Integer countAllPaws(List<Animal> list) {
return list.stream()
.mapToInt(Animal::paws)
.sum();
}

public static List<Animal> getAllWhereAgeIsNotEqualToPawsCount(List<Animal> list) {
return list.stream()
.filter(animal -> animal.age() != animal.paws())
.collect(Collectors.toList());
}

public static List<Animal> getAllWhichBitesAndHigherHundredCM(List<Animal> list) {
return list.stream()
.filter(animal -> animal.bites())
.filter(animal -> animal.height() > HUNDRED_CENTIMETERS)
.collect(Collectors.toList());
}

public static Integer countAnimalsWhereWeightLargerThanHeight(List<Animal> list) {
return Math.toIntExact(list.stream()
.filter(animal -> animal.weight() > animal.height())
.count());
}

public static List<Animal> getAllWhereNameHasMoreThanTwoWords(List<Animal> list) {
return list.stream()
.filter(animal -> animal.name().split(" ").length > 2)
.collect(Collectors.toList());
}

public static boolean containsDogHeightLargerThanK(List<Animal> list, int k) {
return list.stream()
.filter(animal -> animal.type().equals(Animal.Type.DOG))
.anyMatch(animal -> animal.height() > k);
}

public static Integer countWeightSumByAge(List<Animal> list, int ageFrom, int ageTo) {
return list.stream()
.filter(animal -> animal.age() >= ageFrom && animal.age() <= ageTo)
.mapToInt(Animal::weight)
.sum();
}

public static List<Animal> sortByTypeThenSexThenName(List<Animal> list) {
return list.stream()
.sorted(Comparator.comparing(Animal::type)
.thenComparing(Animal::sex)
.thenComparing(Animal::name))
.toList();
}

public static boolean isSpiderBitesOftenThanDog(List<Animal> list) {
long spiderBitesCount = list.stream()
.filter(animal -> animal.type().equals(Animal.Type.SPIDER))
.filter(Animal::bites)
.count();
long dogBitesCount = list.stream()
.filter(animal -> animal.type().equals(Animal.Type.DOG))
.filter(Animal::bites)
.count();
return spiderBitesCount > dogBitesCount;
}

public static Animal getHeavierFish(List<Animal>... list) {
return Arrays.stream(list)
.flatMap(Collection::stream)
.filter(animal -> animal.type().equals(Animal.Type.FISH))
.max(Comparator.comparing(Animal::weight))
.orElse(null);
}

public static Map<String, Set<ValidationError>> getAnimalsWithBadRecords(List<Animal> list) {
Map<String, ValidationError> typeFieldErrors = AnimalValidator.validateTypeProperty(list);
Map<String, ValidationError> sexFieldErrors = AnimalValidator.validateSexProperty(list);
Map<String, ValidationError> numberVariablesFieldErrors = AnimalValidator.validateNumericProperties(list);

return Stream.of(typeFieldErrors, sexFieldErrors, numberVariablesFieldErrors)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
entry -> entry.getKey(),
Collectors.mapping(entry -> entry.getValue(), Collectors.toSet())
));
}

public static Map<String, String> getAnimalsWithBadRecordsBetter(List<Animal> list) {
Map<String, ValidationError> typeFieldErrors = AnimalValidator.validateTypeProperty(list);
Map<String, ValidationError> sexFieldErrors = AnimalValidator.validateSexProperty(list);
Map<String, ValidationError> numberVariablesFieldErrors = AnimalValidator.validateNumericProperties(list);

return Stream.of(typeFieldErrors, sexFieldErrors, numberVariablesFieldErrors)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(
entry -> entry.getKey(),
Collectors.mapping(e -> e.getValue().property(), Collectors.joining(", "))
));
}

}
35 changes: 35 additions & 0 deletions src/main/java/edu/hw4/AnimalValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package edu.hw4;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AnimalValidator {
private static final String TYPE_ERROR_MESSAGE = "type cant be null";
private static final String SEX_ERROR_MESSAGE = "sex cant be null";
private static final String NUM_ERROR_MESSAGE = "nums cant be less than 0";

private AnimalValidator() {
}

public static Map<String, ValidationError> validateTypeProperty(List<Animal> animals) {
return animals.stream()
.filter(o -> o.type() == null)
.collect(Collectors
.toMap(Animal::name, a -> new ValidationError("type", TYPE_ERROR_MESSAGE)));
}

public static Map<String, ValidationError> validateSexProperty(List<Animal> animals) {
return animals.stream()
.filter(o -> o.sex() == null)
.collect(Collectors
.toMap(Animal::name, a -> new ValidationError("sex", SEX_ERROR_MESSAGE)));
}

public static Map<String, ValidationError> validateNumericProperties(List<Animal> animals) {
return animals.stream()
.filter(o -> o.age() < 0 || o.weight() < 0 || o.height() < 0)
.collect(Collectors
.toMap(Animal::name, a -> new ValidationError("age/weight/height", NUM_ERROR_MESSAGE)));
}
}
5 changes: 5 additions & 0 deletions src/main/java/edu/hw4/ValidationError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.hw4;

public record ValidationError(String property, String message) {

}
Loading

0 comments on commit 9c7be54

Please sign in to comment.