Advertisement
crassus9999

Untitled

Apr 16th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package cz.cvut.k36.pr2.hw.hw06.impl;
  2.  
  3. import cz.cvut.k36.pr2.hw.hw06.Function;
  4. import cz.cvut.k36.pr2.hw.hw06.Pr2List;
  5. import cz.cvut.k36.pr2.hw.hw06.Predicate;
  6.  
  7. /**
  8.  *
  9.  * @author Lukas
  10.  */
  11. public class FilterView<T> extends View<T> {
  12.    
  13.     Pr2List<T> source;
  14.     final Predicate<T> predicate;
  15.     Boolean isEmpty;
  16.  
  17.     public FilterView(Pr2List<T> source, Predicate<T> predicate) {
  18.         this.source = source;
  19.         this.predicate = predicate;
  20.     }
  21.  
  22.     @Override
  23.     public T head() {
  24.         if (isEmpty())
  25.             throw new UnsupportedOperationException("Prázdný list.");
  26.         return source.head();
  27.     }
  28.    
  29.     @Override
  30.     public Pr2List<T> tail() {
  31.         if (isEmpty())
  32.             throw new UnsupportedOperationException("Prázdný list.");
  33.         return new FilterView(source.tail(), predicate);
  34.     }
  35.    
  36.     @Override
  37.     public boolean isEmpty() {
  38.         if(isEmpty != null) return isEmpty;
  39.         if(source.isEmpty()) return isEmpty = true;
  40.        
  41.         if(predicate.apply(source.head())) return isEmpty = false;
  42.        
  43.         source = source.tail();
  44.         return isEmpty();
  45.     }
  46.    
  47.     @Override
  48.     public int size() {
  49.         if(source.isEmpty()) return 0;
  50.        
  51.         if(predicate.apply(source.head()))
  52.             return 1 + new FilterView(source.tail(), predicate).size();
  53.        
  54.         source = source.tail();
  55.         return size();
  56.     }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement