Azathoth42

Untitled

Apr 14th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package cz.cvut.k36.pr2.hw.hw06.impl;
  8.  
  9. import cz.cvut.k36.pr2.hw.hw06.Function;
  10. import cz.cvut.k36.pr2.hw.hw06.Pr2List;
  11. import cz.cvut.k36.pr2.hw.hw06.Predicate;
  12. import java.util.Iterator;
  13. import java.util.NoSuchElementException;
  14.  
  15. /**
  16.  *
  17.  * @author Azathoth
  18.  */
  19. public class NotNilViewFilter<Input, Output> implements Pr2List<Input>{
  20.    
  21.     protected Pr2List<Input> list;
  22.     private Predicate predicate;
  23.     private boolean lazyEvaluate=false;
  24.     private boolean lazyFilter=false;
  25.     private boolean lazyMap=false;
  26.     private boolean lazyPartialFilter=false;
  27.     private boolean lazyPartialMap=false;
  28.     protected Pr2List<Output> listHead;
  29.    
  30.     public NotNilViewFilter(Pr2List list, Predicate predicate) {
  31.         this.list = list;
  32.         this.predicate = predicate;
  33.     }
  34.    
  35.     @Override
  36.     public Pr2List append(Input t) {
  37.         throw new UnsupportedOperationException("Can not append view.");
  38.     }
  39.  
  40.     @Override
  41.     public Pr2List cons(Input t) {
  42.         throw new UnsupportedOperationException("Can not cons view.");
  43.     }
  44.  
  45.     @Override
  46.     public boolean isEmpty() {
  47.         lazyIsEmpty();
  48.         return this.list.isEmpty();
  49.     }
  50.    
  51.     @Override
  52.     public Pr2List filter(Predicate predicate) {
  53.         Pr2List output=new NotNilViewFilter(this, predicate);
  54.         return output;
  55.     }
  56.  
  57.     @Override
  58.     public <O> Pr2List<O> map(Function<Input, O> function){
  59.         Pr2List output=new NotNilViewMap(this, function);
  60.         return output;
  61.     }
  62.  
  63.     @Override
  64.     public Input head() {
  65.         lazyHead();
  66.         return this.list.head();
  67.     }
  68.  
  69.     @Override
  70.     public Pr2List tail() {
  71.         lazyTail();
  72.         NotNilViewFilter<Input, Output> output=new NotNilViewFilter<>(this.list.tail(), predicate);
  73.         return output;
  74.     }
  75.  
  76.     @Override
  77.     public Pr2List view() {
  78.         NotNilView<Input, Output> output=new NotNilView(this);
  79.         return output;
  80.     }
  81.  
  82.     @Override
  83.     public int size() {
  84.         lazySize();
  85.         if (this.listHead!=null) {
  86.             return this.listHead.size();
  87.         }
  88.         return this.list.size();
  89.     }
  90.  
  91.     @Override
  92.     public Iterator iterator() {
  93.         return new MyIterator();
  94.     }
  95.  
  96.     @Override
  97.     public String toString() {
  98.         return this.getClass()+", "+this.list.toString();
  99.     }
  100.    
  101.     private class MyIterator implements Iterator{
  102.  
  103.         Pr2List<Input> iterator;
  104.        
  105.         private MyIterator() {
  106.             this.iterator = NotNilViewFilter.this;
  107.         }
  108.        
  109.         @Override
  110.         public boolean hasNext() {
  111.             return !(iterator.tail().isEmpty());
  112.         }
  113.  
  114.         @Override
  115.         public Input next() {
  116.             if (iterator.isEmpty()) {
  117.                 throw new NoSuchElementException("Toto je poslednĂ­ prvek.");
  118.             }
  119.             iterator=iterator.tail();
  120.             return iterator.head();
  121.         }
  122.  
  123.         @Override
  124.         public void remove() {
  125.             throw new UnsupportedOperationException("Z iteratoru nejde nic odejmout.");
  126.         }
  127.     }
  128.  
  129.     public void lazyTail(){
  130.         if (!lazyEvaluate) {
  131.             this.lazyEvaluate=true;
  132.             if (this.list instanceof NotNilViewFilter) {
  133.                 NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
  134.                 temp.lazyTail();
  135.                 this.list=temp.list;
  136.             } else if (this.list instanceof NotNilViewMap) {
  137.                 NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
  138.                 temp.lazyTail();
  139.                 this.list=temp.list;
  140.             } else if (this.list instanceof NotNil) {
  141.                 NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
  142.                 Pr2List<Input> temp2;
  143.                 temp2=temp.partialFilter(predicate);
  144.                 this.list=temp2;
  145.             } else {
  146.                 this.list=this.list.filter(predicate);
  147.             }
  148.         }
  149.     }
  150.    
  151.     public void lazyHead(){
  152.         this.lazyFilter=true;
  153.         if (this.list instanceof NotNilViewFilter) {
  154.             NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
  155.             temp.lazyHead();
  156.             this.list=temp.list;
  157.         } else if (this.list instanceof NotNilViewMap) {
  158.             NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
  159.             temp.lazyHead();
  160.             this.list=temp.list;
  161.         } else if (this.list instanceof NotNil) {
  162.             NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
  163.             Pr2List<Input> temp2;
  164.             temp2=temp.partialFilter(predicate);
  165.             this.list=temp2;
  166.         } else {
  167.             this.list=this.list.filter(predicate);
  168.         }
  169.     }
  170.    
  171.     public void lazySize(){
  172.         this.lazyFilter=true;
  173.         if (this.list instanceof NotNilViewFilter) {
  174.             NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
  175.             temp.lazySize();
  176.             this.list=temp.list;
  177.         } else if (this.list instanceof NotNilViewMap) {
  178.             NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
  179.             temp.lazyMap();
  180.             this.listHead=temp.listHead;
  181.         } if (this.listHead!=null) {
  182.             this.listHead=this.listHead.filter(predicate);
  183.         } else {
  184.             this.list=this.list.filter(predicate);
  185.         }
  186.        
  187.     }
  188.    
  189.     public void lazyIsEmpty(){
  190.         this.lazyPartialFilter=true;
  191.         if (this.list instanceof NotNilViewFilter) {
  192.             NotNilViewFilter<Input, Output> temp=(NotNilViewFilter<Input, Output>) this.list;
  193.             temp.lazyIsEmpty();
  194.             this.list=temp.list;
  195.         } else if (this.list instanceof NotNilViewMap) {
  196.             NotNilViewMap<Input, Output> temp=(NotNilViewMap<Input, Output>) this.list;
  197.             temp.lazyIsEmpty();
  198.             this.list=temp.list;
  199.         } else if (this.list instanceof NotNil) {
  200.             NotNil<Input, Output> temp=(NotNil<Input, Output>) this.list;
  201.             Pr2List<Input> temp2;
  202.             temp2=temp.partialFilter(predicate);
  203.             this.list=temp2;
  204.         } else {
  205.             this.list=this.list.filter(predicate);
  206.         }
  207.     }
  208.    
  209.    
  210. }
Advertisement
Add Comment
Please, Sign In to add comment