Coverage Report - org.apache.commons.functor.core.composite.Sequence
 
Classes in this File Line Coverage Branch Coverage Complexity
Sequence
77%
17/22
70%
14/20
1.889
 
 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.core.composite;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.commons.functor.Procedure;
 25  
 
 26  
 /**
 27  
  * A {@link Procedure Procedure}
 28  
  * that {@link Procedure#run runs} an ordered
 29  
  * sequence of {@link Procedure Procedures}.
 30  
  * When the sequence is empty, this procedure is does
 31  
  * nothing.
 32  
  * <p>
 33  
  * Note that although this class implements
 34  
  * {@link Serializable}, a given instance will
 35  
  * only be truly <code>Serializable</code> if all the
 36  
  * underlying functors are.  Attempts to serialize
 37  
  * an instance whose delegates are not all
 38  
  * <code>Serializable</code> will result in an exception.
 39  
  * </p>
 40  
  * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
 41  
  * @author Rodney Waldhoff
 42  
  */
 43  
 public class Sequence implements Procedure, Serializable {
 44  
 
 45  
     /**
 46  
      * serialVersionUID declaration.
 47  
      */
 48  
     private static final long serialVersionUID = 8041703589149547883L;
 49  
     // attributes
 50  
     // ------------------------------------------------------------------------
 51  32
     private List<Procedure> list = new ArrayList<Procedure>();
 52  
 
 53  
     // constructor
 54  
     // ------------------------------------------------------------------------
 55  
     /**
 56  
      * Create a new Sequence.
 57  
      */
 58  
     public Sequence() {
 59  32
         super();
 60  32
     }
 61  
 
 62  
     /**
 63  
      * Create a new Sequence instance.
 64  
      *
 65  
      * @param procedures to run sequentially
 66  
      */
 67  
     public Sequence(Procedure... procedures) {
 68  24
         this();
 69  24
         if (procedures != null) {
 70  70
             for (Procedure p : procedures) {
 71  46
                 then(p);
 72  
             }
 73  
         }
 74  24
     }
 75  
 
 76  
     /**
 77  
      * Create a new Sequence instance.
 78  
      *
 79  
      * @param procedures to run sequentially
 80  
      */
 81  
     public Sequence(Iterable<Procedure> procedures) {
 82  0
         this();
 83  0
         if (procedures != null) {
 84  0
             for (Procedure p : procedures) {
 85  0
                 then(p);
 86  
             }
 87  
         }
 88  0
     }
 89  
 
 90  
     // modifiers
 91  
     // ------------------------------------------------------------------------
 92  
     /**
 93  
      * Fluently add a Procedure.
 94  
      * @param p Procedure to add
 95  
      * @return this
 96  
      */
 97  
     public final Sequence then(Procedure p) {
 98  90
         list.add(p);
 99  90
         return this;
 100  
     }
 101  
 
 102  
     // predicate interface
 103  
     // ------------------------------------------------------------------------
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public final void run() {
 108  28
         for (Iterator<Procedure> iter = list.iterator(); iter.hasNext();) {
 109  116
             iter.next().run();
 110  
         }
 111  28
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     public final boolean equals(Object that) {
 117  126
         return that == this || (that instanceof Sequence && equals((Sequence) that));
 118  
     }
 119  
 
 120  
     /**
 121  
      * Learn whether a given Sequence is equal to this.
 122  
      * @param that Sequence to test
 123  
      * @return boolean
 124  
      */
 125  
     public boolean equals(Sequence that) {
 126  
         // by construction, list is never null
 127  118
         return null != that && list.equals(that.list);
 128  
     }
 129  
 
 130  
     /**
 131  
      * {@inheritDoc}
 132  
      */
 133  
     public int hashCode() {
 134  
         // by construction, list is never null
 135  140
         return "Sequence".hashCode() ^ list.hashCode();
 136  
     }
 137  
 
 138  
     /**
 139  
      * {@inheritDoc}
 140  
      */
 141  
     public String toString() {
 142  132
         return "Sequence<" + list + ">";
 143  
     }
 144  
 
 145  
 }