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 java.util.Iterator;
- import java.util.NoSuchElementException;
- /**
- *
- * @author Azathoth
- */
- public class NotNilViewFilter<Input, Output> implements Pr2List<Input>{
- protected Pr2List<Input> list;
- private Predicate predicate;
- private boolean lazyEvaluate=false;
- private boolean lazyFilter=false;
- private boolean lazyMap=false;
- private boolean lazyPartialFilter=false;
- private boolean lazyPartialMap=false;
- protected Pr2List<Output> listHead;
- public NotNilViewFilter(Pr2List list, Predicate predicate) {
- this.list = list;
- this.predicate = predicate;
- }
- @Override
- public Pr2List append(Input t) {
- throw new UnsupportedOperationException("Can not append view.");
- }
- @Override
- public Pr2List cons(Input t) {
- throw new UnsupportedOperationException("Can not cons view.");
- }
- @Override
- public boolean isEmpty() {
- lazyIsEmpty();
- return this.list.isEmpty();
- }
- @Override
- public Pr2List filter(Predicate predicate) {
- Pr2List output=new NotNilViewFilter(this, predicate);
- return output;
- }
- @Override
- public <O> Pr2List<O> map(Function<Input, O> function){
- Pr2List output=new NotNilViewMap(this, function);
- return output;
- }
- @Override
- public Input head() {
- lazyHead();
- return this.list.head();
- }
- @Override
- public Pr2List tail() {
- lazyTail();
- NotNilViewFilter<Input, Output> output=new NotNilViewFilter<>(this.list.tail(), predicate);
- return output;
- }
- @Override
- public Pr2List view() {
- NotNilView<Input, Output> output=new NotNilView(this);
- return output;
- }
- @Override
- public int size() {
- lazySize();
- if (this.listHead!=null) {
- return this.listHead.size();
- }
- return this.list.size();
- }
- @Override
- public Iterator iterator() {
- return new MyIterator();
- }
- @Override
- public String toString() {
- return this.getClass()+", "+this.list.toString();
- }
- private class MyIterator implements Iterator{
- Pr2List<Input> iterator;
- private MyIterator() {
- this.iterator = NotNilViewFilter.this;
- }
- @Override
- public boolean hasNext() {
- return !(iterator.tail().isEmpty());
- }
- @Override
- public Input next() {
- if (iterator.isEmpty()) {
- throw new NoSuchElementException("Toto je poslednĂ prvek.");
- }
- iterator=iterator.tail();
- return iterator.head();
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Z iteratoru nejde nic odejmout.");
- }
- }
- public void lazyTail(){
- if (!lazyEvaluate) {
- this.lazyEvaluate=true;
- if (this.list instanceof NotNilViewFilter) {
- NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
- temp.lazyTail();
- this.list=temp.list;
- } else if (this.list instanceof NotNilViewMap) {
- NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
- temp.lazyTail();
- this.list=temp.list;
- } else if (this.list instanceof NotNil) {
- NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
- Pr2List<Input> temp2;
- temp2=temp.partialFilter(predicate);
- this.list=temp2;
- } else {
- this.list=this.list.filter(predicate);
- }
- }
- }
- public void lazyHead(){
- this.lazyFilter=true;
- if (this.list instanceof NotNilViewFilter) {
- NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
- temp.lazyHead();
- this.list=temp.list;
- } else if (this.list instanceof NotNilViewMap) {
- NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
- temp.lazyHead();
- this.list=temp.list;
- } else if (this.list instanceof NotNil) {
- NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
- Pr2List<Input> temp2;
- temp2=temp.partialFilter(predicate);
- this.list=temp2;
- } else {
- this.list=this.list.filter(predicate);
- }
- }
- public void lazySize(){
- this.lazyFilter=true;
- if (this.list instanceof NotNilViewFilter) {
- NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
- temp.lazySize();
- this.list=temp.list;
- } else if (this.list instanceof NotNilViewMap) {
- NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
- temp.lazyMap();
- this.listHead=temp.listHead;
- } if (this.listHead!=null) {
- this.listHead=this.listHead.filter(predicate);
- } else {
- this.list=this.list.filter(predicate);
- }
- }
- public void lazyIsEmpty(){
- this.lazyPartialFilter=true;
- if (this.list instanceof NotNilViewFilter) {
- NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
- temp.lazyIsEmpty();
- this.list=temp.list;
- } else if (this.list instanceof NotNilViewMap) {
- NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
- temp.lazyIsEmpty();
- this.list=temp.list;
- } else if (this.list instanceof NotNil) {
- NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
- Pr2List<Input> temp2;
- temp2=temp.partialFilter(predicate);
- this.list=temp2;
- } else {
- this.list=this.list.filter(predicate);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment