EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.item.file.transform]

COVERAGE SUMMARY FOR SOURCE FILE [FixedLengthLineAggregator.java]

nameclass, %method, %block, %line, %
FixedLengthLineAggregator.java100% (1/1)100% (6/6)100% (234/234)100% (40/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FixedLengthLineAggregator100% (1/1)100% (6/6)100% (234/234)100% (40/40)
FixedLengthLineAggregator (): void 100% (1/1)100% (9/9)100% (3/3)
aggregate (FieldSet): String 100% (1/1)100% (179/179)100% (22/22)
findLastColumn (Range []): int 100% (1/1)100% (27/27)100% (7/7)
setAlignment (Alignment): void 100% (1/1)100% (4/4)100% (2/2)
setColumns (Range []): void 100% (1/1)100% (11/11)100% (4/4)
setPadding (char): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17package org.springframework.batch.item.file.transform;
18 
19import java.util.Arrays;
20 
21import org.springframework.batch.item.file.mapping.FieldSet;
22import org.springframework.util.Assert;
23 
24/**
25 * {@link LineAggregator} implementation which produces line by aggregating provided
26 * strings into columns with fixed length. Columns are specified by array of
27 * ranges ({@link #setColumns(Range[])}.</br>
28 * 
29 * @author tomas.slanina
30 * @author peter.zozom
31 * @author Dave Syer
32 */
33public class FixedLengthLineAggregator implements LineAggregator {
34 
35        private Range[] ranges;
36 
37        private int lastColumn;
38 
39        private Alignment align = Alignment.LEFT;
40 
41        private char padding = ' ';
42 
43        /**
44         * Set column ranges. Used in conjunction with the
45         * {@link RangeArrayPropertyEditor} this property can be set in the form of
46         * a String describing the range boundaries, e.g. "1,4,7" or "1-3,4-6,7" or
47         * "1-2,4-5,7-10".
48         * 
49         * @param columns array of Range objects which specify column start and end
50         * position
51         */
52        public void setColumns(Range[] columns) {
53                Assert.notNull(columns);
54                lastColumn = findLastColumn(columns);
55                this.ranges = columns;
56        }
57 
58        /**
59         * Aggregate provided strings into single line using specified column
60         * ranges.
61         * 
62         * @param fieldSet arrays of strings representing data to be aggregated
63         * @return aggregated strings
64         */
65        public String aggregate(FieldSet fieldSet) {
66 
67                Assert.notNull(fieldSet);
68                Assert.notNull(ranges);
69 
70                String[] args = fieldSet.getValues();
71                Assert.isTrue(args.length <= ranges.length, "Number of arguments must match number of fields in a record");
72 
73                // calculate line length
74                int lineLength = ranges[lastColumn].hasMaxValue() ? ranges[lastColumn].getMax() : ranges[lastColumn].getMin()
75                                + args[lastColumn].length() - 1;
76 
77                // create stringBuffer with length of line filled with padding
78                // characters
79                char[] emptyLine = new char[lineLength];
80                Arrays.fill(emptyLine, padding);
81 
82                StringBuffer stringBuffer = new StringBuffer(lineLength);
83                stringBuffer.append(emptyLine);
84 
85                // aggregate all strings
86                for (int i = 0; i < args.length; i++) {
87 
88                        // offset where text will be inserted
89                        int start = ranges[i].getMin() - 1;
90 
91                        // calculate column length
92                        int columnLength;
93                        if ((i == lastColumn) && (!ranges[lastColumn].hasMaxValue())) {
94                                columnLength = args[lastColumn].length();
95                        }
96                        else {
97                                columnLength = ranges[i].getMax() - ranges[i].getMin() + 1;
98                        }
99 
100                        String textToInsert = (args[i] == null) ? "" : args[i];
101 
102                        Assert.isTrue(columnLength >= textToInsert.length(), "Supplied text: " + textToInsert
103                                        + " is longer than defined length: " + columnLength);
104 
105                        if (align == Alignment.RIGHT) {
106                                start += (columnLength - textToInsert.length());
107                        }
108                        else if (align == Alignment.CENTER) {
109                                start += ((columnLength - textToInsert.length()) / 2);
110                        }
111 
112                        stringBuffer.replace(start, start + textToInsert.length(), textToInsert);
113                }
114 
115                return stringBuffer.toString();
116        }
117 
118        /**
119         * Recognized alignments are <code>CENTER, RIGHT, LEFT</code>. An
120         * IllegalArgumentException is thrown in case the argument does not match
121         * any of the recognized values.
122         * 
123         * @param alignment the alignment to be used
124         */
125        public void setAlignment(Alignment alignment) {
126                this.align = alignment;
127        }
128 
129        /**
130         * Setter for padding (default is space).
131         * 
132         * @param padding the padding character
133         */
134        public void setPadding(char padding) {
135                this.padding = padding;
136        }
137 
138        /*
139         * Find last column. Columns are not sorted. Returns index of last column
140         * (column with highest offset).
141         */
142        private int findLastColumn(Range[] columns) {
143 
144                int lastOffset = 1;
145                int lastIndex = 0;
146 
147                for (int i = 0; i < columns.length; i++) {
148                        if (columns[i].getMin() > lastOffset) {
149                                lastOffset = columns[i].getMin();
150                                lastIndex = i;
151                        }
152                }
153 
154                return lastIndex;
155        }
156}

[all classes][org.springframework.batch.item.file.transform]
EMMA 2.0.5312 (C) Vladimir Roubtsov