Coverage Report - org.apache.commons.flatfile.PadJustifyFieldSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
PadJustifyFieldSupport
100%
10/10
100%
2/2
0
PadJustifyFieldSupport$1
N/A
N/A
0
PadJustifyFieldSupport$Justify
100%
4/4
N/A
0
PadJustifyFieldSupport$Justify$1
100%
4/4
100%
2/2
0
PadJustifyFieldSupport$Justify$2
100%
6/6
100%
4/4
0
PadJustifyFieldSupport$Justify$3
92%
12/13
75%
3/4
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.InputStream;
 21  
 import java.util.ArrayList;
 22  
 
 23  
 import org.apache.commons.flatfile.util.ConcatenatedInputStream;
 24  
 import org.apache.commons.flatfile.util.RepeatingInputStream;
 25  
 
 26  
 /**
 27  
  * Support for fields with pad/justify.
 28  
  * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 29  
  */
 30  128
 public abstract class PadJustifyFieldSupport extends FieldSupport {
 31  
     /** Serialization version */
 32  
     private static final long serialVersionUID = -4953059253157670418L;
 33  
 
 34  
     /**
 35  
      * Justify enumerated type.
 36  
      */
 37  8
     public static enum Justify implements FieldOption {
 38  
         /** Left-justify */
 39  1
         LEFT {
 40  
             /**
 41  
              * {@inheritDoc}
 42  
              */
 43  1
             protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) {
 44  104
                 InputStream is = new ByteArrayInputStream(src);
 45  104
                 int dlen = dest.getPadJustifyLength();
 46  104
                 return dlen <= src.length ? is : new ConcatenatedInputStream(is,
 47  
                         RepeatingInputStream.getInstance(dest.getPad(), dlen - src.length));
 48  
             }
 49  
         },
 50  
 
 51  
         /** Right-justify */
 52  1
         RIGHT {
 53  
             /**
 54  
              * {@inheritDoc}
 55  
              */
 56  1
             protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) {
 57  12
                 int dlen = dest.getPadJustifyLength();
 58  12
                 if (dlen < src.length) {
 59  3
                     return new ByteArrayInputStream(src, src.length - dlen, dlen);
 60  
                 }
 61  9
                 InputStream is = new ByteArrayInputStream(src);
 62  9
                 return dlen == src.length ? is : new ConcatenatedInputStream(RepeatingInputStream
 63  
                         .getInstance(dest.getPad(), dlen - src.length), is);
 64  
             }
 65  
         },
 66  
 
 67  
         /** Center */
 68  1
         CENTER {
 69  
             /**
 70  
              * {@inheritDoc}
 71  
              */
 72  1
             protected InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest) {
 73  5
                 int dlen = dest.getPadJustifyLength();
 74  
                 // it would be odd and therefore hopefully rare to center a
 75  
                 // field that allowed overflow,
 76  
                 // but we'll provide for it:
 77  5
                 if (dlen < src.length) {
 78  2
                     return new ByteArrayInputStream(src, (src.length - dlen) / 2, dlen);
 79  
                 }
 80  3
                 InputStream is = new ByteArrayInputStream(src);
 81  3
                 if (dlen == src.length) {
 82  0
                     return is;
 83  
                 }
 84  3
                 ArrayList<InputStream> l = new ArrayList<InputStream>(3);
 85  3
                 byte pad = dest.getPad();
 86  3
                 l.add(RepeatingInputStream.getInstance(pad, (dlen - src.length) / 2));
 87  3
                 l.add(is);
 88  3
                 l.add(RepeatingInputStream.getInstance(pad));
 89  3
                 return new ConcatenatedInputStream(l);
 90  
             }
 91  
         };
 92  
 
 93  
         /**
 94  
          * Get an InputStream for the specified parameters.
 95  
          * @param src byte[] value
 96  
          * @param dest PadJustifyFieldSupport
 97  
          * @return InputStream
 98  
          */
 99  
         protected abstract InputStream getInputStream(byte[] src, PadJustifyFieldSupport dest);
 100  
     }
 101  
 
 102  128
     private byte pad = 0x20;
 103  
     private Justify justify;
 104  
 
 105  
     /**
 106  
      * Get the justify.
 107  
      * @return Justify
 108  
      */
 109  
     public Justify getJustify() {
 110  163
         return justify == null ? Justify.LEFT : justify;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Set the justify.
 115  
      * @param justify Justify
 116  
      */
 117  
     public void setJustify(Justify justify) {
 118  20
         this.justify = justify;
 119  20
     }
 120  
 
 121  
     /**
 122  
      * Get the pad.
 123  
      * @return byte
 124  
      */
 125  
     public byte getPad() {
 126  53
         return pad;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Set the pad.
 131  
      * @param pad byte
 132  
      */
 133  
     public void setPad(byte pad) {
 134  16
         this.pad = pad;
 135  16
     }
 136  
 
 137  
     /**
 138  
      * Implement pad/justify.
 139  
      * @param b value set
 140  
      */
 141  
     public void setValue(byte[] b) {
 142  121
         dieOnExceptionRead(getJustify().getInputStream(b, this));
 143  121
     }
 144  
 
 145  
     /**
 146  
      * Provide the desired target length for padding/justification actions.
 147  
      * @return int
 148  
      */
 149  
     protected abstract int getPadJustifyLength();
 150  
 }