Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.cvut.k36.pr2.hw.hw06.impl;
- import cz.cvut.k36.pr2.hw.hw06.Function;
- import cz.cvut.k36.pr2.hw.hw06.Pr2List;
- import cz.cvut.k36.pr2.hw.hw06.Predicate;
- /**
- *
- * @author Lukas
- */
- public class FilterView<T> extends View<T> {
- Pr2List<T> source;
- final Predicate<T> predicate;
- Boolean isEmpty;
- public FilterView(Pr2List<T> source, Predicate<T> predicate) {
- this.source = source;
- this.predicate = predicate;
- }
- @Override
- public T head() {
- if (isEmpty())
- throw new UnsupportedOperationException("Prázdný list.");
- return source.head();
- }
- @Override
- public Pr2List<T> tail() {
- if (isEmpty())
- throw new UnsupportedOperationException("Prázdný list.");
- return new FilterView(source.tail(), predicate);
- }
- @Override
- public boolean isEmpty() {
- if(isEmpty != null) return isEmpty;
- if(source.isEmpty()) return isEmpty = true;
- if(predicate.apply(source.head())) return isEmpty = false;
- source = source.tail();
- return isEmpty();
- }
- @Override
- public int size() {
- if(source.isEmpty()) return 0;
- if(predicate.apply(source.head()))
- return 1 + new FilterView(source.tail(), predicate).size();
- source = source.tail();
- return size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement