Azathoth42

definitelyNotFilter

Apr 17th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 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 static cz.cvut.k36.pr2.hw.hw06.impl.ListFactory.nil;
  13. import java.util.Iterator;
  14. import java.util.NoSuchElementException;
  15.  
  16. /**
  17.  *
  18.  * @author Azathoth
  19.  */
  20. public class NotNilViewFilter<Input> extends NotNilView<Input>{
  21.    
  22.     private Predicate<Input> predicate;
  23.     protected Pr2List<Input> list;
  24.     Input head=null;
  25.  
  26.     public NotNilViewFilter(Pr2List list, Predicate predicate) {
  27.         super(list);
  28.         this.list=list;
  29.         this.predicate=predicate;
  30.     }
  31.  
  32.     @Override
  33.     public boolean isEmpty() {
  34.         if (this.list.isEmpty()) {
  35.             return this.list.isEmpty();
  36.         }
  37.         head=this.list.head();
  38.         if (predicate.apply(head)) {
  39.             return false;
  40.         } else {
  41.             this.list=this.list.tail();
  42.         }
  43.         return this.isEmpty();
  44.     }
  45.    
  46.     @Override
  47.     public Pr2List filter(Predicate predicate) {
  48.         Pr2List<Input> output=new NotNilViewFilter<Input>(this, predicate);
  49.         return output;
  50.     }
  51.  
  52.     @Override
  53.     public <Output> Pr2List<Output> map(Function<Input, Output> function){
  54.         Pr2List<Output> output=new NotNilViewMap<Input, Output>(this, function);
  55.         return output;
  56.     }
  57.  
  58.     @Override
  59.     public Input head() {
  60.         if (isEmpty()) {
  61.             return this.list.head();
  62.         }
  63.         return head;
  64.     }
  65.  
  66.     @Override
  67.     public Pr2List tail() {
  68.         this.isEmpty();
  69.         NotNilViewFilter<Input> output=new NotNilViewFilter(this.list.tail(), predicate);
  70.         return output;
  71.     }
  72.  
  73.     @Override
  74.     public Pr2List view() {
  75.         NotNilView<Input> output=new NotNilView(this);
  76.         return output;
  77.     }
  78.  
  79.     @Override
  80.     public int size() {
  81.         isEmpty();
  82.         return this.list.size();
  83.     }
  84.  
  85.     @Override
  86.     public Iterator iterator() {
  87.         return new MyIterator();
  88.     }
  89.  
  90.     @Override
  91.     public String toString() {
  92.         return this.getClass()+", "+this.list.toString();
  93.     }
  94.    
  95.     private class MyIterator<Input> implements Iterator<Input>{
  96.  
  97.         Pr2List<Input> iterator=new NotNilViewFilter<>(list,predicate);
  98.         Input head;
  99.         Pr2List<Input> next;
  100.        
  101.         @Override
  102.         public boolean hasNext() {
  103.             System.out.println("hasNextBegin");
  104.             if (!iterator.equals(nil())) {
  105.                 try {
  106.                     next=iterator.tail();
  107.                 } catch (UnsupportedOperationException e){
  108.                     return false;
  109.                 }
  110.             }
  111.             if (!next.equals(nil())) {
  112.                 System.out.println("hasNextEnd");
  113.                 return true;
  114.             }
  115.             System.out.println("hasNextEnd");
  116.             return false;
  117.         }
  118.  
  119.         @Override
  120.         public Input next() {
  121.             System.out.println("nextBegin");
  122.             Input output;
  123.             if (iterator instanceof NotNilViewFilter) {
  124.                 NotNilViewFilter<Input> temp=(NotNilViewFilter) iterator;
  125.                 head=temp.list.head();
  126.             } else {
  127.                 head=iterator.head();
  128.             }
  129.             iterator=next;
  130.             output=head;
  131.             System.out.println("nextEnd");
  132.             return output;
  133.         }
  134.  
  135.         @Override
  136.         public void remove() {
  137.             throw new UnsupportedOperationException("Z view nejde odebĂ­rat.");
  138.         }
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment