Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- 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;
- import static cz.cvut.k36.pr2.hw.hw06.impl.ListFactory.nil;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- /**
- *
- * @author Azathoth
- */
- public class NotNilViewFilter<Input> extends NotNilView<Input>{
- private Predicate<Input> predicate;
- protected Pr2List<Input> list;
- Input head=null;
- public NotNilViewFilter(Pr2List list, Predicate predicate) {
- super(list);
- this.list=list;
- this.predicate=predicate;
- }
- @Override
- public boolean isEmpty() {
- if (this.list.isEmpty()) {
- return this.list.isEmpty();
- }
- head=this.list.head();
- if (predicate.apply(head)) {
- return false;
- } else {
- this.list=this.list.tail();
- }
- return this.isEmpty();
- }
- @Override
- public Pr2List filter(Predicate predicate) {
- Pr2List<Input> output=new NotNilViewFilter<Input>(this, predicate);
- return output;
- }
- @Override
- public <Output> Pr2List<Output> map(Function<Input, Output> function){
- Pr2List<Output> output=new NotNilViewMap<Input, Output>(this, function);
- return output;
- }
- @Override
- public Input head() {
- if (isEmpty()) {
- return this.list.head();
- }
- return head;
- }
- @Override
- public Pr2List tail() {
- this.isEmpty();
- NotNilViewFilter<Input> output=new NotNilViewFilter(this.list.tail(), predicate);
- return output;
- }
- @Override
- public Pr2List view() {
- NotNilView<Input> output=new NotNilView(this);
- return output;
- }
- @Override
- public int size() {
- isEmpty();
- return this.list.size();
- }
- @Override
- public Iterator iterator() {
- return new MyIterator();
- }
- @Override
- public String toString() {
- return this.getClass()+", "+this.list.toString();
- }
- private class MyIterator<Input> implements Iterator<Input>{
- Pr2List<Input> iterator=new NotNilViewFilter<>(list,predicate);
- Input head;
- Pr2List<Input> next;
- @Override
- public boolean hasNext() {
- System.out.println("hasNextBegin");
- if (!iterator.equals(nil())) {
- try {
- next=iterator.tail();
- } catch (UnsupportedOperationException e){
- return false;
- }
- }
- if (!next.equals(nil())) {
- System.out.println("hasNextEnd");
- return true;
- }
- System.out.println("hasNextEnd");
- return false;
- }
- @Override
- public Input next() {
- System.out.println("nextBegin");
- Input output;
- if (iterator instanceof NotNilViewFilter) {
- NotNilViewFilter<Input> temp=(NotNilViewFilter) iterator;
- head=temp.list.head();
- } else {
- head=iterator.head();
- }
- iterator=next;
- output=head;
- System.out.println("nextEnd");
- return output;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Z view nejde odebĂrat.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment