true
value
36 | */
37 | public static final String TRUE_VALUE = "T";
38 |
39 | /**
40 | * The default false
value
41 | */
42 | public static final String FALSE_VALUE = "F";
43 |
44 | /**
45 | * The string to map a boolean true value to.
46 | * @return contains the string representation of a true
value
47 | */
48 | String trueValue() default TRUE_VALUE;
49 |
50 | /**
51 | * The string to map a boolean false value to.
52 | * @return contains the string representation of a false
value
53 | */
54 | String falseValue() default FALSE_VALUE;
55 | }
56 |
--------------------------------------------------------------------------------
/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/FixedFormatDecimal.java:
--------------------------------------------------------------------------------
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 java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 | import java.math.BigDecimal;
23 |
24 | /**
25 | * @author Jacob von Eyben - http://www.ancientprogramming.com
26 | * @since 1.0.0
27 | */
28 |
29 | @Retention(RetentionPolicy.RUNTIME)
30 | @Target({ElementType.METHOD, ElementType.FIELD})
31 | public @interface FixedFormatDecimal {
32 |
33 | public static final int DECIMALS = 2;
34 | public static final boolean USE_DECIMAL_DELIMITER = false;
35 | public static final char DECIMAL_DELIMITER = '.';
36 | public static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;
37 |
38 | int decimals() default DECIMALS;
39 |
40 | boolean useDecimalDelimiter() default USE_DECIMAL_DELIMITER;
41 |
42 | char decimalDelimiter() default DECIMAL_DELIMITER;
43 |
44 | int roundingMode() default ROUNDING_MODE;
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/FixedFormatNumber.java:
--------------------------------------------------------------------------------
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 java.lang.annotation.Retention;
19 | import java.lang.annotation.RetentionPolicy;
20 | import java.lang.annotation.Target;
21 | import java.lang.annotation.ElementType;
22 |
23 | /**
24 | * @author Jacob von Eyben - http://www.ancientprogramming.com
25 | * @since 1.1.0
26 | */
27 |
28 | @Retention(RetentionPolicy.RUNTIME)
29 | @Target({ElementType.METHOD, ElementType.FIELD})
30 | public @interface FixedFormatNumber {
31 |
32 | //todo: this gives exception
33 | //annotation com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber is missing value
only contained adding chars.
39 | */
40 | String getRemovePadding(String value, FormatInstructions instructions) {
41 | return instructions.getAlignment().remove(value, instructions.getPaddingChar());
42 | }
43 |
44 | public String format(T value, FormatInstructions instructions) {
45 | return instructions.getAlignment().apply(asString(value, instructions), instructions.getLength(), instructions.getPaddingChar());
46 | }
47 |
48 | public abstract T asObject(String string, FormatInstructions instructions);
49 |
50 | public abstract String asString(T obj, FormatInstructions instructions);
51 | }
52 |
--------------------------------------------------------------------------------
/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FixedFormatManager.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FixedFormatManager.java
--------------------------------------------------------------------------------
/fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FixedFormatUtil.java:
--------------------------------------------------------------------------------
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.format;
17 |
18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
19 | import org.apache.commons.logging.Log;
20 | import org.apache.commons.logging.LogFactory;
21 |
22 | import static java.lang.String.format;
23 |
24 | /**
25 | * Utility class used when loading and exporting to and from fixedformat data.
26 | *
27 | * @author Jacob von Eyben - http://www.ancientprogramming.com
28 | * @since 1.0.0
29 | */
30 | public class FixedFormatUtil {
31 |
32 | private static final Log LOG = LogFactory.getLog(FixedFormatUtil.class);
33 |
34 | /**
35 | * Fetch data from the record string according to the {@link FormatInstructions} and {@link FormatContext}
36 | * @param record the string to fetch from
37 | * @param instructions the fixed
38 | * @param context the context to fetch data in
39 | * @return the String data fetched from the record. Can be null
if the record was shorter than the context expected
40 | */
41 | public static String fetchData(String record, FormatInstructions instructions, FormatContext context) {
42 | String result;
43 | int offset = context.getOffset() - 1;
44 | int length = instructions.getLength();
45 | if (record.length() >= offset + length) {
46 | result = record.substring(offset, offset + length);
47 | } else if (record.length() > offset) {
48 | //the field does contain data, but is not as long as the instructions tells.
49 | result = record.substring(offset, record.length());
50 | if (LOG.isDebugEnabled()) {
51 | LOG.info(format("The record field was not as long as expected by the instructions. Expected field to be %s long but it was %s.", length, record.length()));
52 | }
53 | } else {
54 | result = null;
55 | LOG.info(format("Could not fetch data from record as the recordlength[%s] was shorter than or equal to the requested offset[%s] of the request data. Returning null", record.length(), offset));
56 | }
57 | if (LOG.isDebugEnabled()) {
58 | LOG.debug(format("fetched '%s' from record", result));
59 | }
60 | return result;
61 | }
62 |
63 | public static @Field(offset = 1, length = 20, formatter = {@link DateFormatter}.class)