1 /*
2 * Copyright 2004 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 package com.ancientprogramming.fixedformat4j.annotation;
17
18 import org.apache.commons.lang.StringUtils;
19
20 /**
21 * Capable of pad or chop data in a given direction
22 *
23 * @author Jacob von Eyben- http://www.ancientprogramming.com
24 * @since 1.0.0
25 */
26 public enum Align {
27
28 /**
29 * Pad or chop data to the left, so the text is aligned to the right
30 */
31 RIGHT {
32 public String apply(String data, int length, char paddingChar) {
33 String result;
34 if (data == null) {
35 data = "";
36 }
37 int dataLength = data.length();
38 if (dataLength > length) {
39 result = StringUtils.substring(data, dataLength - length, dataLength);
40 } else {
41 result = StringUtils.leftPad(data, length, paddingChar);
42 }
43 return result;
44 }
45 public String remove(String data, char paddingChar) {
46 String result = data;
47 if (data == null) {
48 result = "";
49 }
50 while (result.startsWith("" + paddingChar)) {
51 result = result.substring(1, result.length());
52 }
53 return result;
54 }},
55
56
57 /**
58 * Pad or chop data to the right, so the text is aligned to the left
59 */
60 LEFT {
61 public String apply(String data, int length, char paddingChar) {
62 String result;
63 if (data == null) {
64 data = "";
65 }
66 int dataLength = data.length();
67 if (dataLength > length) {
68 result = StringUtils.substring(data, 0, length);
69 } else {
70 result = StringUtils.rightPad(data, length, paddingChar);
71 }
72 return result;
73 }
74
75 public String remove(String data, char paddingChar) {
76 String result = data;
77 if (data == null) {
78 result = "";
79 }
80 while (result.endsWith("" + paddingChar)) {
81 result = result.substring(0, result.length()-1);
82 }
83 return result;
84 }};
85
86 /**
87 * Pads the data in the length specified with the given padding char.
88 * No padding will be applied if the length of the data is longer than the given length.
89 *
90 * @param data the data to pad.
91 * @param length the minimum length after the padding is applied.
92 * @param paddingChar the char the data is padded with.
93 * @return the data after padding is applied.
94 */
95 public abstract String apply(String data, int length, char paddingChar);
96
97 /**
98 * Remove the padding chars from the data.
99 *
100 * @param data the data including padding chars
101 * @param paddingChar the padding char to remove
102 * @return the data after padding is removed.
103 */
104 public abstract String remove(String data, char paddingChar);
105 }