Coverage Report - org.apache.commons.flatfile.EntityCollectionSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityCollectionSupport
82%
50/61
88%
28/32
0
 
 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.flatfile;
 18  
 
 19  
 import java.io.ByteArrayInputStream;
 20  
 import java.io.ByteArrayOutputStream;
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.OutputStream;
 24  
 import java.util.Iterator;
 25  
 
 26  
 import org.apache.commons.lang.ArrayUtils;
 27  
 
 28  
 /**
 29  
  * Implementation of common operations for an EntityCollection.
 30  
  * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 31  
  */
 32  67
 public abstract class EntityCollectionSupport extends EntitySupport implements EntityCollection {
 33  
     /** Serialization version */
 34  
     private static final long serialVersionUID = 5902476686737324304L;
 35  
 
 36  1
     private static final byte[] DEFAULT_DELIMITER = ArrayUtils.EMPTY_BYTE_ARRAY;
 37  
 
 38  67
     private byte[] delim = DEFAULT_DELIMITER;
 39  67
     private boolean delimAfter = true;
 40  67
     private boolean suppressEmptyChildren = true;
 41  
 
 42  
     /**
 43  
      * {@inheritDoc}
 44  
      */
 45  
     public final synchronized int length() {
 46  224
         int result = 0;
 47  224
         for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) {
 48  1646
             Entity e = it.next();
 49  1646
             if (shouldSuppress(e)) {
 50  3
                 continue;
 51  
             }
 52  1643
             result += e.length();
 53  1643
             if (it.hasNext() || isDelimAfter()) {
 54  1631
                 result += getDelim().length;
 55  
             }
 56  1643
         }
 57  224
         return result;
 58  
     }
 59  
 
 60  
     /**
 61  
      * {@inheritDoc}
 62  
      */
 63  
     public final synchronized void readFrom(InputStream is) throws IOException {
 64  20
         for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) {
 65  156
             Entity e = it.next();
 66  156
             if (shouldSuppress(e)) {
 67  0
                 continue;
 68  
             }
 69  156
             e.readFrom(is);
 70  156
             if (it.hasNext() || isDelimAfter()) {
 71  156
                 for (int i = 0; i < getDelim().length; i++) {
 72  0
                     is.read();
 73  
                 }
 74  
             }
 75  156
         }
 76  20
     }
 77  
 
 78  
     /**
 79  
      * {@inheritDoc}
 80  
      */
 81  
     public final synchronized void writeTo(OutputStream os) throws IOException {
 82  57
         for (Iterator<Entity> it = getChildren().iterator(); it.hasNext();) {
 83  192
             Entity e = it.next();
 84  192
             if (shouldSuppress(e)) {
 85  14
                 continue;
 86  
             }
 87  178
             e.writeTo(os);
 88  178
             if (it.hasNext() || isDelimAfter()) {
 89  172
                 os.write(getDelim());
 90  
             }
 91  178
         }
 92  56
     }
 93  
 
 94  
     /**
 95  
      * {@inheritDoc}
 96  
      */
 97  
     public final synchronized byte[] getValue() {
 98  48
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 99  
         try {
 100  48
             writeTo(baos);
 101  0
         } catch (IOException e) {
 102  0
             throw new RuntimeException(e);
 103  47
         }
 104  47
         return baos.toByteArray();
 105  
     }
 106  
 
 107  
     /**
 108  
      * {@inheritDoc}
 109  
      */
 110  
     public final synchronized void setValue(byte[] b) {
 111  
         try {
 112  5
             readFrom(new ByteArrayInputStream(b));
 113  0
         } catch (IOException e) {
 114  0
             throw new RuntimeException(e);
 115  5
         }
 116  5
     }
 117  
 
 118  
     /**
 119  
      * {@inheritDoc}
 120  
      */
 121  
     public final void setValue(byte[] b, int offset, int length) {
 122  
         try {
 123  0
             readFrom(new ByteArrayInputStream(b, offset, length));
 124  0
         } catch (IOException e) {
 125  0
             throw new RuntimeException(e);
 126  0
         }
 127  0
     }
 128  
 
 129  
     /**
 130  
      * Learn whether a trailing delimiter will be added when a delimiter is being used.
 131  
      * @return boolean.
 132  
      */
 133  
     public boolean isDelimAfter() {
 134  309
         return delimAfter;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Set whether to add a trailing delimiter when a delimiter is being used.
 139  
      * Default is <code>true</code>.
 140  
      * @param delimAfter The boolean delimAfter to set.
 141  
      */
 142  
     public void setDelimAfter(boolean delimAfter) {
 143  4
         this.delimAfter = delimAfter;
 144  4
     }
 145  
 
 146  
     /**
 147  
      * Get the delimiter to use between children.
 148  
      * @return byte[].
 149  
      */
 150  
     public byte[] getDelim() {
 151  1999
         return delim;
 152  
     }
 153  
 
 154  
     /**
 155  
      * Set the delimiter to use between children.
 156  
      * @param delim The byte[] delimiter to set.
 157  
      */
 158  
     public synchronized void setDelim(byte[] delim) {
 159  10
         this.delim = delim == null ? DEFAULT_DELIMITER : delim;
 160  10
     }
 161  
 
 162  
     /**
 163  
      * Get the boolean suppressEmptyChildren.
 164  
      * @return boolean
 165  
      */
 166  
     public boolean isSuppressEmptyChildren() {
 167  2002
         return suppressEmptyChildren;
 168  
     }
 169  
 
 170  
     /**
 171  
      * Set the boolean suppressEmptyChildren.
 172  
      * @param suppressEmptyChildren boolean
 173  
      */
 174  
     public void setSuppressEmptyChildren(boolean suppressEmptyChildren) {
 175  2
         this.suppressEmptyChildren = suppressEmptyChildren;
 176  2
     }
 177  
 
 178  
     /**
 179  
      * {@inheritDoc}
 180  
      */
 181  
     @Override
 182  
     public EntityCollectionSupport clone() {
 183  115
         return (EntityCollectionSupport) super.clone();
 184  
     }
 185  
 
 186  
     /**
 187  
      * Learn whether the specified child should be suppressed.
 188  
      * @param child to check
 189  
      * @return boolean
 190  
      */
 191  
     protected boolean shouldSuppress(Entity child) {
 192  1994
         return isSuppressEmptyChildren() && child.length() == 0;
 193  
     }
 194  
 
 195  
 }