POO-ITBA / 2024_02

Consultas 2C 2024
2 stars 0 forks source link

1C2024.Ej1 #22

Open joseravera opened 3 weeks ago

joseravera commented 3 weeks ago

Hola, cómo va? Quería consultar si esta solución está bien, ya que no usé SequencedSet. Gracias!

MatchCollection

package Par1C2024.Ej1;

import java.util.List;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.function.Predicate;

public interface MatchCollection<E extends Comparable<? super E>> extends List<E>{

    SortedSet<E> getAllMatched();

    SortedSet<E> getAllMatchedReversed();

    void addMatchPredicate(Predicate<E> predicate);

}

MatchCollectionImpl


public class MatchCollectionImpl<E extends Comparable<? super E>> extends ArrayList<E> implements MatchCollection<E> {
    private List<Predicate<E>> predicates = new ArrayList<>();

    @Override
    public SortedSet<E> getAllMatched() {
        return getAll(Comparator.naturalOrder());
    }

    @Override
    public SortedSet<E> getAllMatchedReversed(){
        return getAll(Comparator.reverseOrder());
    }

    public SortedSet<E> getAll(Comparator<E> cmp) {
        SortedSet<E> set = new TreeSet<>(cmp);
        for (E element : this) {
            if (satisfies(element)) {
              set.add(element);
            }
        }
        return set;
    }

    private boolean satisfies(E element) {
        for (Predicate<E> predicate : predicates) {
            if (!predicate.test(element)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void addMatchPredicate(Predicate<E> predicate){
        predicates.add(predicate);
    }
}
fmeola commented 3 weeks ago

Hola @joseravera La implementación es correcta.