Coverage Report - org.apache.commons.functor.generator.GenerateUntil
 
Classes in this File Line Coverage Branch Coverage Complexity
GenerateUntil
37%
9/24
12%
2/16
3
GenerateUntil$1
100%
5/5
100%
2/2
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.functor.generator;
 18  
 
 19  
 import org.apache.commons.functor.UnaryPredicate;
 20  
 import org.apache.commons.functor.UnaryProcedure;
 21  
 
 22  
 /**
 23  
  * Wrap another {@link Generator} such that {@link #run(UnaryProcedure)} terminates once
 24  
  * a condition has been satisfied (test after).
 25  
  *
 26  
  * @version $Revision: 1156796 $ $Date: 2011-08-11 22:09:21 +0200 (Thu, 11 Aug 2011) $
 27  
  */
 28  8
 public class GenerateUntil<E> extends BaseGenerator<E> {
 29  
     private final UnaryPredicate<? super E> test;
 30  
 
 31  
     /**
 32  
      * Create a new GenerateUntil.
 33  
      * @param wrapped {@link Generator}
 34  
      * @param test {@link UnaryPredicate}
 35  
      */
 36  
     public GenerateUntil(Generator<? extends E> wrapped, UnaryPredicate<? super E> test) {
 37  2
         super(wrapped);
 38  2
         if (wrapped == null) {
 39  0
             throw new IllegalArgumentException("Generator argument was null");
 40  
         }
 41  2
         if (test == null) {
 42  0
             throw new IllegalArgumentException("UnaryPredicate argument was null");
 43  
         }
 44  2
         this.test = test;
 45  2
     }
 46  
 
 47  
     /**
 48  
      * {@inheritDoc}
 49  
      */
 50  
     public void run(final UnaryProcedure<? super E> proc) {
 51  2
         getWrappedGenerator().run(new UnaryProcedure<E>() {
 52  
             public void run(E obj) {
 53  8
                 proc.run(obj);
 54  8
                 if (test.test(obj)) {
 55  2
                     getWrappedGenerator().stop();
 56  
                 }
 57  8
             }
 58  
         });
 59  2
     }
 60  
 
 61  
     /**
 62  
      * {@inheritDoc}
 63  
      */
 64  
     @SuppressWarnings("unchecked")
 65  
     @Override
 66  
     protected Generator<? extends E> getWrappedGenerator() {
 67  4
         return (Generator<? extends E>) super.getWrappedGenerator();
 68  
     }
 69  
 
 70  
     /**
 71  
      * {@inheritDoc}
 72  
      */
 73  
     public boolean equals(Object obj) {
 74  0
         if (obj == this) {
 75  0
             return true;
 76  
         }
 77  0
         if (!(obj instanceof GenerateUntil<?>)) {
 78  0
             return false;
 79  
         }
 80  0
         GenerateUntil<?> other = (GenerateUntil<?>) obj;
 81  0
         return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.test.equals(test);
 82  
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public int hashCode() {
 88  0
         int result = "GenerateUntil".hashCode();
 89  0
         result <<= 2;
 90  0
         Generator<?> gen = getWrappedGenerator();
 91  0
         result ^= gen == null ? 0 : gen.hashCode();
 92  0
         result <<= 2;
 93  0
         result ^= test == null ? 0 : test.hashCode();
 94  0
         return result;
 95  
     }
 96  
 }