├── .gitignore ├── .travis.yml ├── README.md ├── fixedformat4j ├── LICENSE.txt ├── pom.xml └── src │ ├── changes │ └── changes.xml │ ├── main │ └── java │ │ └── com │ │ └── ancientprogramming │ │ └── fixedformat4j │ │ ├── annotation │ │ ├── Align.java │ │ ├── Field.java │ │ ├── Fields.java │ │ ├── FixedFormatBoolean.java │ │ ├── FixedFormatDecimal.java │ │ ├── FixedFormatNumber.java │ │ ├── FixedFormatPattern.java │ │ ├── Record.java │ │ └── Sign.java │ │ ├── exception │ │ └── FixedFormatException.java │ │ └── format │ │ ├── AbstractFixedFormatter.java │ │ ├── FixedFormatManager.java │ │ ├── FixedFormatUtil.java │ │ ├── FixedFormatter.java │ │ ├── FormatContext.java │ │ ├── FormatInstructions.java │ │ ├── ParseException.java │ │ ├── data │ │ ├── FixedFormatBooleanData.java │ │ ├── FixedFormatDecimalData.java │ │ ├── FixedFormatNumberData.java │ │ └── FixedFormatPatternData.java │ │ └── impl │ │ ├── AbstractDecimalFormatter.java │ │ ├── AbstractNumberFormatter.java │ │ ├── BigDecimalFormatter.java │ │ ├── BooleanFormatter.java │ │ ├── ByTypeFormatter.java │ │ ├── CharacterFormatter.java │ │ ├── DateFormatter.java │ │ ├── DoubleFormatter.java │ │ ├── FixedFormatManagerImpl.java │ │ ├── FloatFormatter.java │ │ ├── IntegerFormatter.java │ │ ├── LongFormatter.java │ │ ├── ShortFormatter.java │ │ └── StringFormatter.java │ ├── site │ ├── apt │ │ ├── fiveMinutesTutorial.apt │ │ ├── fixedformat4jOverview.apt │ │ ├── get-it.apt │ │ ├── index.apt │ │ └── usage │ │ │ ├── index.apt │ │ │ └── nested-records.apt │ ├── fixedformat4j-template.vm │ ├── fml │ │ └── faq.fml │ ├── resources │ │ ├── css │ │ │ ├── maven-base.css │ │ │ └── print.css │ │ └── images │ │ │ ├── collapsed.gif │ │ │ ├── expanded.gif │ │ │ └── logos │ │ │ ├── build-by-maven-black.png │ │ │ ├── build-by-maven-white.png │ │ │ └── maven-feather.png │ ├── site.xml │ └── xdoc │ │ └── usage │ │ └── annotations.xml │ └── test │ ├── java │ └── com │ │ └── ancientprogramming │ │ └── fixedformat4j │ │ ├── annotation │ │ ├── TestAlign.java │ │ └── TestSign.java │ │ ├── format │ │ ├── TestFixedFormatUtil.java │ │ └── impl │ │ │ ├── MultibleFieldsRecord.java │ │ │ ├── MyOtherRecord.java │ │ │ ├── MyRecord.java │ │ │ ├── NoDefaultConstructorClass.java │ │ │ ├── TestBigDecimalFormatter.java │ │ │ ├── TestBooleanFormatter.java │ │ │ ├── TestCharacterFormatter.java │ │ │ ├── TestDateFormatter.java │ │ │ ├── TestDoubleFormatter.java │ │ │ ├── TestFixedFormatManagerImpl.java │ │ │ ├── TestFloatFormatter.java │ │ │ ├── TestIntegerFormatter.java │ │ │ ├── TestLongFormatter.java │ │ │ ├── TestShortFormatter.java │ │ │ └── TestStringFormatter.java │ │ └── issues │ │ ├── TestIssue10.java │ │ ├── TestIssue7.java │ │ └── TestIssue9.java │ └── resources │ └── log4j.xml ├── pom.xml ├── samples ├── pom.xml └── src │ └── main │ └── java │ ├── com │ └── ancientprogramming │ │ └── fixedformat4j │ │ └── samples │ │ ├── basic │ │ ├── BasicRecord.java │ │ └── BasicUsage.java │ │ └── usage │ │ ├── BasicUsageManager.java │ │ ├── BasicUsageRecord.java │ │ ├── NestedRecord.java │ │ └── NestedRecordUsage.java │ └── log4j.xml └── todo.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target 3 | .idea 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was originally hosted at https://code.google.com/p/fixedformat4j/ 2 | 3 | 4 | 5 | After google decided to close down code.google.com the repository was moved. 6 | 7 | Go to the offical Fixedformat4j page for documentation: http://jeyben.github.io/fixedformat4j 8 | -------------------------------------------------------------------------------- /fixedformat4j/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | Fixed Format for Java 6 | com.ancientprogramming.fixedformat4j 7 | 8 | 1.4.0-SNAPSHOT 9 | fixedformat4j 10 | jar 11 | 12 | 2008 13 | 14 | ancientprogramming.com 15 | http://www.ancientprogramming.com 16 | 17 | 18 | 21 | 22 | http://fixedformat4j.ancientprogramming.com 23 | 24 | 25 | 2.0.7 26 | 27 | 28 | 29 | 30 | The Apache Software License, Version 2.0 31 | http://www.apache.org/licenses/LICENSE-2.0.txt 32 | repo 33 | 34 | 35 | 36 | 37 | Google Code 38 | http://code.google.com/p/fixedformat4j/issues/list 39 | 40 | 41 | 42 | scm:svn:http://fixedformat4j.googlecode.com/svn/trunk/fixedformat4j 43 | scm:svn:https://fixedformat4j.googlecode.com/svn/trunk/fixedformat4j 44 | http://code.google.com/p/fixedformat4j/source/browse/trunk/fixedformat4j 45 | 46 | 47 | 48 | 49 | Jacob von Eyben 50 | jeyben 51 | jacobvoneyben@gmail.com 52 | 53 | 54 | http://www.ancientprogramming.com 55 | 2 56 | 57 | Developer 58 | 59 | 60 | 61 | 62 | 63 | 64 | ancientprogramming 65 | scp://ancientprogramming.com/home/ancientp/www/_fixedformat4j 66 | 67 | 68 | ancientprogramming 69 | scp://ancientprogramming.com/home/ancientp/www/_maven/repository 70 | 71 | 72 | 73 | 74 | 75 | commons-lang 76 | commons-lang 77 | 2.3 78 | 79 | 80 | 81 | commons-logging 82 | commons-logging 83 | 1.1.1 84 | 85 | 86 | 87 | junit 88 | junit 89 | 4.4 90 | test 91 | 92 | 93 | 94 | log4j 95 | log4j 96 | 1.2.14 97 | test 98 | 99 | 100 | 101 | 102 | 103 | Fixedformat4j User List 104 | fixedformat4j-user-list@googlegroups.com 105 | fixedformat4j-user-list+unsubscribe@googlegroups.com 106 | http://groups.google.com/group/fixedformat4j-user-list/subscribe 107 | http://groups.google.com/group/fixedformat4j-user-list/topics?gvc=2 108 | 109 | 110 | 111 | 112 | install 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-compiler-plugin 117 | 118 | 1.8 119 | 1.8 120 | UTF-8 121 | true 122 | 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-site-plugin 127 | 128 | ${basedir}/src/site 129 | src/site/fixedformat4j-template.vm 130 | 131 | 132 | 133 | com.ancientprogramming.maven.doxia 134 | doxia-module-syntaxhighlighter 135 | 1.0-alpha-11 136 | 137 | 138 | 139 | 140 | org.apache.maven.plugins 141 | maven-release-plugin 142 | 143 | https://fixedformat4j.googlecode.com/svn/tags/fixedformat4j 144 | 145 | 146 | 147 | org.apache.maven.plugins 148 | maven-assembly-plugin 149 | 150 | 151 | bin 152 | src 153 | 154 | 155 | 156 | 157 | org.apache.maven.plugins 158 | maven-source-plugin 159 | 160 | 161 | package 162 | 163 | jar 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | org.apache.maven.plugins 175 | maven-changes-plugin 176 | 177 | 178 | 179 | changes-report 180 | 181 | 182 | 183 | 184 | %URL%/detail?id=%ISSUE% 185 | 186 | 187 | 188 | org.apache.maven.plugins 189 | maven-surefire-report-plugin 190 | 191 | 192 | org.apache.maven.plugins 193 | maven-javadoc-plugin 194 | 195 | 196 | org.apache.maven.plugins 197 | maven-jxr-plugin 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | -------------------------------------------------------------------------------- /fixedformat4j/src/changes/changes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Fixed Format for Java 4 | Jacob von Eyben 5 | 6 | 7 | 8 | 9 | API change on generic value T in AbstractNumberFormatter to be restricted on java.lang.Number unnecessary. 10 | 11 | 12 | Revert fix for issue #22 on paddingChar honouring 13 | 14 | 15 | 16 | 17 | @FixedFormatDecimal with more than 3 decimals truncates decimals 18 | 19 | 20 | AbstractDecimalFormatter should support explicit rounding 21 | 22 | 23 | AbstractDecimalFormatter DecimalFormat usage is not thread-safe 24 | 25 | 26 | AbstractDecimalFormatter hard codes '0' as padding char - should honor annotation paddingChar setting 27 | 28 | 29 | 30 | 31 | Sign.APPEND.apply fails to detect minus symbol correctly 32 | 33 | 34 | 35 | 36 | NullPointerException in export. 37 | 38 | 39 | 40 | 41 | Support skipping unparseable fields within records. 42 | 43 | 44 | Added a Short/short build-in formatter and added this to the ByTypeFormatter. 45 | 46 | 47 | Support for @Record annotated classes can contain other @Record annotated classes and still get proberly imported and exported. 48 | Especially usefull when you like to store logically grouped domain objects. i.e. creditcard informations inside a record 49 | containing other information as well. 50 | 51 | 52 | The ParseException now contains getter methods making it possible to retreive the reasons why the parsing went wrong. 53 | That makes it easy to build up a custom error message for the user in a localized way instead of just relying on 54 | the message written in english. 55 | 56 | 57 | 58 | 59 | 60 | Fixed issue where data wasn't loaded if the text length was shorter just a bit shorter than the last field in a record. 61 | 62 | 63 | 64 | 65 | 66 | Added support for processing annotated static nested classes and inner classes. 67 | 68 | 69 | Fixed bug when the setter/getter data type is an interface or abstract class, the format manager failed at runtime determining the datatype. 70 | 71 | 72 | Added support for primitive datatypes like int, boolean, float etc. 73 | 74 | 75 | 76 | 77 | 78 | Improved error reporting when fail to parse a string. 79 | All details about how data was suppose to be parse will be included in the error message. 80 | The class and method name of the instance trying to create will be included as well. 81 | 82 | 83 | Added the ability to annotate methods starting with 'is' in addition to 'get'. 84 | 85 | 86 | 87 | 88 | 89 | Added the ability to not sign numbers. And made it default 90 | 91 | 92 | Generified the FixedFormatter interface. Reguires custom formatters to be updated. 93 | 94 | 95 | Fixed bug when parsing numbers from string having prependend signs. 96 | 97 | 98 | Fixed various smaller bugs primarily in predefined formatters. 99 | 100 | 101 | 102 | 103 | 104 | Introduced ability to parse and format signed numbers. 105 | 106 | 107 | 108 | 109 | 110 | Initial release of fixedformat4j. 111 | 112 | 113 | 114 | 115 | 116 | 133 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/Align.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 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 | } 106 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/Field.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 com.ancientprogramming.fixedformat4j.format.FixedFormatter; 19 | import com.ancientprogramming.fixedformat4j.format.impl.ByTypeFormatter; 20 | 21 | import java.lang.annotation.Target; 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | 26 | /** 27 | * This annotation descibes how a setter/getter pairs should be formatted by the fixedFormatManager. 28 | * 29 | * @author Jacob von Eyben - http://www.ancientprogramming.com 30 | * @since 1.0.0 31 | */ 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target({ElementType.METHOD}) 34 | public @interface Field { 35 | 36 | /** 37 | * A one based offset to insert data at in a record. 38 | * @return the offset as an int 39 | */ 40 | int offset(); 41 | 42 | /** 43 | * The length of the formatted field 44 | * @return the length as an int 45 | */ 46 | int length(); 47 | 48 | /** 49 | * @return The direction of the padding. Defaults to {@link Align#RIGHT}. 50 | */ 51 | Align align() default Align.LEFT; 52 | 53 | /** 54 | * The character to pad with if the length is longer than the formatted data 55 | * @return the padding character 56 | */ 57 | char paddingChar() default ' '; 58 | 59 | Class formatter() default ByTypeFormatter.class; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/Fields.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 | 23 | /** 24 | * Wrapper for more than one field. 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.0.0 27 | */ 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target({ElementType.METHOD}) 30 | public @interface Fields { 31 | 32 | /** 33 | * Defines a list of field annotations. 34 | * Usefull a field is to be mapped different palces in the text 35 | * @return a list of {@link Field} annotations 36 | */ 37 | public abstract Field[] value(); 38 | } 39 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/FixedFormatBoolean.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 | * Define representations for {@link Boolean#TRUE} and {@link Boolean#FALSE} 25 | * 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.METHOD, ElementType.FIELD}) 32 | public @interface FixedFormatBoolean { 33 | 34 | /** 35 | * The default 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 34 | //public static final Sign DEFAULT_SIGN = Sign.NOSIGN; 35 | 36 | public static final char DEFAULT_POSITIVE_SIGN = '+'; 37 | public static final char DEFAULT_NEGATIVE_SIGN = '-'; 38 | 39 | 40 | Sign sign() default Sign.NOSIGN; 41 | 42 | char positiveSign() default DEFAULT_POSITIVE_SIGN; 43 | 44 | char negativeSign() default DEFAULT_NEGATIVE_SIGN; 45 | } 46 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/FixedFormatPattern.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 | * Used together with FixedFormatField annotations to provide a pattern for the data. 25 | * This annotation is required for {@link java.util.Date} datatype. 26 | * 27 | * @author Jacob von Eyben - http://www.ancientprogramming.com 28 | * @since 1.0.0 29 | */ 30 | 31 | @Retention(RetentionPolicy.RUNTIME) 32 | @Target({ElementType.METHOD, ElementType.FIELD}) 33 | public @interface FixedFormatPattern { 34 | 35 | public static final String DATE_PATTERN = "yyyyMMdd"; 36 | 37 | /** 38 | * The pattern used in formatting and parsing a fixed format field. 39 | * Date: yyyyMMdd 40 | * Other: ####-######.## 41 | * Currency: ??? 42 | * @return the pattern 43 | */ 44 | String value(); 45 | } 46 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/Record.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 | * Marks a class as a representation of a fixed format record 25 | * 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.TYPE}) 32 | public @interface Record { 33 | 34 | /** 35 | * The fixed length of the record. It means that the record will always be that long padded with {#paddingChar()}'s 36 | * @return the length of the record. -1 means no fixed length. 37 | */ 38 | int length() default -1; 39 | 40 | /** 41 | * The char to pad with. 42 | * @return the char to pad with if the record is set to a fixed length; 43 | */ 44 | char paddingChar() default ' '; 45 | } 46 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/annotation/Sign.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 com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | import org.apache.commons.lang.StringUtils; 20 | 21 | /** 22 | * Sign defines where to place a sign defining a positive or negative number. 23 | * Is to be used in formatters operating numbers. 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.1.0 27 | */ 28 | public enum Sign { 29 | 30 | /** 31 | * Doesn't do anything with signs. 32 | * This just delegate to the {@link Align} defined in {@link FormatInstructions}. 33 | */ 34 | NOSIGN { 35 | public String apply(String value, FormatInstructions instructions) { 36 | return instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar()); 37 | } 38 | 39 | public String remove(String value, FormatInstructions instructions) { 40 | String result = instructions.getAlignment().remove(value, instructions.getPaddingChar()); 41 | if (StringUtils.isEmpty(result)) { 42 | result = "0"; 43 | } 44 | return result; 45 | 46 | } 47 | }, 48 | 49 | /** 50 | * Prepend the sign to the string 51 | */ 52 | PREPEND { 53 | public String apply(String value, FormatInstructions instructions) { 54 | String sign = StringUtils.substring(value, 0, 1); 55 | if ("-".equals(sign)) { 56 | value = StringUtils.substring(value, 1); 57 | } else { 58 | sign = "+"; 59 | } 60 | String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar()); 61 | return sign + StringUtils.substring(result, 1); 62 | } 63 | 64 | public String remove(String value, FormatInstructions instructions) { 65 | String sign = StringUtils.substring(value, 0, 1); 66 | String valueWithoutSign = StringUtils.substring(value, 1); 67 | String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar()); 68 | if (removeSign(instructions, sign, result)) { 69 | sign = ""; 70 | } 71 | 72 | if (StringUtils.isEmpty(result)) { 73 | result = "0"; 74 | } 75 | return sign + result; 76 | } 77 | }, 78 | 79 | /** 80 | * Append the sign to the string 81 | */ 82 | APPEND { 83 | public String apply(String value, FormatInstructions instructions) { 84 | String sign = StringUtils.substring(value, 0, 1); 85 | if ("-".equals(sign)) { 86 | value = StringUtils.substring(value, 1); 87 | } else { 88 | sign = "+"; 89 | } 90 | String result = instructions.getAlignment().apply(value, instructions.getLength(), instructions.getPaddingChar()); 91 | return StringUtils.substring(result, 1) + sign; 92 | 93 | } 94 | public String remove(String value, FormatInstructions instructions) { 95 | String sign = StringUtils.substring(value, value.length()-1); 96 | String valueWithoutSign = StringUtils.substring(value, 0, value.length()-1); 97 | String result = instructions.getAlignment().remove(valueWithoutSign, instructions.getPaddingChar()); 98 | if (removeSign(instructions, sign, result)) { 99 | sign = ""; 100 | } 101 | if (StringUtils.isEmpty(result)) { 102 | result = "0"; 103 | } 104 | return sign + result; 105 | } 106 | }; 107 | 108 | /** 109 | *remove sign in three cases: 110 | * 1. positive sign 111 | * 2. the unsigned value is empty (can happen if paddingchar is 0 and the value is zero) 112 | * 3. the unsigned value is 0 (can happen if paddingchar isn't 0 and the value is zero) 113 | * @param instructions 114 | * @param sign 115 | * @param valueWithoutSign 116 | * @return 117 | */ 118 | private static boolean removeSign(FormatInstructions instructions, String sign, String valueWithoutSign) { 119 | return instructions.getFixedFormatNumberData().getPositiveSign().equals(sign.charAt(0)) || 120 | StringUtils.isEmpty(valueWithoutSign) || 121 | "0".equals(valueWithoutSign); 122 | } 123 | 124 | 125 | public abstract String apply(String value, FormatInstructions instructions); 126 | 127 | public abstract String remove(String value, FormatInstructions instructions); 128 | } 129 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/exception/FixedFormatException.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.exception; 17 | 18 | /** 19 | * Thrown when errors occur while loading or exporting data. 20 | * 21 | * @author Jacob von Eyben - http://www.ancientprogramming.com 22 | * @since 1.0.0 23 | */ 24 | public class FixedFormatException extends RuntimeException { 25 | 26 | public FixedFormatException(String s) { 27 | super(s); 28 | } 29 | 30 | public FixedFormatException(String s, Throwable throwable) { 31 | super(s, throwable); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/AbstractFixedFormatter.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 | /** 19 | * Handles default formatting and parsing based on FixedFormatAnnotation values. 20 | * 21 | * @author Jacob von Eyben - http://www.ancientprogramming.com 22 | * @since 1.0.0 23 | */ 24 | public abstract class AbstractFixedFormatter implements FixedFormatter { 25 | public T parse(String value, FormatInstructions instructions) { 26 | T result = null; 27 | if (value != null) { 28 | instructions.getFixedFormatNumberData(); 29 | result = asObject(getRemovePadding(value, instructions), instructions); 30 | } 31 | return result; 32 | } 33 | 34 | /** 35 | * Removes the padding characters defined in the instructions. 36 | * @param value the string to remove padding chars from 37 | * @param instructions the instructions containing the padding char 38 | * @return the remaining string value after padding chars was removed. The empty string if he 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 FixedFormatter getFixedFormatterInstance(Class> formatterClass, FormatContext context) { 64 | FixedFormatter formatter = getFixedFormatterInstance(formatterClass, context.getClass(), context); 65 | if (formatter == null) { 66 | formatter = getFixedFormatterInstance(formatterClass, null, null); 67 | } 68 | if (formatter == null) { 69 | throw new FixedFormatException("could not create instance of [" + formatterClass.getName() + "] because the class has no default constructor and no constructor with " + FormatContext.class.getName() + " as argument."); 70 | } 71 | return formatter; 72 | } 73 | 74 | public static FixedFormatter getFixedFormatterInstance(Class> formatterClass, Class paramType, FormatContext paramValue) { 75 | FixedFormatter result; 76 | if (paramType != null && paramValue != null) { 77 | try { 78 | result = formatterClass.getConstructor(paramType).newInstance(paramValue); 79 | } catch (NoSuchMethodException e) { 80 | result = null; 81 | } catch (Exception e) { 82 | throw new FixedFormatException("Could not create instance with one argument constructor", e); 83 | } 84 | } else { 85 | try { 86 | result = formatterClass.getConstructor().newInstance(); 87 | } catch (NoSuchMethodException e) { 88 | result = null; 89 | } catch (Exception e) { 90 | throw new FixedFormatException("Could not create instance with no arg constructor", e); 91 | } 92 | } 93 | return result; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FixedFormatter.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.annotation.Field; 19 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 20 | import com.ancientprogramming.fixedformat4j.format.impl.DateFormatter; 21 | 22 | /** 23 | * Formatter capable of transforming data to and from a string used in text records. 24 | *

25 | * A concrete class is used together with the @{@link Field} annotation. 26 | *

27 | * Example:

@Field(offset = 1, length = 20, formatter = {@link DateFormatter}.class)

28 | * 29 | * @author Jacob von Eyben - http://www.ancientprogramming.com 30 | * @since 1.0.0 31 | */ 32 | public interface FixedFormatter { 33 | 34 | /** 35 | * Parses the given String value according to the format instruction. 36 | * How the data is parsed and to what object is defined by the implementor. 37 | * @param value the data to parse. 38 | * @param instructions contains the instructions telling how to parse the value 39 | * @return An instance of T after the value was parsed according to the instructions 40 | * @throws FixedFormatException if the value could not be parsed according to the instructions 41 | */ 42 | T parse(String value, FormatInstructions instructions) throws FixedFormatException; 43 | 44 | 45 | /** 46 | * Formats an instance of T according to the instructions 47 | * @param value the object to format 48 | * @param instructions contains the instructions telling how to format the value 49 | * @return a string representation of the value object after it was formatted according to the instructions 50 | * @throws FixedFormatException if the value could not be formatted according to the instructions 51 | */ 52 | String format(T value, FormatInstructions instructions) throws FixedFormatException; 53 | } 54 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FormatContext.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 | /** 19 | * Contains context for loading and exporting fixedformat data. 20 | * The context describes what kind of formatter to use, what datatype to convert and what offset to fetch data from. 21 | * 22 | * @author Jacob von Eyben - http://www.ancientprogramming.com 23 | * @since 1.0.0 24 | */ 25 | public class FormatContext { 26 | 27 | private int offset; 28 | private Class dataType; 29 | private Class> formatter; 30 | 31 | public FormatContext(int offset, Class dataType, Class> formatter) { 32 | this.offset = offset; 33 | this.dataType = dataType; 34 | this.formatter = formatter; 35 | } 36 | 37 | public int getOffset() { 38 | return offset; 39 | } 40 | 41 | public Class getDataType() { 42 | return dataType; 43 | } 44 | 45 | public Class> getFormatter() { 46 | return formatter; 47 | } 48 | 49 | 50 | public String toString() { 51 | return "FormatContext{" + 52 | "offset=" + offset + 53 | ", dataType=" + dataType.getName() + 54 | ", formatter=" + formatter.getName() + 55 | '}'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/FormatInstructions.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.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatBooleanData; 20 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatPatternData; 21 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 23 | 24 | /** 25 | * Contains instructions on how to export and load fixed formatted data. 26 | * 27 | * @author Jacob von Eyben - http://www.ancientprogramming.com 28 | * @since 1.0.0 29 | */ 30 | public class FormatInstructions { 31 | 32 | private int length; 33 | private Align alignment; 34 | private char paddingChar; 35 | private FixedFormatPatternData fixedFormatPatternData; 36 | private FixedFormatBooleanData fixedFormatBooleanData; 37 | private FixedFormatNumberData fixedFormatNumberData; 38 | private FixedFormatDecimalData fixedFormatDecimalData; 39 | 40 | public FormatInstructions(int length, Align alignment, char paddingChar, FixedFormatPatternData fixedFormatPatternData, FixedFormatBooleanData fixedFormatBooleanData, FixedFormatNumberData fixedFormatNumberData, FixedFormatDecimalData fixedFormatDecimalData) { 41 | this.length = length; 42 | this.alignment = alignment; 43 | this.paddingChar = paddingChar; 44 | this.fixedFormatPatternData = fixedFormatPatternData; 45 | this.fixedFormatBooleanData = fixedFormatBooleanData; 46 | this.fixedFormatNumberData = fixedFormatNumberData; 47 | this.fixedFormatDecimalData = fixedFormatDecimalData; 48 | } 49 | 50 | public int getLength() { 51 | return length; 52 | } 53 | 54 | public Align getAlignment() { 55 | return alignment; 56 | } 57 | 58 | public char getPaddingChar() { 59 | return paddingChar; 60 | } 61 | 62 | public FixedFormatPatternData getFixedFormatPatternData() { 63 | return fixedFormatPatternData; 64 | } 65 | 66 | public FixedFormatBooleanData getFixedFormatBooleanData() { 67 | return fixedFormatBooleanData; 68 | } 69 | 70 | public FixedFormatDecimalData getFixedFormatDecimalData() { 71 | return fixedFormatDecimalData; 72 | } 73 | 74 | public FixedFormatNumberData getFixedFormatNumberData() { 75 | return fixedFormatNumberData; 76 | } 77 | 78 | public String toString() { 79 | return "FormatInstructions{" + 80 | "length=" + length + 81 | ", alignment=" + alignment + 82 | ", paddingChar='" + paddingChar + "'" + 83 | ", fixedFormatPatternData=" + fixedFormatPatternData + 84 | ", fixedFormatBooleanData=" + fixedFormatBooleanData + 85 | ", fixedFormatNumberData=" + fixedFormatNumberData + 86 | ", fixedFormatDecimalData=" + fixedFormatDecimalData + 87 | '}'; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/ParseException.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 | 20 | import java.lang.reflect.Method; 21 | 22 | /** 23 | * Used in cases where data couldn't be parse according to the {@link FormatContext} and {@link FormatInstructions}. 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.2.0 27 | */ 28 | public class ParseException extends FixedFormatException { 29 | 30 | private String completeText; 31 | private String failedText; 32 | private Class annotatedClass; 33 | private Method annotatedMethod; 34 | private FormatContext formatContext; 35 | private FormatInstructions formatInstructions; 36 | 37 | /** 38 | * Create an new instance 39 | * @param completeText the complete text that failed to be parsed 40 | * @param failedText the part of the complete text that failed the actual parsing according to the {@link #getFormatInstructions()} 41 | * @param annotatedClass the Class containing the fixedformat annotations 42 | * @param annotatedMethod the method containing the annotations that was used to trying to parse the text in {@link #getFailedText()} 43 | * @param formatContext the context within the parsing was tried 44 | * @param formatInstructions The format instructions used to try parsing the text in {@link #getFailedText()} 45 | * @param cause the reason why the data couldn't be parsed 46 | */ 47 | public ParseException(String completeText, String failedText, Class annotatedClass, Method annotatedMethod, FormatContext formatContext, FormatInstructions formatInstructions, Throwable cause) { 48 | super("Failed to parse '" + failedText + "' at offset " + formatContext.getOffset() + " as " + formatContext.getDataType().getName() + " from '" + completeText + "'. Got format instructions from " + annotatedClass.getName() + "." + annotatedMethod.getName() + ". See details{" + formatContext.toString() + ", " +formatInstructions.toString() + "}", cause); 49 | this.completeText = completeText; 50 | this.failedText = failedText; 51 | this.annotatedClass = annotatedClass; 52 | this.annotatedMethod = annotatedMethod; 53 | this.formatContext = formatContext; 54 | this.formatInstructions = formatInstructions; 55 | } 56 | 57 | /** 58 | * Contains the complete text that failed to be parsed 59 | * @return String containing the complete text 60 | */ 61 | public String getCompleteText() { 62 | return completeText; 63 | } 64 | 65 | /** 66 | * The part of the complete text that failed the actual parsing according to the {@link #getFormatInstructions()} 67 | * @return String containing the part that failed 68 | */ 69 | public String getFailedText() { 70 | return failedText; 71 | } 72 | 73 | /** 74 | * The Class containing the fixedformat annotations 75 | * @return the Class 76 | */ 77 | public Class getAnnotatedClass() { 78 | return annotatedClass; 79 | } 80 | 81 | /** 82 | * The method containing the annotations that was used to trying to parse the text in {@link #getFailedText()} 83 | * @return the annotated method 84 | */ 85 | public Method getAnnotatedMethod() { 86 | return annotatedMethod; 87 | } 88 | 89 | /** 90 | * The context within the parsing was tried 91 | * @return the format context 92 | */ 93 | public FormatContext getFormatContext() { 94 | return formatContext; 95 | } 96 | 97 | /** 98 | * The format instructions used to try parsing the text in {@link #getFailedText()} 99 | * @return the format instructions 100 | */ 101 | public FormatInstructions getFormatInstructions() { 102 | return formatInstructions; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/data/FixedFormatBooleanData.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.data; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatBoolean; 19 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatBoolean.*; 20 | 21 | /** 22 | * Data object containing the exact same data as {@link FixedFormatBoolean} 23 | * 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.0.0 26 | */ 27 | public class FixedFormatBooleanData { 28 | 29 | public static final FixedFormatBooleanData DEFAULT = new FixedFormatBooleanData(TRUE_VALUE, FALSE_VALUE); 30 | 31 | private String trueValue; 32 | 33 | private String falseValue; 34 | 35 | public FixedFormatBooleanData(String trueValue, String falseValue) { 36 | this.trueValue = trueValue; 37 | this.falseValue = falseValue; 38 | } 39 | 40 | public String getTrueValue() { 41 | return trueValue; 42 | } 43 | 44 | public String getFalseValue() { 45 | return falseValue; 46 | } 47 | 48 | 49 | public String toString() { 50 | return "FixedFormatBooleanData{" + 51 | "trueValue='" + trueValue + "'" + 52 | ", falseValue='" + falseValue + "'" + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/data/FixedFormatDecimalData.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.data; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatDecimal; 19 | 20 | import java.math.RoundingMode; 21 | 22 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatDecimal.*; 23 | 24 | /** 25 | * Data object containing the exact same data as {@link FixedFormatDecimal} 26 | * 27 | * @author Jacob von Eyben - http://www.ancientprogramming.com 28 | * @since 1.0.0 29 | */ 30 | public class FixedFormatDecimalData { 31 | 32 | private int decimals; 33 | private boolean useDecimalDelimiter; 34 | private char decimalDelimiter; 35 | private RoundingMode roundingMode; 36 | 37 | public static final FixedFormatDecimalData DEFAULT = new FixedFormatDecimalData(DECIMALS, USE_DECIMAL_DELIMITER, DECIMAL_DELIMITER, RoundingMode.valueOf(ROUNDING_MODE)); 38 | 39 | public FixedFormatDecimalData(int decimals, boolean useDecimalDelimiter, char decimalDelimiter, RoundingMode roundingMode) { 40 | this.decimals = decimals; 41 | this.useDecimalDelimiter = useDecimalDelimiter; 42 | this.decimalDelimiter = decimalDelimiter; 43 | this.roundingMode = roundingMode; 44 | } 45 | 46 | public int getDecimals() { 47 | return decimals; 48 | } 49 | 50 | public boolean isUseDecimalDelimiter() { 51 | return useDecimalDelimiter; 52 | } 53 | 54 | public char getDecimalDelimiter() { 55 | return decimalDelimiter; 56 | } 57 | 58 | public RoundingMode getRoundingMode() { 59 | return roundingMode; 60 | } 61 | 62 | public String toString() { 63 | return "FixedFormatDecimalData{" + 64 | "decimals=" + decimals + 65 | ", useDecimalDelimiter=" + useDecimalDelimiter + 66 | ", decimalDelimiter='" + decimalDelimiter + "'" + 67 | ", roundingMode='" + roundingMode + "'" + 68 | '}'; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/data/FixedFormatNumberData.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.data; 17 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 18 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.*; 19 | 20 | /** 21 | * Data object containing the exact same data as {@link com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber} 22 | * 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.1.0 25 | */ 26 | public class FixedFormatNumberData { 27 | 28 | public static final FixedFormatNumberData DEFAULT = new FixedFormatNumberData(Sign.NOSIGN, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN); 29 | 30 | private Sign signing; 31 | private char positiveSign; 32 | private char negativeSign; 33 | 34 | public FixedFormatNumberData(Sign signing, char positiveSign, char negativeSign) { 35 | this.signing = signing; 36 | this.positiveSign = positiveSign; 37 | this.negativeSign = negativeSign; 38 | } 39 | 40 | 41 | public Sign getSigning() { 42 | return signing; 43 | } 44 | 45 | public Character getPositiveSign() { 46 | return positiveSign; 47 | } 48 | 49 | public Character getNegativeSign() { 50 | return negativeSign; 51 | } 52 | 53 | 54 | public String toString() { 55 | return "FixedFormatNumberData{" + 56 | "signing=" + signing + 57 | ", positiveSign='" + positiveSign + "'" + 58 | ", negativeSign='" + negativeSign + "'" + 59 | '}'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/data/FixedFormatPatternData.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.data; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 19 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern.*; 20 | 21 | /** 22 | * Data object containing the exact same data as {@link FixedFormatPattern} 23 | * 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.0.0 26 | */ 27 | public class FixedFormatPatternData { 28 | 29 | private String pattern; 30 | public static final FixedFormatPatternData DEFAULT = new FixedFormatPatternData(DATE_PATTERN); 31 | 32 | public FixedFormatPatternData(String pattern) { 33 | this.pattern = pattern; 34 | } 35 | 36 | public String getPattern() { 37 | return pattern; 38 | } 39 | 40 | 41 | public String toString() { 42 | return "FixedFormatPatternData{" + 43 | "pattern='" + pattern + '\'' + 44 | '}'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/AbstractDecimalFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | import org.apache.commons.lang.StringUtils; 20 | import org.apache.commons.logging.Log; 21 | import org.apache.commons.logging.LogFactory; 22 | 23 | import java.math.BigDecimal; 24 | import java.math.RoundingMode; 25 | import java.text.DecimalFormat; 26 | 27 | /** 28 | * Base class for formatting decimal data 29 | * 30 | * @author Jacob von Eyben - http://www.ancientprogramming.com 31 | * @since 1.0.0 32 | */ 33 | public abstract class AbstractDecimalFormatter extends AbstractNumberFormatter { 34 | 35 | private static final Log LOG = LogFactory.getLog(AbstractDecimalFormatter.class); 36 | 37 | public String asString(T obj, FormatInstructions instructions) { 38 | BigDecimal roundedValue = null; 39 | int decimals = instructions.getFixedFormatDecimalData().getDecimals(); 40 | if (obj != null) { 41 | BigDecimal value = obj instanceof BigDecimal ? (BigDecimal)obj : BigDecimal.valueOf(obj.doubleValue()); 42 | 43 | RoundingMode roundingMode = instructions.getFixedFormatDecimalData().getRoundingMode(); 44 | 45 | roundedValue = value.setScale(decimals, roundingMode); 46 | 47 | if (LOG.isDebugEnabled()) { 48 | LOG.debug("Value before rounding = '" + value + "', value after rounding = '" + roundedValue + "', decimals = " + decimals + ", rounding mode = " + roundingMode); 49 | } 50 | } 51 | 52 | DecimalFormat formatter = new DecimalFormat(); 53 | formatter.setDecimalSeparatorAlwaysShown(true); 54 | formatter.setMaximumFractionDigits(decimals); 55 | 56 | char decimalSeparator = formatter.getDecimalFormatSymbols().getDecimalSeparator(); 57 | char groupingSeparator = formatter.getDecimalFormatSymbols().getGroupingSeparator(); 58 | String zeroString = "0" + decimalSeparator + "0"; 59 | 60 | String rawString = roundedValue != null ? formatter.format(roundedValue) : zeroString; 61 | if (LOG.isDebugEnabled()) { 62 | LOG.debug("rawString: " + rawString + " - G[" + groupingSeparator + "] D[" + decimalSeparator + "]"); 63 | } 64 | rawString = rawString.replaceAll("\\" + groupingSeparator, ""); 65 | boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter(); 66 | 67 | String beforeDelimiter = rawString.substring(0, rawString.indexOf(decimalSeparator)); 68 | String afterDelimiter = rawString.substring(rawString.indexOf(decimalSeparator)+1, rawString.length()); 69 | if (LOG.isDebugEnabled()) { 70 | LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]"); 71 | } 72 | 73 | //trim decimals 74 | afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals); 75 | afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0'); 76 | 77 | String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter() : ""; 78 | String result = beforeDelimiter + delimiter + afterDelimiter; 79 | if (LOG.isDebugEnabled()) { 80 | LOG.debug("result[" + result + "]"); 81 | } 82 | return result; 83 | } 84 | 85 | protected String getStringToConvert(String string, FormatInstructions instructions) { 86 | String toConvert; 87 | boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter(); 88 | if(useDecimalDelimiter) { 89 | char delimiter = instructions.getFixedFormatDecimalData().getDecimalDelimiter(); 90 | toConvert = string.replace(delimiter, '.'); //convert to normal delimiter 91 | } else { 92 | int decimals = instructions.getFixedFormatDecimalData().getDecimals(); 93 | final boolean theZeroString = string.matches("^[0]+$"); 94 | //only change the string to convert if data contains decimals AND is not the zero string 95 | if (decimals > 0 && !theZeroString) { 96 | //ensuring the string to convert is at least as long as the decimals length 97 | string = StringUtils.leftPad(string, decimals, "0"); 98 | String beforeDelimiter = string.substring(0, string.length()-decimals); 99 | String afterDelimiter = string.substring(string.length()-decimals); 100 | toConvert = beforeDelimiter + '.' + afterDelimiter; 101 | } else { 102 | toConvert = string; 103 | } 104 | 105 | } 106 | return toConvert; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/AbstractNumberFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 19 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | 22 | /** 23 | * Apply signing to values 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.1.0 27 | */ 28 | public abstract class AbstractNumberFormatter extends AbstractFixedFormatter { 29 | 30 | /** 31 | * Override and applies signing instead of align. 32 | * 33 | * @param value the value 34 | * @param instructions the instructions 35 | * @return the parsed object 36 | */ 37 | public T parse(String value, FormatInstructions instructions) { 38 | T result = null; 39 | if (value != null) { 40 | Sign signing = instructions.getFixedFormatNumberData().getSigning(); 41 | String rawString = signing.remove(value, instructions); 42 | result = asObject(rawString, instructions); 43 | } 44 | return result; 45 | } 46 | 47 | /** 48 | * Override and applies signing instead of align. 49 | * 50 | * @param obj the object to format 51 | * @param instructions the instructions 52 | * @return the raw value 53 | */ 54 | public String format(T obj, FormatInstructions instructions) { 55 | return instructions.getFixedFormatNumberData().getSigning().apply(asString(obj, instructions), instructions); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/BigDecimalFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | 20 | import java.math.BigDecimal; 21 | 22 | /** 23 | * Formatter for {@link BigDecimal} data 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.0.0 27 | */ 28 | public class BigDecimalFormatter extends AbstractDecimalFormatter { 29 | 30 | public BigDecimal asObject(String string, FormatInstructions instructions) { 31 | String toConvert = getStringToConvert(string, instructions); 32 | return new BigDecimal("".equals(toConvert) ? "0" : toConvert); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/BooleanFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 19 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | import org.apache.commons.lang.StringUtils; 22 | 23 | /** 24 | * Formatter for {@link Boolean} data 25 | * 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | public class BooleanFormatter extends AbstractFixedFormatter { 30 | 31 | public Boolean asObject(String string, FormatInstructions instructions) throws FixedFormatException { 32 | Boolean result = false; 33 | if (!StringUtils.isEmpty(string)) { 34 | if (instructions.getFixedFormatBooleanData().getTrueValue().equals(string)) { 35 | result = true; 36 | } else if (instructions.getFixedFormatBooleanData().getFalseValue().equals(string)) { 37 | result = false; 38 | } else { 39 | throw new FixedFormatException("Could not convert string[" + string + "] to boolean value according to booleanData[" + instructions.getFixedFormatBooleanData() + "]"); 40 | } 41 | } 42 | return result; 43 | } 44 | 45 | public String asString(Boolean obj, FormatInstructions instructions) { 46 | String result = instructions.getFixedFormatBooleanData().getFalseValue(); 47 | if (obj != null) { 48 | result = obj ? instructions.getFixedFormatBooleanData().getTrueValue() : instructions.getFixedFormatBooleanData().getFalseValue(); 49 | } 50 | return result; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/ByTypeFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 19 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 20 | import com.ancientprogramming.fixedformat4j.format.FormatContext; 21 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 22 | 23 | import java.io.Serializable; 24 | import java.math.BigDecimal; 25 | import java.util.Date; 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | /** 30 | * Formatter capable of formatting a bunch of known java standard library classes. So far: 31 | * {@link String}, {@link Integer}, {@link Short}, {@link Long}, {@link Date}, 32 | * {@link Character}, {@link Boolean}, {@link Double}, {@link Float} and {@link BigDecimal} 33 | * 34 | * 35 | * @author Jacob von Eyben - http://www.ancientprogramming.com 36 | * @since 1.0.0 37 | */ 38 | public class ByTypeFormatter implements FixedFormatter { 39 | private FormatContext context; 40 | 41 | private static final Map, Class> KNOWN_FORMATTERS = new HashMap, Class>(); 42 | 43 | static { 44 | KNOWN_FORMATTERS.put(String.class, StringFormatter.class); 45 | KNOWN_FORMATTERS.put(short.class, ShortFormatter.class); 46 | KNOWN_FORMATTERS.put(Short.class, ShortFormatter.class); 47 | KNOWN_FORMATTERS.put(int.class, IntegerFormatter.class); 48 | KNOWN_FORMATTERS.put(Integer.class, IntegerFormatter.class); 49 | KNOWN_FORMATTERS.put(long.class, LongFormatter.class); 50 | KNOWN_FORMATTERS.put(Long.class, LongFormatter.class); 51 | KNOWN_FORMATTERS.put(Date.class, DateFormatter.class); 52 | KNOWN_FORMATTERS.put(char.class, CharacterFormatter.class); 53 | KNOWN_FORMATTERS.put(Character.class, CharacterFormatter.class); 54 | KNOWN_FORMATTERS.put(boolean.class, BooleanFormatter.class); 55 | KNOWN_FORMATTERS.put(Boolean.class, BooleanFormatter.class); 56 | KNOWN_FORMATTERS.put(double.class, DoubleFormatter.class); 57 | KNOWN_FORMATTERS.put(Double.class, DoubleFormatter.class); 58 | KNOWN_FORMATTERS.put(float.class, FloatFormatter.class); 59 | KNOWN_FORMATTERS.put(Float.class, FloatFormatter.class); 60 | KNOWN_FORMATTERS.put(BigDecimal.class, BigDecimalFormatter.class); 61 | } 62 | 63 | public ByTypeFormatter(FormatContext context) { 64 | this.context = context; 65 | } 66 | 67 | 68 | public Object parse(String value, FormatInstructions instructions) { 69 | FixedFormatter formatter = actualFormatter(context.getDataType()); 70 | return formatter.parse(value, instructions); 71 | } 72 | 73 | public String format(Object value, FormatInstructions instructions) { 74 | FixedFormatter formatter = actualFormatter(context.getDataType()); 75 | return formatter.format(value, instructions); 76 | } 77 | 78 | public FixedFormatter actualFormatter(final Class dataType) { 79 | Class formatterClass = KNOWN_FORMATTERS.get(dataType); 80 | 81 | if (formatterClass != null) { 82 | try { 83 | return formatterClass.getConstructor().newInstance(); 84 | } catch (NoSuchMethodException e) { 85 | throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "] because no default constructor exists"); 86 | } catch (Exception e) { 87 | throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "]", e); 88 | } 89 | } else { 90 | throw new FixedFormatException(ByTypeFormatter.class.getName() + " cannot handle datatype[" + dataType.getName() + "]. Provide your own custom FixedFormatter for this datatype."); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/CharacterFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | import org.apache.commons.logging.Log; 21 | import org.apache.commons.logging.LogFactory; 22 | import org.apache.commons.lang.StringUtils; 23 | 24 | /** 25 | * Formatter for {@link Character} data 26 | * 27 | * @author Jacob von Eyben - http://www.ancientprogramming.com 28 | * @since 1.0.0 29 | */ 30 | public class CharacterFormatter extends AbstractFixedFormatter { 31 | 32 | private static final Log LOG = LogFactory.getLog(CharacterFormatter.class); 33 | 34 | public Character asObject(String string, FormatInstructions instructions) { 35 | Character result = null; 36 | if (!StringUtils.isEmpty(string)) { 37 | result = string.charAt(0); 38 | if (string.length() > 1) { 39 | LOG.warn("found more than one character[" + string + "] after reading instructions from record. Will return first character[" + result + "]"); 40 | } 41 | } 42 | return result; 43 | } 44 | 45 | public String asString(Character obj, FormatInstructions instructions) { 46 | String result = ""; 47 | if (obj != null) { 48 | result = Character.toString(obj); 49 | } 50 | return result; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/DateFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 19 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | import org.apache.commons.lang.StringUtils; 22 | 23 | import java.text.DateFormat; 24 | import java.text.ParseException; 25 | import java.text.SimpleDateFormat; 26 | import java.util.Date; 27 | 28 | /** 29 | * Formatter for {@link java.util.Date} data. 30 | * The formatting and parsing is perfomed by using an instance of the {@link SimpleDateFormat} class. 31 | * 32 | * @author Jacob von Eyben - http://www.ancientprogramming.com 33 | * @since 1.0.0 34 | */ 35 | public class DateFormatter extends AbstractFixedFormatter { 36 | 37 | public Date asObject(String string, FormatInstructions instructions) throws FixedFormatException { 38 | Date result = null; 39 | 40 | if (!StringUtils.isEmpty(string)) { 41 | try { 42 | result = getFormatter(instructions.getFixedFormatPatternData().getPattern()).parse(string); 43 | } catch (ParseException e) { 44 | throw new FixedFormatException("Could not parse value[" + string + "] by pattern[" + instructions.getFixedFormatPatternData().getPattern() + "] to " + Date.class.getName()); 45 | } 46 | } 47 | return result; 48 | } 49 | 50 | public String asString(Date date, FormatInstructions instructions) { 51 | String result = null; 52 | if (date != null) { 53 | result = getFormatter(instructions.getFixedFormatPatternData().getPattern()).format(date); 54 | } 55 | return result; 56 | } 57 | 58 | DateFormat getFormatter(String pattern) { 59 | return new SimpleDateFormat(pattern); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/DoubleFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | 20 | /** 21 | * Formatter for {@link Double} data 22 | * 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.0.0 25 | */ 26 | public class DoubleFormatter extends AbstractDecimalFormatter { 27 | 28 | public Double asObject(String string, FormatInstructions instructions) { 29 | String toConvert = getStringToConvert(string, instructions); 30 | return Double.parseDouble("".equals(toConvert) ? "0" : toConvert); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/FloatFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | 20 | /** 21 | * Formatter for {@link Float} data 22 | * 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.0.0 25 | */ 26 | public class FloatFormatter extends AbstractDecimalFormatter { 27 | 28 | public Float asObject(String string, FormatInstructions instructions) { 29 | String toConvert = getStringToConvert(string, instructions); 30 | return Float.parseFloat("".equals(toConvert) ? "0" : toConvert); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/IntegerFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | 20 | /** 21 | * Formatter for {@link Integer} data 22 | * 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.0.0 25 | */ 26 | public class IntegerFormatter extends AbstractNumberFormatter { 27 | 28 | public Integer asObject(String string, FormatInstructions instructions) { 29 | return Integer.parseInt(string); 30 | } 31 | 32 | public String asString(Integer obj, FormatInstructions instructions) { 33 | String result = null; 34 | if (obj != null) { 35 | result = Integer.toString(obj); 36 | } 37 | return result; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/LongFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | 21 | /** 22 | * Formatter for {@link Long} data 23 | * 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.0.0 26 | */ 27 | public class LongFormatter extends AbstractNumberFormatter { 28 | 29 | public Long asObject(String string, FormatInstructions instructions) { 30 | return Long.parseLong(string); 31 | } 32 | 33 | public String asString(Long obj, FormatInstructions instructions) { 34 | String result = null; 35 | if (obj != null) { 36 | result = Long.toString(obj); 37 | } 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/ShortFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 19 | 20 | /** 21 | * Formatter for {@link Short} data 22 | * 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.3.0 25 | */ 26 | public class ShortFormatter extends AbstractNumberFormatter { 27 | 28 | public Short asObject(String string, FormatInstructions instructions) { 29 | return Short.parseShort(string); 30 | } 31 | 32 | public String asString(Short obj, FormatInstructions instructions) { 33 | String result = null; 34 | if (obj != null) { 35 | result = Short.toString(obj); 36 | } 37 | return result; 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /fixedformat4j/src/main/java/com/ancientprogramming/fixedformat4j/format/impl/StringFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | 21 | /** 22 | * Formatter for {@link String} data 23 | * 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.0.0 26 | */ 27 | public class StringFormatter extends AbstractFixedFormatter { 28 | 29 | public String asObject(String string, FormatInstructions instructions) { 30 | return string; 31 | } 32 | 33 | public String asString(String string, FormatInstructions instructions) { 34 | String result = null; 35 | if (string != null) { 36 | result = string; 37 | } 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/fiveMinutesTutorial.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | 5 minutes tutorial 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | 5 minutes tutorial 8 | 9 | This tutorial will demonstrate how easy it is to read and write in fixed format using fixedformat4j. 10 | 11 | * Reading and writing using build in datatypes. 12 | 13 | Lets assume you would like to read and write some text like this: 14 | 15 | +-- 16 | A200803150000140050A longer desciption T 17 | +-- 18 | 19 | (TBD) 20 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/fixedformat4jOverview.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | Fixedformat4j overview 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | FixedFormat4J Overview 8 | 9 | * Annotations 10 | 11 | * RecordFactory 12 | 13 | * Formatters 14 | 15 | * -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/get-it.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | Get it 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | Get the latest version of fixedformat4j 8 | 9 | Fixedformat4j is available in one of two ways: 10 | 11 | [[1]] You can either download it from the {{{http://code.google.com/p/fixedformat4j/downloads/list}download list}} at {{http://fixedformat4j.googlecode.com}}. 12 | 13 | You can see fixedformat4j's dependencies {{{dependencies.html}here}}. 14 | 15 | [[2]] You can depend on fixedformat4j in your maven project by adding the following dependency to your pom.xml: 16 | 17 | +--- 18 | 19 | com.ancientprogramming.fixedformat4j 20 | fixedformat4j 21 | 1.2.2 22 | 23 | +--- 24 | 25 | Repository 26 | 27 | I will submit an upload request to the maven ibiblio repository after each release. Unfortunately it can take som time 28 | to process such an upload request and that is why I have created a public maven repository where the latest fixedformat4j 29 | releases can be found. 30 | 31 | Just add the following to your pom.xml and you will always have access to the latest fixedformat4j versions! 32 | 33 | +--- 34 | 35 | ... 36 | 37 | fixedformat4j-repo 38 | http://maven.ancientprogramming.com/repository 39 | 40 | ... 41 | 42 | +--- 43 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/index.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | A Java API for working with fixed formatted textfiles 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | Fixedformat4j introduction 8 | 9 | Fixedformat4j is intended to be an easy to use, small and non intrusive Java framework for working with flat fixed formatted text files. By annotating 10 | your code you can setup the offsets and format as for your data when reading/writing to and from flat fixed format files. 11 | 12 | Fixedformat4j handles many build in datatypes like: String, Character/char, Long/long, Integer/int, Short/short, Double / double, Float /float, Boolean / boolean, Date and BigDecmial 13 | 14 | At the same time it is straight forward to write and plugin your own formatters for custom datatypes. 15 | 16 | * Why should I use Fixedformat4j? 17 | 18 | If you need to read or write fixed format text, fixedformat4j is the api to use: 19 | 20 | * Support for both loading and exporting objects to and from text 21 | 22 | * Uses annotations as a clean way to instruct how your data should be read and written. 23 | 24 | * Support for many build-in datatypes, which means you don't have to write format and parse rutines. 25 | 26 | * Handles signed numbers. ex. string like '-1000' or '1000-' can be treated as a negative 1000 number. 27 | 28 | * Detailed error reporting incase a parsing fails. 29 | 30 | * Deployed into the global maven repository for easy accesss. 31 | 32 | * Getting started 33 | 34 | To start using Fixedformat4j you only have to annotate your getter methods and use the FixedFormatManager to load and 35 | export your fixedformat text according to your annotation instructions. 36 | 37 | A sample can be seen here: 38 | 39 | %{code-snippet|id=basicrecord|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic/BasicRecord.java} 40 | 41 | This annotated class can now be loaded and exported using a FixedFormatManager 42 | 43 | %{code-snippet|id=basicusage|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic/BasicUsage.java} 44 | 45 | Running this simple program will generate the following output. 46 | 47 | Note that the integer changed value in the exported string. 48 | 49 | +-- 50 | The parsed string: string 51 | The parsed integer: 123 52 | The parsed date: Thu May 29 00:00:00 CEST 2008 53 | Exported: string 001002008-05-29 54 | +-- 55 | 56 | You can see the complete sourcecode for this sample {{{http://code.google.com/p/fixedformat4j/source/browse/trunk/samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic}here}}. -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/usage/index.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | Usage 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | Usage 8 | 9 | * Overview 10 | 11 | To read and write text to and from java objects you will annotate your java object with instructions 12 | on where data is located in the string and how to translate the data into meaningful objects. 13 | 14 | You will use an instance of the FixedFormatManager to load and export the data to and from string representation. 15 | 16 | Fixedformat4j rely on your java object adhere to the following conventions for being able to 17 | load and export data: 18 | 19 | [[1]] A class that is to be used in fixedformat4j has to be annotated with the Record annotation. 20 | 21 | [[2]] A property is a pair of a getter and a setter method. 22 | 23 | [[3]] The getter method is annotated with instructions on how to convert to and from string representation. 24 | 25 | ** Example 26 | 27 | The Following example defines one string property on a pojo class called BasicRecord 28 | 29 | %{code-snippet|id=basicusage|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/BasicUsageRecord.java} 30 | 31 | * FixedFormatManager 32 | 33 | An instance of a fixedformatmanager does the work of translating annotation instructions and controlling the actual 34 | parsing and formatting. 35 | 36 | Fixedformat4j comes with an implementation of a FixedFormatManager called FixedFormatManagerImpl. 37 | 38 | ** Example 39 | 40 | %{code-snippet|id=basicusage|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/BasicUsageManager.java} 41 | 42 | * Annotations 43 | 44 | As you can see you will use annotations to instruct the manager on how to load and export data. 45 | 46 | A complete list of annotations can be found {{{annotations.html}here}}. -------------------------------------------------------------------------------- /fixedformat4j/src/site/apt/usage/nested-records.apt: -------------------------------------------------------------------------------- 1 | ------ 2 | Nested records 3 | ------ 4 | Jacob von Eyben 5 | ------ 6 | 7 | Nested records 8 | 9 | * Overview 10 | 11 | A record annotated class can contain nested record annotated member objects that will be processed by the FixedFormatManager. 12 | 13 | When the FixedFormatManager meets a Field annotated getter method returning a Record annotated object, the FixedFormatManager 14 | will format the field as a Record and *not* use any given formatter. 15 | 16 | ** Example 17 | 18 | The Following example shows how you can use nested records: 19 | 20 | %{code-snippet|id=basicrecord|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic/BasicRecord.java} 21 | 22 | %{code-snippet|id=nestedrecord|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/NestedRecord.java} 23 | 24 | And here is a simple main program showing how to access nested record data: 25 | 26 | %{code-snippet|id=nestedrecordusage|code=java|file=../samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/NestedRecordUsage.java} 27 | 28 | The outut looks like this: 29 | 30 | +--- 31 | foo bar 001232008-10-21 32 | foo 33 | bar 34 | 123 35 | Tue Oct 21 00:00:00 CEST 2008 36 | foo fubar 098762008-10-21 37 | +--- -------------------------------------------------------------------------------- /fixedformat4j/src/site/fml/faq.fml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | General 7 | 8 | 9 | 10 | Can fixedformat4j help me parse large text files? 11 | 12 | 13 |

As the API is now it can't. But on the other hand it doesn't constrain you in anyway either.

14 |

Fixedformat4j currently concentrate on mapping one single line to a java object. 15 | You are free to loop through any large text file and use the fixedformatmanager to create 16 | instances of java objects.

17 |
18 |
19 | 20 | 21 | I would like to map my own object. Can I apply my own custom formatter to do this? 22 | 23 | 24 |

Yes.

25 |

To map your domain object you should create a custum formatter instance implementing the FixedFormatter interface.

26 |

Your custom formatter will have access to all the different annotation data through a FormatInstructions instance. 27 | Your formatter receives this isntance when the parse and format methods of your formatter is called by the FixedFormatManager.
28 | Ex. if you would like to access the value given to the FixedFormatPattern annotation you do this through the formatInstruction.getFixedFormatPatternData().getPattern()

29 |

To instruct the manager to use you custom formatter, you set the formatter attribute of the field annotation:

30 |

@Field(offset = 1, length = 10, formatter = CustomFormatter.class)

31 |
32 |
33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
-------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/css/maven-base.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0px; 3 | padding: 0px; 4 | } 5 | img { 6 | border:none; 7 | } 8 | table { 9 | padding:0px; 10 | width: 100%; 11 | margin-left: -2px; 12 | margin-right: -2px; 13 | } 14 | acronym { 15 | cursor: help; 16 | border-bottom: 1px dotted #feb; 17 | } 18 | table.bodyTable th, table.bodyTable td { 19 | padding: 2px 4px 2px 4px; 20 | vertical-align: top; 21 | } 22 | div.clear{ 23 | clear:both; 24 | visibility: hidden; 25 | } 26 | div.clear hr{ 27 | display: none; 28 | } 29 | #bannerLeft, #bannerRight { 30 | font-size: xx-large; 31 | font-weight: bold; 32 | } 33 | #bannerLeft img, #bannerRight img { 34 | margin: 0px; 35 | } 36 | .xleft, #bannerLeft img { 37 | float:left; 38 | text-shadow: #7CFC00 1px 1px 1px; 39 | } 40 | .xright, #bannerRight { 41 | float:right; 42 | text-shadow: #7CFC00 1px 1px 1px; 43 | } 44 | #banner { 45 | padding: 0px; 46 | } 47 | #banner img { 48 | border: none; 49 | } 50 | #breadcrumbs { 51 | padding: 3px 10px 3px 10px; 52 | } 53 | #leftColumn { 54 | width: 170px; 55 | float:left; 56 | overflow: auto; 57 | } 58 | #bodyColumn { 59 | margin-right: 1.5em; 60 | margin-left: 197px; 61 | } 62 | #legend { 63 | padding: 8px 0 8px 0; 64 | } 65 | #navcolumn { 66 | padding: 8px 4px 0 8px; 67 | } 68 | #navcolumn h5 { 69 | margin: 0; 70 | padding: 0; 71 | font-size: small; 72 | } 73 | #navcolumn ul { 74 | margin: 0; 75 | padding: 0; 76 | font-size: small; 77 | } 78 | #navcolumn li { 79 | list-style-type: none; 80 | background-image: none; 81 | background-repeat: no-repeat; 82 | background-position: 0 0.4em; 83 | padding-left: 16px; 84 | list-style-position: outside; 85 | line-height: 1.2em; 86 | font-size: smaller; 87 | } 88 | #navcolumn li.expanded { 89 | background-image: url(../images/expanded.gif); 90 | } 91 | #navcolumn li.collapsed { 92 | background-image: url(../images/collapsed.gif); 93 | } 94 | #poweredBy { 95 | text-align: center; 96 | } 97 | #navcolumn img { 98 | margin-top: 10px; 99 | margin-bottom: 3px; 100 | } 101 | #poweredBy img { 102 | display:block; 103 | margin: 20px 0 20px 17px; 104 | } 105 | #search img { 106 | margin: 0px; 107 | display: block; 108 | } 109 | #search #q, #search #btnG { 110 | border: 1px solid #999; 111 | margin-bottom:10px; 112 | } 113 | #search form { 114 | margin: 0px; 115 | } 116 | #lastPublished { 117 | font-size: x-small; 118 | } 119 | .navSection { 120 | margin-bottom: 2px; 121 | padding: 8px; 122 | } 123 | .navSectionHead { 124 | font-weight: bold; 125 | font-size: x-small; 126 | } 127 | .section { 128 | padding: 4px; 129 | } 130 | #footer { 131 | padding: 3px 10px 3px 10px; 132 | font-size: x-small; 133 | } 134 | #breadcrumbs { 135 | font-size: x-small; 136 | margin: 0pt; 137 | } 138 | .source { 139 | padding: 12px; 140 | margin: 1em 7px 1em 7px; 141 | } 142 | .source pre { 143 | margin: 0px; 144 | padding: 0px; 145 | } 146 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/css/print.css: -------------------------------------------------------------------------------- 1 | #banner, #footer, #leftcol, #breadcrumbs, .docs #toc, .docs .courtesylinks, #leftColumn, #navColumn { 2 | display: none !important; 3 | } 4 | #bodyColumn, body.docs div.docs { 5 | margin: 0 !important; 6 | border: none !important 7 | } 8 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/images/collapsed.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/site/resources/images/collapsed.gif -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/images/expanded.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/site/resources/images/expanded.gif -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/images/logos/build-by-maven-black.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/site/resources/images/logos/build-by-maven-black.png -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/images/logos/build-by-maven-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/site/resources/images/logos/build-by-maven-white.png -------------------------------------------------------------------------------- /fixedformat4j/src/site/resources/images/logos/maven-feather.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeyben/fixedformat4j/63aa391ba657032d0eada5badc34778285f0a614/fixedformat4j/src/site/resources/images/logos/maven-feather.png -------------------------------------------------------------------------------- /fixedformat4j/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | fixedformat4j 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /fixedformat4j/src/site/xdoc/usage/annotations.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | Annotations 20 | 21 | 22 | 23 |
24 |

25 | Annotations is used to instruct the manager on how to handle properties when loading and exporting classes to 26 | and from strings. 27 |
See the complete list of annotations here: 28 |

29 | 30 | 31 |

Record annotations is used on classes.
It marks that the class contains additional Field annotations 32 | on its getter methods and can be imported and exported to and from string representation using the 33 | FixedFormatManager 34 |

35 | 36 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
37 | Record 38 |
AttributeDefaultTypeRequiredDescription
length-1intnoDefines a minimum length the string represenation of a class should have. 52 |
53 | If the string initially is shorter thatn the given length the string is padded with the given paddingChar 54 |
paddingChar' 'charnoThe paddingChar to use if padding is needed.
64 |
65 | 66 | 67 |

Field annotations are used on getter methods. It contains basic mapping instructions and it is required 68 | for getter/setter pairs that is supposed to be mapped to and from string representation. 69 |

70 | 71 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 113 | 114 | 115 | 116 | 120 | 121 | 122 | 123 | 124 |
72 | Field 73 |
AttributeDefaultTypeRequiredDescription
offset-intyesDefines the offset in a string where the data for this field starts.
length-intyesDefines the length for this field in string representation.
alignAlign. 98 | 99 | LEFT 100 | 101 | AlignnoDefines how to align the field value when it is represented as a string.
paddingChar' 'charnoThe character to pad with if the given length is longer than the field value in string representation. 112 |
formatter 118 | ByTypeFormatter.class 119 | Class<FixedFormatter>noThe formatter to use when reading and writing the field to and from string representation.
125 |
126 | 127 |

Fields annotations are used on getter methods in case a single field maps to more than just one 128 | position in a string.

129 | 130 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 149 | 150 |
131 | Fields 132 |
AttributeDefaultTypeRequiredDescription
value-Field[]yesIf a single field is mapped more than once in a string a Fields annotation can be used to describe this. 146 |
147 | Not: when reading the field from a string only the first Field annotation in the array will be read. 148 |
151 |
152 | 153 |

FixedFormatBoolean annotations are used on getter methods. It is optional and is used when the datatype to map is 154 | a Boolean object.

155 | 156 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 |
157 | FixedFormatBoolean 158 |
AttributeDefaultTypeRequiredDescription
trueValue"T"StringnoDefines how a field should be represented when the field is a equal to java.lang.Boolean.TRUE
falseValue"F"StringnoDefines how a field should be represented when the field is a equal to java.lang.Boolean.FALSE
181 |
182 | 183 |

description on its way...

184 | 185 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 |
186 | FixedFormatNumber 187 |
AttributeDefaultTypeRequiredDescription
signSign.NOSIGNSignnoDefines if and how a number field should be signed when represented as a string. 201 | Besides the default no signing, it is possible to append or prepend a sign. 202 |
positiveSign'+'charnoDefines the character representing a positive sign.
negativeSign'-'charnoDefines the character representing a negative sign.
219 |
220 | 221 |

description on its way...

222 | 223 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 |
224 | FixedFormatDecimal 225 |
AttributeDefaultTypeRequiredDescription
decimals2intnoDefines the number of decimals the field contains.
useDecimalDelimiterfalsebooleannoDefines if the field contains a decimal delimiter when represented as a string.
decimalDelimiter'.'charnoDefines the decimal delimiter to use if the useDecimalDelimiter equals to true
255 |
256 | 257 |

description on its way...

258 | 259 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 280 | 281 |
260 | FixedFormatPattern 261 |
AttributeDefaultTypeRequiredDescription
value-StringyesDefines pattern to use when parsing and formatting the field intro string representation. 275 |
276 | This is currently only used to format dates using the same patterns the 277 | SimpleDateFormatter 278 | uses 279 |
282 |
283 |
284 | 285 |
-------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/annotation/TestAlign.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 junit.framework.TestCase; 19 | 20 | /** 21 | * @author Jacob von Eyben - http://www.ancientprogramming.com 22 | * @since 1.0.0 23 | */ 24 | public class TestAlign extends TestCase { 25 | 26 | public void testLeftPadding() { 27 | assertEquals(" ", Align.RIGHT.apply(null, 1, ' ')); 28 | assertEquals(" ", Align.RIGHT.apply(" ", 1, ' ')); 29 | assertEquals("r", Align.RIGHT.apply("foobar", 1, ' ')); 30 | assertEquals("bar", Align.RIGHT.apply("foobar", 3, ' ')); 31 | assertEquals("foobar", Align.RIGHT.apply("foobar", 6, ' ')); 32 | assertEquals(" foobar", Align.RIGHT.apply("foobar", 7, ' ')); 33 | assertEquals("__foobar", Align.RIGHT.apply("foobar", 8, '_')); 34 | } 35 | 36 | public void testLeftRemove() { 37 | assertEquals("", Align.RIGHT.remove(null, ' ')); 38 | assertEquals("", Align.RIGHT.remove(" ", ' ')); 39 | assertEquals("foobar ", Align.RIGHT.remove("foobar ", ' ')); 40 | assertEquals("foobar", Align.RIGHT.remove(" foobar", ' ')); 41 | } 42 | 43 | public void testRightPadding() { 44 | assertEquals(" ", Align.LEFT.apply(null, 1, ' ')); 45 | assertEquals(" ", Align.LEFT.apply(" ", 1, ' ')); 46 | assertEquals("f", Align.LEFT.apply("foobar", 1, ' ')); 47 | assertEquals("foo", Align.LEFT.apply("foobar", 3, ' ')); 48 | assertEquals("foobar", Align.LEFT.apply("foobar", 6, ' ')); 49 | assertEquals("foobar ", Align.LEFT.apply("foobar", 7, ' ')); 50 | assertEquals("foobar__", Align.LEFT.apply("foobar", 8, '_')); 51 | } 52 | 53 | public void testRightRemove() { 54 | assertEquals("", Align.LEFT.remove(null, ' ')); 55 | assertEquals("", Align.LEFT.remove(" ", ' ')); 56 | assertEquals("foobar", Align.LEFT.remove("foobar ", ' ')); 57 | assertEquals(" foobar", Align.LEFT.remove(" foobar", ' ')); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/annotation/TestSign.java: -------------------------------------------------------------------------------- 1 | package com.ancientprogramming.fixedformat4j.annotation; 2 | 3 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 4 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 5 | import junit.framework.TestCase; 6 | 7 | public class TestSign extends TestCase { 8 | 9 | public void testSignNoSign() { 10 | assertEquals("0000000000", Sign.NOSIGN.apply("", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 11 | assertEquals("0000000000", Sign.NOSIGN.apply("0", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 12 | assertEquals("0000001000", Sign.NOSIGN.apply("1000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 13 | assertEquals("10000", Sign.NOSIGN.remove("0000010000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 14 | } 15 | 16 | public void testSignAppend() { 17 | assertEquals("000000000+", Sign.APPEND.apply("", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 18 | assertEquals("000000000+", Sign.APPEND.apply("0", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 19 | assertEquals("000001000+", Sign.APPEND.apply("1000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 20 | assertEquals("000001000-", Sign.APPEND.apply("-1000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 21 | assertEquals("1000", Sign.APPEND.remove("000001000+", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 22 | assertEquals("1001", Sign.APPEND.remove("000001001+", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 23 | assertEquals("0", Sign.APPEND.remove("000000000+", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 24 | assertEquals("0", Sign.APPEND.remove("000000000-", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 25 | assertEquals("-1", Sign.APPEND.remove("000000001-", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 26 | assertEquals("-2001", Sign.APPEND.remove("000002001-", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 27 | } 28 | 29 | public void testSignPrepend() { 30 | assertEquals("+000000000", Sign.PREPEND.apply("", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 31 | assertEquals("+000000000", Sign.PREPEND.apply("0", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 32 | assertEquals("+000001000", Sign.PREPEND.apply("1000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 33 | assertEquals("1000", Sign.PREPEND.remove("+000001000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 34 | assertEquals("1001", Sign.PREPEND.remove("+000001001", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 35 | assertEquals("0", Sign.PREPEND.remove("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 36 | assertEquals("0", Sign.PREPEND.remove("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 37 | assertEquals("-1", Sign.PREPEND.remove("-000000001", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 38 | assertEquals("-2001", Sign.PREPEND.remove("-000002001", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/TestFixedFormatUtil.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.format.impl.ByTypeFormatter; 19 | import com.ancientprogramming.fixedformat4j.format.impl.StringFormatter; 20 | import junit.framework.TestCase; 21 | 22 | /** 23 | * @author Jacob von Eyben - http://www.ancientprogramming.com 24 | * @since 1.0.0 25 | */ 26 | public class TestFixedFormatUtil extends TestCase { 27 | 28 | public void testGetInstance() { 29 | FixedFormatUtil.getFixedFormatterInstance(StringFormatter.class, new FormatContext(1, java.lang.String.class, StringFormatter.class)); 30 | FixedFormatUtil.getFixedFormatterInstance(ByTypeFormatter.class, new FormatContext(1, java.lang.String.class, ByTypeFormatter.class)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/MultibleFieldsRecord.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Fields; 21 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 22 | import com.ancientprogramming.fixedformat4j.annotation.Record; 23 | 24 | import java.util.Date; 25 | 26 | /** 27 | * @author Jacob von Eyben - http://www.ancientprogramming.com 28 | * @since 1.0.0 29 | */ 30 | @Record 31 | public class MultibleFieldsRecord { 32 | 33 | private String stringData; 34 | private Date dateData; 35 | private Integer integerdata; 36 | 37 | @Field(offset = 1, length = 10, align = Align.RIGHT, paddingChar = ' ') 38 | public String getStringData() { 39 | return stringData; 40 | } 41 | 42 | public void setStringData(String stringData) { 43 | this.stringData = stringData; 44 | } 45 | 46 | @Fields({@Field(offset = 11, length = 8), @Field(offset = 19, length = 8)}) 47 | public Date getDateData() { 48 | return dateData; 49 | } 50 | 51 | public void setDateData(Date dateData) { 52 | this.dateData = dateData; 53 | } 54 | 55 | @Field(offset = 50, length = 4, align = Align.RIGHT, paddingChar = '0') 56 | public Integer getIntegerdata() { 57 | return integerdata; 58 | } 59 | 60 | public void setIntegerdata(Integer integerdata) { 61 | this.integerdata = integerdata; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/MyOtherRecord.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Field; 19 | import com.ancientprogramming.fixedformat4j.annotation.Record; 20 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 21 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 22 | import com.ancientprogramming.fixedformat4j.format.FormatContext; 23 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 24 | 25 | /** 26 | * A record used in testcases which contains annotated fields that reference instance of other annotated type. 27 | */ 28 | @Record 29 | public class MyOtherRecord implements FixedFormatter { 30 | 31 | private MyRecord myRecord; 32 | 33 | public MyOtherRecord(FormatContext context) { 34 | // dummy 35 | } 36 | 37 | public MyOtherRecord(MyRecord myRecord) { 38 | this.myRecord = myRecord; 39 | } 40 | 41 | @Field(offset = 1, length = 0, formatter = MyOtherRecord.class) 42 | public MyRecord getStringData() { 43 | return myRecord; 44 | } 45 | 46 | public Object parse(String value, FormatInstructions instructions) throws FixedFormatException { 47 | return null; 48 | } 49 | 50 | public String format(Object value, FormatInstructions instructions) throws FixedFormatException { 51 | return ""; 52 | } 53 | } -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/MyRecord.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.*; 19 | 20 | import java.math.BigDecimal; 21 | import java.util.Date; 22 | 23 | /** 24 | * A record used in testcases 25 | * 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | @Record 30 | public class MyRecord { 31 | 32 | private String stringData; 33 | private Integer integerData; 34 | private Date dateData; 35 | private Character charData; 36 | private Boolean booleanData; 37 | private Long longData; 38 | private Double doubleData; 39 | private Float floatData; 40 | private BigDecimal bigDecimalData; 41 | private float simpleFloatData; 42 | 43 | 44 | @Field(offset = 1, length = 10, align = Align.RIGHT, paddingChar = ' ') 45 | public String getStringData() { 46 | return stringData; 47 | } 48 | 49 | public void setStringData(String stringData) { 50 | this.stringData = stringData; 51 | } 52 | 53 | @Field(offset = 11, length = 5, align = Align.RIGHT, paddingChar = '0') 54 | public Integer getIntegerData() { 55 | return integerData; 56 | } 57 | 58 | public void setIntegerData(Integer integerData) { 59 | this.integerData = integerData; 60 | } 61 | 62 | @Field(offset = 16, length = 8) 63 | public Date getDateData() { 64 | return dateData; 65 | } 66 | 67 | public void setDateData(Date dateData) { 68 | this.dateData = dateData; 69 | } 70 | 71 | @Field(offset = 24, length = 1) 72 | public Character getCharData() { 73 | return charData; 74 | } 75 | 76 | public void setCharData(Character charData) { 77 | this.charData = charData; 78 | } 79 | 80 | @Field(offset = 25, length = 1) 81 | public Boolean isBooleanData() { 82 | return booleanData; 83 | } 84 | 85 | public void setBooleanData(Boolean booleanData) { 86 | this.booleanData = booleanData; 87 | } 88 | 89 | @Field(offset = 26, length = 4, align = Align.RIGHT, paddingChar = '0') 90 | public Long getLongData() { 91 | return longData; 92 | } 93 | 94 | public void setLongData(Long longData) { 95 | this.longData = longData; 96 | } 97 | 98 | @Field(offset = 30, length = 10, align = Align.RIGHT, paddingChar = '0') 99 | public Double getDoubleData() { 100 | return doubleData; 101 | } 102 | 103 | public void setDoubleData(Double doubleData) { 104 | this.doubleData = doubleData; 105 | } 106 | 107 | @Field(offset = 40, length = 10, align = Align.RIGHT, paddingChar = '0') 108 | public Float getFloatData() { 109 | return floatData; 110 | } 111 | 112 | public void setFloatData(Float floatData) { 113 | this.floatData = floatData; 114 | } 115 | 116 | @Field(offset = 50, length = 10, align = Align.RIGHT, paddingChar = '0') 117 | @FixedFormatDecimal(decimals = 4, decimalDelimiter = ' ', useDecimalDelimiter = true) 118 | @FixedFormatNumber(sign = Sign.PREPEND) 119 | public BigDecimal getBigDecimalData() { 120 | return bigDecimalData; 121 | } 122 | 123 | public void setBigDecimalData(BigDecimal bigDecimalData) { 124 | this.bigDecimalData = bigDecimalData; 125 | } 126 | 127 | @Field(offset = 60, length = 10, align = Align.RIGHT, paddingChar = '0') 128 | public float getSimpleFloatData() { 129 | return simpleFloatData; 130 | } 131 | 132 | public void setSimpleFloatData(float simpleFloatData) { 133 | this.simpleFloatData = simpleFloatData; 134 | } 135 | 136 | 137 | @Record 138 | static class MyStaticNestedClass { 139 | 140 | private String stringData; 141 | 142 | @Field(offset = 1, length = 10) 143 | public String getStringData() { 144 | return stringData; 145 | } 146 | 147 | public void setStringData(String stringData) { 148 | this.stringData = stringData; 149 | } 150 | } 151 | 152 | @Record 153 | class MyInnerClass { 154 | 155 | private String stringData; 156 | 157 | @Field(offset = 1, length = 10) 158 | public String getStringData() { 159 | return stringData; 160 | } 161 | 162 | public void setStringData(String stringData) { 163 | this.stringData = stringData; 164 | } 165 | } 166 | } -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/NoDefaultConstructorClass.java: -------------------------------------------------------------------------------- 1 | package com.ancientprogramming.fixedformat4j.format.impl; 2 | 3 | import com.ancientprogramming.fixedformat4j.annotation.Field; 4 | import com.ancientprogramming.fixedformat4j.annotation.Record; 5 | 6 | public class NoDefaultConstructorClass { 7 | 8 | private String someData; 9 | 10 | public NoDefaultConstructorClass(String someData) { 11 | this.someData = someData; 12 | } 13 | 14 | @Record 15 | static class MyStaticNestedClass { 16 | 17 | private String stringData; 18 | 19 | @Field(offset = 1, length = 10) 20 | public String getStringData() { 21 | return stringData; 22 | } 23 | 24 | public void setStringData(String stringData) { 25 | this.stringData = stringData; 26 | } 27 | } 28 | 29 | @Record 30 | class MyInnerClass { 31 | 32 | private String stringData; 33 | 34 | @Field(offset = 1, length = 10) 35 | public String getStringData() { 36 | return stringData; 37 | } 38 | 39 | public void setStringData(String stringData) { 40 | this.stringData = stringData; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestBooleanFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatBooleanData; 23 | import junit.framework.TestCase; 24 | 25 | /** 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | public class TestBooleanFormatter extends TestCase { 30 | 31 | FixedFormatter formatter = new BooleanFormatter(); 32 | 33 | public void testParse() { 34 | assertEquals(true, formatter.parse("T", new FormatInstructions(1, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 35 | assertEquals(false, formatter.parse("F ", new FormatInstructions(2, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 36 | assertEquals(false, formatter.parse(" ", new FormatInstructions(1, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 37 | try { 38 | formatter.parse("", new FormatInstructions(0, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null)); 39 | } catch (FixedFormatException e) { 40 | //expected as string is empty 41 | } 42 | } 43 | 44 | public void testFormat() { 45 | assertEquals("T", formatter.format(true, new FormatInstructions(1, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 46 | assertEquals("F", formatter.format(false, new FormatInstructions(1, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 47 | assertEquals("F", formatter.format(null, new FormatInstructions(1, Align.LEFT, ' ', null, new FixedFormatBooleanData("T", "F"), null, null))); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestCharacterFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import junit.framework.TestCase; 22 | 23 | /** 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.0.0 26 | */ 27 | public class TestCharacterFormatter extends TestCase { 28 | 29 | FixedFormatter formatter = new CharacterFormatter(); 30 | 31 | public void testParse() { 32 | assertEquals('J', formatter.parse("J", new FormatInstructions(1, Align.LEFT, ' ', null, null, null, null))); 33 | assertEquals('J', formatter.parse("JN", new FormatInstructions(2, Align.LEFT, ' ', null, null, null, null))); 34 | assertEquals(null, formatter.parse("", new FormatInstructions(0, Align.LEFT, ' ', null, null, null, null))); 35 | } 36 | 37 | public void testFormat() { 38 | assertEquals("J", formatter.format('J', new FormatInstructions(1, Align.LEFT, ' ', null, null, null, null))); 39 | assertEquals(" ", formatter.format(null, new FormatInstructions(1, Align.LEFT, ' ', null, null, null, null))); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestDateFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatPatternData; 22 | import junit.framework.Assert; 23 | import junit.framework.TestCase; 24 | 25 | import java.util.Calendar; 26 | import java.util.Date; 27 | 28 | /** 29 | * @author Jacob von Eyben - http://www.ancientprogramming.com 30 | * @since 1.0.0 31 | */ 32 | public class TestDateFormatter extends TestCase { 33 | 34 | public FixedFormatter formatter = new DateFormatter(); 35 | 36 | public void testParse() { 37 | Assert.assertEquals(getDate(1979, 10, 13), formatter.parse("13101979", new FormatInstructions(8, Align.LEFT, ' ', new FixedFormatPatternData("ddMMyyyy"), null, null, null))); 38 | Assert.assertEquals(null, formatter.parse(" ", new FormatInstructions(8, Align.LEFT, ' ', new FixedFormatPatternData("ddMMyyyy"), null, null, null))); 39 | } 40 | 41 | public void testFormat() { 42 | Assert.assertEquals("10032008", formatter.format(getDate(2008, 3, 10), new FormatInstructions(8, Align.LEFT, ' ', new FixedFormatPatternData("ddMMyyyy"), null, null, null))); 43 | Assert.assertEquals("08", formatter.format(getDate(2008, 3, 10), new FormatInstructions(2, Align.LEFT, ' ', new FixedFormatPatternData("yy"), null, null, null))); 44 | Assert.assertEquals(" ", formatter.format(null, new FormatInstructions(2, Align.LEFT, ' ', new FixedFormatPatternData("yy"), null, null, null))); 45 | } 46 | 47 | public Date getDate(int year, int month, int day) { 48 | Calendar cal = Calendar.getInstance(); 49 | cal.set(Calendar.YEAR, year); 50 | cal.set(Calendar.MONTH, month - 1); 51 | cal.set(Calendar.DAY_OF_MONTH, day); 52 | cal.set(Calendar.HOUR_OF_DAY, 0); 53 | cal.set(Calendar.MINUTE, 0); 54 | cal.set(Calendar.SECOND, 0); 55 | cal.set(Calendar.MILLISECOND, 0); 56 | return cal.getTime(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestDoubleFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 23 | import junit.framework.TestCase; 24 | 25 | import java.math.RoundingMode; 26 | 27 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_NEGATIVE_SIGN; 28 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_POSITIVE_SIGN; 29 | 30 | /** 31 | * @author Jacob von Eyben - http://www.ancientprogramming.com 32 | * @since 1.0.0 33 | */ 34 | public class TestDoubleFormatter extends TestCase { 35 | DoubleFormatter formatter = new DoubleFormatter(); 36 | 37 | public void testParse() { 38 | assertEquals(.01, formatter.parse("+000000001", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 39 | assertEquals(.05, formatter.parse("+000000005", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 40 | assertEquals(.15, formatter.parse("+000000015", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 41 | 42 | 43 | assertEquals(100.50, formatter.parse("+000010050", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 44 | assertEquals(1234.56, formatter.parse("+000123456", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 45 | 46 | //use delimiter 47 | assertEquals(1234.56, formatter.parse("+001234.56", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.UNNECESSARY)))); 48 | assertEquals(123.456, formatter.parse("+00123_456", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '_', RoundingMode.UNNECESSARY)))); 49 | 50 | //no decimals 51 | assertEquals(123456.0, formatter.parse("+000123456", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 52 | 53 | assertEquals(0.0, formatter.parse("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 54 | 55 | assertEquals(0.0, formatter.parse("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 56 | 57 | //negative number 58 | assertEquals(-100.50, formatter.parse("-000010050", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 59 | assertEquals(0.0, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 60 | assertEquals(0.0, formatter.parse("+000000.00", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.UNNECESSARY)))); 61 | } 62 | 63 | public void testFormat() { 64 | assertEquals("+000000000", formatter.format(new Double(0.00), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 65 | assertEquals("+000000001", formatter.format(new Double(0.01), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 66 | assertEquals("+000000050", formatter.format(new Double(0.5), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 67 | assertEquals("+000000050", formatter.format(new Double(0.5), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 68 | assertEquals("+000010050", formatter.format(new Double(100.5), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 69 | assertEquals("+000001005", formatter.format(new Double(100.51), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.', RoundingMode.HALF_UP)))); 70 | assertEquals("+000123456", formatter.format(new Double(1234.562), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.HALF_UP)))); 71 | 72 | assertEquals("+001234.56", formatter.format(new Double(1234.562), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.HALF_UP)))); 73 | assertEquals("+00123.456", formatter.format(new Double(123.4562), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.HALF_UP)))); 74 | 75 | assertEquals("+00000.000", formatter.format(new Double(0.0), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.UNNECESSARY)))); 76 | assertEquals("+00000.000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.UNNECESSARY)))); 77 | 78 | //the number is bigger than the total length 79 | assertEquals("+456789.01", formatter.format(new Double(123456789.01), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.HALF_UP)))); 80 | 81 | assertEquals("-000010050", formatter.format(new Double(-100.5), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 82 | } 83 | 84 | public void testFormatDoubleWith4DecimalsKeepAllDecimals() { 85 | assertEquals("+001005055", formatter.format(new Double(100.5055), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(4, false, '.', RoundingMode.HALF_UP)))); 86 | } 87 | 88 | public void testFormatDoubleWith5DecimalsTo4Decimals() { 89 | assertEquals("+001005556", formatter.format(new Double(100.55555), new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(4, false, '.', RoundingMode.HALF_UP)))); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestFixedFormatManagerImpl.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 19 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 20 | import com.ancientprogramming.fixedformat4j.format.ParseException; 21 | import junit.framework.Assert; 22 | import junit.framework.TestCase; 23 | import org.apache.commons.logging.Log; 24 | import org.apache.commons.logging.LogFactory; 25 | 26 | import java.math.BigDecimal; 27 | import java.util.Calendar; 28 | 29 | /** 30 | * @author Jacob von Eyben - http://www.ancientprogramming.com 31 | * @since 1.0.0 32 | */ 33 | public class TestFixedFormatManagerImpl extends TestCase { 34 | 35 | private static final Log LOG = LogFactory.getLog(TestFixedFormatManagerImpl.class); 36 | 37 | private static String STR = "some text "; 38 | 39 | public static final String MY_RECORD_DATA = "some text 0012320080514CT001100000010350000002056-0012 01200000002056"; 40 | public static final String MULTIBLE_RECORD_DATA = "some 2008101320081013 0100"; 41 | public static final String MULTIBLE_RECORD_DATA_X_PADDED = "some 2008101320081013xxxxxxxxxxxxxxxxxxxxxxx0100"; 42 | 43 | FixedFormatManager manager = null; 44 | 45 | @Override 46 | protected void setUp() throws Exception { 47 | super.setUp(); 48 | manager = new FixedFormatManagerImpl(); 49 | } 50 | 51 | public void testLoadRecord() { 52 | MyRecord loadedRecord = manager.load(MyRecord.class, MY_RECORD_DATA); 53 | Assert.assertNotNull(loadedRecord); 54 | Assert.assertEquals(STR, loadedRecord.getStringData()); 55 | Assert.assertTrue(loadedRecord.isBooleanData()); 56 | } 57 | 58 | public void testLoadMultibleFieldsRecord() { 59 | //when reading data having multible field annotations the first field will decide what data to return 60 | Calendar someDay = Calendar.getInstance(); 61 | someDay.set(2008, 9, 13, 0, 0, 0); 62 | someDay.set(Calendar.MILLISECOND, 0); 63 | MultibleFieldsRecord loadedRecord = manager.load(MultibleFieldsRecord.class, MULTIBLE_RECORD_DATA); 64 | Assert.assertNotNull(loadedRecord); 65 | Assert.assertEquals("some ", loadedRecord.getStringData()); 66 | Assert.assertEquals(someDay.getTime(), loadedRecord.getDateData()); 67 | } 68 | 69 | public void testExportRecordObject() { 70 | MyRecord myRecord = createMyRecord(); 71 | Assert.assertEquals("wrong record exported", MY_RECORD_DATA, manager.export(myRecord)); 72 | } 73 | 74 | public void testExportNestedRecordObject() { 75 | MyRecord myRecord = createMyRecord(); 76 | MyOtherRecord myOtherRecord = new MyOtherRecord(myRecord); 77 | Assert.assertEquals("wrong record exported", MY_RECORD_DATA, manager.export(myOtherRecord)); 78 | 79 | myOtherRecord = new MyOtherRecord((MyRecord) null); 80 | Assert.assertEquals("wrong record exported", "", manager.export(myOtherRecord)); 81 | } 82 | 83 | private MyRecord createMyRecord() { 84 | Calendar someDay = Calendar.getInstance(); 85 | someDay.set(2008, 4, 14, 0, 0, 0); 86 | someDay.set(Calendar.MILLISECOND, 0); 87 | 88 | MyRecord myRecord = new MyRecord(); 89 | myRecord.setBooleanData(true); 90 | myRecord.setCharData('C'); 91 | myRecord.setDateData(someDay.getTime()); 92 | myRecord.setDoubleData(10.35); 93 | myRecord.setFloatData(20.56F); 94 | myRecord.setLongData(11L); 95 | myRecord.setIntegerData(123); 96 | myRecord.setStringData("some text "); 97 | myRecord.setBigDecimalData(new BigDecimal(-12.012)); 98 | myRecord.setSimpleFloatData(20.56F); 99 | return myRecord; 100 | } 101 | 102 | public void testExportMultibleFieldRecordObject() { 103 | Calendar someDay = Calendar.getInstance(); 104 | someDay.set(2008, 9, 13, 0, 0, 0); 105 | someDay.set(Calendar.MILLISECOND, 0); 106 | 107 | MultibleFieldsRecord multibleFieldsRecord = new MultibleFieldsRecord(); 108 | multibleFieldsRecord.setDateData(someDay.getTime()); 109 | multibleFieldsRecord.setStringData("some "); 110 | multibleFieldsRecord.setIntegerdata(100); 111 | manager.export(multibleFieldsRecord); 112 | Assert.assertEquals("wrong record exported", MULTIBLE_RECORD_DATA, manager.export(multibleFieldsRecord)); 113 | } 114 | 115 | public void testExportIntoExistingString() { 116 | Calendar someDay = Calendar.getInstance(); 117 | someDay.set(2008, 9, 13, 0, 0, 0); 118 | someDay.set(Calendar.MILLISECOND, 0); 119 | 120 | MultibleFieldsRecord multibleFieldsRecord = new MultibleFieldsRecord(); 121 | multibleFieldsRecord.setDateData(someDay.getTime()); 122 | multibleFieldsRecord.setStringData("some "); 123 | multibleFieldsRecord.setIntegerdata(100); 124 | String exportedString = manager.export("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", multibleFieldsRecord); 125 | Assert.assertEquals("wrong record exported", MULTIBLE_RECORD_DATA_X_PADDED, exportedString); 126 | } 127 | 128 | public void testLoadNonRecordAnnotatedClass() { 129 | try { 130 | manager.load(String.class, "some"); 131 | } catch (FixedFormatException e) { 132 | //expected 133 | } 134 | } 135 | 136 | public void testExportAnnotatedNestedClass() { 137 | MyRecord.MyStaticNestedClass myStaticNestedClass = new MyRecord.MyStaticNestedClass(); 138 | myStaticNestedClass.setStringData("xyz"); 139 | String exportedString = manager.export(myStaticNestedClass); 140 | Assert.assertEquals("xyz ", exportedString); 141 | 142 | NoDefaultConstructorClass.MyStaticNestedClass myStaticNestedClass2 = new NoDefaultConstructorClass.MyStaticNestedClass(); 143 | myStaticNestedClass2.setStringData("xyz"); 144 | String exportedString2 = manager.export(myStaticNestedClass2); 145 | Assert.assertEquals("xyz ", exportedString2); 146 | } 147 | 148 | public void testExportAnnotatedInnerClass() { 149 | MyRecord myRecord = new MyRecord(); 150 | MyRecord.MyInnerClass myInnerClass = myRecord.new MyInnerClass(); 151 | myInnerClass.setStringData("xyz"); 152 | String exportedString = manager.export(myInnerClass); 153 | Assert.assertEquals("xyz ", exportedString); 154 | 155 | NoDefaultConstructorClass noDefaultConstructorClass = new NoDefaultConstructorClass("foobar"); 156 | NoDefaultConstructorClass.MyInnerClass myInnerClass2 = noDefaultConstructorClass.new MyInnerClass(); 157 | myInnerClass2.setStringData("xyz"); 158 | exportedString = manager.export(myInnerClass2); 159 | Assert.assertEquals("xyz ", exportedString); 160 | } 161 | 162 | public void testImportAnnotatedNestedClass() { 163 | MyRecord.MyStaticNestedClass staticNested = manager.load(MyRecord.MyStaticNestedClass.class, "xyz "); 164 | Assert.assertEquals("xyz", staticNested.getStringData()); 165 | 166 | NoDefaultConstructorClass.MyStaticNestedClass staticNested2 = manager.load(NoDefaultConstructorClass.MyStaticNestedClass.class, "xyz "); 167 | Assert.assertEquals("xyz", staticNested2.getStringData()); 168 | } 169 | 170 | public void testImportAnnotatedInnerClass() { 171 | MyRecord.MyInnerClass inner = manager.load(MyRecord.MyInnerClass.class, "xyz "); 172 | Assert.assertEquals("xyz", inner.getStringData()); 173 | 174 | 175 | try { 176 | manager.load(NoDefaultConstructorClass.MyInnerClass.class, "xyz "); 177 | fail(String.format("expected an %s exception to be thrown", FixedFormatException.class.getName())); 178 | } catch (FixedFormatException e) { 179 | //expected this 180 | } 181 | } 182 | 183 | public void testParseFail() { 184 | try { 185 | manager.load(MyRecord.class, "foobarfoobarfoobarfoobar"); 186 | fail("expected parse exception"); 187 | } catch (ParseException e) { 188 | if (LOG.isDebugEnabled()) { 189 | LOG.debug("bu", e); 190 | } 191 | //expected 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestFloatFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 20 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 21 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 23 | import junit.framework.TestCase; 24 | 25 | import java.math.RoundingMode; 26 | 27 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_NEGATIVE_SIGN; 28 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_POSITIVE_SIGN; 29 | 30 | /** 31 | * @author Jacob von Eyben - http://www.ancientprogramming.com 32 | * @since 1.0.0 33 | */ 34 | public class TestFloatFormatter extends TestCase { 35 | FloatFormatter formatter = new FloatFormatter(); 36 | 37 | public void testParse() { 38 | assertEquals(100.50F, formatter.parse("0000010050", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 39 | assertEquals(1234.56F, formatter.parse("0000123456", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 40 | 41 | //use delimiter 42 | assertEquals(1234.56F, formatter.parse("0001234.56", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, new FixedFormatDecimalData(2, true, '.', RoundingMode.UNNECESSARY)))); 43 | assertEquals(123.456F, formatter.parse("000123_456", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, new FixedFormatDecimalData(3, true, '_', RoundingMode.UNNECESSARY)))); 44 | 45 | //no decimals 46 | assertEquals(123456.0F, formatter.parse("0000123456", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 47 | 48 | assertEquals(0.0F, formatter.parse("0000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 49 | 50 | assertEquals(0.0F, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(0, true, '_', RoundingMode.UNNECESSARY)))); 51 | 52 | assertEquals(-100.50F, formatter.parse("-000010050", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 53 | } 54 | 55 | public void testFormat() { 56 | assertEquals("+000010050", formatter.format(100.5F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 57 | assertEquals("+000001005", formatter.format(100.51F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.', RoundingMode.HALF_UP)))); 58 | assertEquals("+000123456", formatter.format(1234.562F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.HALF_UP)))); 59 | 60 | assertEquals("+001234.56", formatter.format(1234.562F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.HALF_UP)))); 61 | assertEquals("+00123.456", formatter.format(123.4562F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.HALF_UP)))); 62 | 63 | assertEquals("+00000.000", formatter.format(0.0F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.UNNECESSARY)))); 64 | assertEquals("+00000.000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(3, true, '.', RoundingMode.UNNECESSARY)))); 65 | 66 | //the number is bigger than the total length 67 | assertEquals("+45.01", formatter.format(12345.01F, new FormatInstructions(6, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, true, '.', RoundingMode.HALF_UP)))); 68 | 69 | assertEquals("-000010050", formatter.format(-100.5F, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 70 | } 71 | } -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestIntegerFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 23 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 24 | import junit.framework.TestCase; 25 | 26 | import java.math.RoundingMode; 27 | 28 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_NEGATIVE_SIGN; 29 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_POSITIVE_SIGN; 30 | 31 | /** 32 | * @author Jacob von Eyben - http://www.ancientprogramming.com 33 | * @since 1.0.0 34 | */ 35 | public class TestIntegerFormatter extends TestCase { 36 | 37 | private FixedFormatter formatter = new IntegerFormatter(); 38 | 39 | public void testParse() { 40 | assertEquals(100, formatter.parse("0000000100", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 41 | assertEquals(100, formatter.parse("100", new FormatInstructions(3, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 42 | assertEquals(1, formatter.parse("01", new FormatInstructions(2, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 43 | assertEquals(1234, formatter.parse("0000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 44 | assertEquals(0, formatter.parse("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 45 | assertEquals(0, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 46 | assertEquals(-1234, formatter.parse("-000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 47 | } 48 | 49 | public void testFormat() { 50 | assertEquals("+000000100", formatter.format(100, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 51 | assertEquals("+000000101", formatter.format(101, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.', RoundingMode.UNNECESSARY)))); 52 | assertEquals("+000001234", formatter.format(1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 53 | assertEquals("-000001234", formatter.format(-1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 54 | assertEquals("+000000000", formatter.format(0, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 55 | assertEquals("+000000000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestLongFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 23 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 24 | import junit.framework.TestCase; 25 | 26 | import java.math.RoundingMode; 27 | 28 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_NEGATIVE_SIGN; 29 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_POSITIVE_SIGN; 30 | 31 | /** 32 | * 33 | * @author Jacob von Eyben - http://www.ancientprogramming.com 34 | * @since 1.0.0 35 | */ 36 | public class TestLongFormatter extends TestCase { 37 | 38 | private FixedFormatter formatter = new LongFormatter(); 39 | 40 | public void testParse() { 41 | assertEquals(100L, formatter.parse("0000000100", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 42 | assertEquals(1234L, formatter.parse("000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 43 | assertEquals(0L, formatter.parse("000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 44 | assertEquals(0L, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 45 | assertEquals(-1234L, formatter.parse("-000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 46 | } 47 | 48 | public void testFormat() { 49 | assertEquals("+000000100", formatter.format(100L, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 50 | assertEquals("+000000101", formatter.format(101L, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.', RoundingMode.UNNECESSARY)))); 51 | assertEquals("+000001234", formatter.format(1234L, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 52 | assertEquals("-000001234", formatter.format(-1234L, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 53 | assertEquals("+000000000", formatter.format(0L, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 54 | assertEquals("+000000000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestShortFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Sign; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 22 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatDecimalData; 23 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatNumberData; 24 | import junit.framework.TestCase; 25 | 26 | import java.math.RoundingMode; 27 | 28 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_NEGATIVE_SIGN; 29 | import static com.ancientprogramming.fixedformat4j.annotation.FixedFormatNumber.DEFAULT_POSITIVE_SIGN; 30 | 31 | /** 32 | * @author Jacob von Eyben - http://www.ancientprogramming.com 33 | * @since 1.3.0 34 | */ 35 | public class TestShortFormatter extends TestCase { 36 | 37 | private FixedFormatter formatter = new ShortFormatter(); 38 | 39 | public void testParse() { 40 | assertEquals((short) 100, formatter.parse("0000000100", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 41 | assertEquals((short) 100, formatter.parse("100", new FormatInstructions(3, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 42 | assertEquals((short) 1, formatter.parse("01", new FormatInstructions(2, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 43 | assertEquals((short) 1234, formatter.parse("0000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, FixedFormatNumberData.DEFAULT, null))); 44 | assertEquals((short) 0, formatter.parse("+000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 45 | assertEquals((short) 0, formatter.parse("-000000000", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 46 | assertEquals((short) -1234, formatter.parse("-000001234", new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), null))); 47 | } 48 | 49 | public void testFormat() { 50 | assertEquals("+000000100", formatter.format((short) 100, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 51 | assertEquals("+000000101", formatter.format((short) 101, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(1, false, '.', RoundingMode.UNNECESSARY)))); 52 | assertEquals("+000001234", formatter.format((short) 1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 53 | assertEquals("-000001234", formatter.format((short) -1234, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 54 | assertEquals("+000000000", formatter.format((short) 0, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 55 | assertEquals("+000000000", formatter.format(null, new FormatInstructions(10, Align.RIGHT, '0', null, null, new FixedFormatNumberData(Sign.PREPEND, DEFAULT_POSITIVE_SIGN, DEFAULT_NEGATIVE_SIGN), new FixedFormatDecimalData(2, false, '.', RoundingMode.UNNECESSARY)))); 56 | } 57 | } -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/format/impl/TestStringFormatter.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.impl; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.format.FormatInstructions; 20 | import com.ancientprogramming.fixedformat4j.format.FixedFormatter; 21 | import com.ancientprogramming.fixedformat4j.format.data.FixedFormatPatternData; 22 | import junit.framework.Assert; 23 | import junit.framework.TestCase; 24 | 25 | /** 26 | * @author Jacob von Eyben - http://www.ancientprogramming.com 27 | * @since 1.0.0 28 | */ 29 | public class TestStringFormatter extends TestCase { 30 | 31 | private FixedFormatter formatter = new StringFormatter(); 32 | 33 | public void testParse() { 34 | Assert.assertEquals("string", formatter.parse("string ", new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 35 | Assert.assertEquals("s", formatter.parse("s", new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 36 | Assert.assertEquals("", formatter.parse("", new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 37 | } 38 | 39 | public void testFormat() { 40 | Assert.assertEquals(" ", formatter.format(null, new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 41 | Assert.assertEquals(" ", formatter.format("", new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 42 | Assert.assertEquals("a string i", formatter.format("a string is too long", new FormatInstructions(10, Align.LEFT, ' ', null, null, null, null))); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/issues/TestIssue10.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.issues; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 19 | import com.ancientprogramming.fixedformat4j.format.ParseException; 20 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 21 | import com.ancientprogramming.fixedformat4j.annotation.Record; 22 | import com.ancientprogramming.fixedformat4j.annotation.Field; 23 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 24 | import com.ancientprogramming.fixedformat4j.annotation.Align; 25 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; 26 | 27 | import java.util.Date; 28 | 29 | import junit.framework.TestCase; 30 | import org.junit.Test; 31 | 32 | /** 33 | * Verifies Issue 10 - parse exception contains details for better error reporting posibilities 34 | * 35 | * @author Jacob von Eyben - http://www.ancientprogramming.com 36 | * @since 1.3.0 37 | */ 38 | public class TestIssue10 extends TestCase { 39 | 40 | FixedFormatManager fixedFormatManager = new FixedFormatManagerImpl(); 41 | 42 | @Test 43 | public void testParseExceptionContainsDetails() { 44 | 45 | String text = "some 20081021123f5foobar"; 46 | try { 47 | fixedFormatManager.load(Issue10.class, text); 48 | fail("expected a parseexception"); 49 | } catch (ParseException e) { 50 | assertEquals(Issue10.class, e.getAnnotatedClass()); 51 | assertEquals("getNumber", e.getAnnotatedMethod().getName()); 52 | assertEquals(text, e.getCompleteText()); 53 | assertEquals("123f5", e.getFailedText()); 54 | assertEquals(int.class, e.getFormatContext().getDataType()); 55 | assertEquals(5, e.getFormatInstructions().getLength()); 56 | } 57 | } 58 | 59 | @Record 60 | public static class Issue10 { 61 | 62 | private String text; 63 | private Date date; 64 | private int number; 65 | private String text2; 66 | 67 | @Field(offset = 1, length = 5) 68 | public String getText() { 69 | return text; 70 | } 71 | 72 | public void setText(String text) { 73 | this.text = text; 74 | } 75 | 76 | @Field(offset = 6, length = 8) 77 | @FixedFormatPattern("yyyyMMdd") 78 | public Date getDate() { 79 | return date; 80 | } 81 | 82 | public void setDate(Date date) { 83 | this.date = date; 84 | } 85 | 86 | @Field(offset = 14, length = 5, paddingChar = '0', align = Align.RIGHT) 87 | public int getNumber() { 88 | return number; 89 | } 90 | 91 | public void setNumber(int number) { 92 | this.number = number; 93 | } 94 | 95 | @Field(offset = 19, length = 5) 96 | public String getText2() { 97 | return text2; 98 | } 99 | 100 | public void setText2(String text2) { 101 | this.text2 = text2; 102 | } 103 | } 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/issues/TestIssue7.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.issues; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Record; 21 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 22 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 23 | import junit.framework.TestCase; 24 | import org.junit.Test; 25 | 26 | /** 27 | * Verifies Issue 7 - record contains other fixedformatted records 28 | * 29 | * @author Jacob von Eyben - http://www.ancientprogramming.com 30 | * @since 1.3.0 31 | */ 32 | public class TestIssue7 extends TestCase { 33 | 34 | FixedFormatManager fixedFormatManager = new FixedFormatManagerImpl(); 35 | 36 | @Test 37 | public void testLoadedNestedRecords() { 38 | String text20 = "0123402345foo bar "; 39 | 40 | Issue7 issue7 = fixedFormatManager.load(Issue7.class, text20); 41 | assertEquals(1234, issue7.getNumber()); 42 | assertEquals(2345, issue7.getNestedIssue7().getNumber()); 43 | assertEquals("foo", issue7.getNestedIssue7().getString()); 44 | assertEquals("bar", issue7.getString()); 45 | } 46 | 47 | @Test 48 | public void testExportNestedRecords() { 49 | String text20 = "0123402345foo bar "; 50 | 51 | NestedIssue7 nestedIssue7 = new NestedIssue7(); 52 | nestedIssue7.setString("foo"); 53 | nestedIssue7.setNumber(2345); 54 | 55 | Issue7 issue7 = new Issue7(); 56 | issue7.setNumber(1234); 57 | issue7.setNestedIssue7(nestedIssue7); 58 | issue7.setString("bar"); 59 | 60 | String actual = fixedFormatManager.export(issue7); 61 | assertEquals(text20, actual); 62 | } 63 | 64 | 65 | @Record 66 | public static class Issue7 { 67 | 68 | private int number; 69 | private NestedIssue7 nestedIssue7; 70 | private String string; 71 | 72 | @Field(offset = 1, length = 5, align = Align.RIGHT, paddingChar = '0') 73 | public int getNumber() { 74 | return number; 75 | } 76 | 77 | public void setNumber(int number) { 78 | this.number = number; 79 | } 80 | 81 | @Field(offset = 6, length = 10) 82 | public NestedIssue7 getNestedIssue7() { 83 | return nestedIssue7; 84 | } 85 | 86 | public void setNestedIssue7(NestedIssue7 nestedIssue7) { 87 | this.nestedIssue7 = nestedIssue7; 88 | } 89 | 90 | @Field(offset = 16, length = 5) 91 | public String getString() { 92 | return string; 93 | } 94 | 95 | public void setString(String string) { 96 | this.string = string; 97 | } 98 | } 99 | 100 | @Record 101 | public static class NestedIssue7 { 102 | 103 | private int number; 104 | private String string; 105 | 106 | @Field(offset = 1, length = 5, align = Align.RIGHT, paddingChar = '0') 107 | public int getNumber() { 108 | return number; 109 | } 110 | 111 | public void setNumber(int number) { 112 | this.number = number; 113 | } 114 | 115 | @Field(offset = 6, length = 5) 116 | public String getString() { 117 | return string; 118 | } 119 | 120 | public void setString(String string) { 121 | this.string = string; 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /fixedformat4j/src/test/java/com/ancientprogramming/fixedformat4j/issues/TestIssue9.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.issues; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Align; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Record; 21 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 22 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 23 | import junit.framework.TestCase; 24 | import org.junit.Test; 25 | 26 | /** 27 | * Verifies Issue 9 28 | * 29 | * @author Jacob von Eyben - http://www.ancientprogramming.com 30 | * @since 1.2.1 31 | */ 32 | public class TestIssue9 extends TestCase { 33 | 34 | FixedFormatManager fixedFormatManager = new FixedFormatManagerImpl(); 35 | 36 | @Test 37 | public void testLastColumnIsIgnored() { 38 | String text21 = "1234567890some "; 39 | String text20 = "1234567890some "; 40 | String text19 = "1234567890some "; 41 | String text11 = "1234567890x"; 42 | String text10 = "1234567890"; 43 | String text9 = "123456789"; 44 | 45 | assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text21)); 46 | assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text20)); 47 | assertText(1234567890, "some", fixedFormatManager.load(Issue9.class, text19)); 48 | assertText(1234567890, "x", fixedFormatManager.load(Issue9.class, text11)); 49 | assertText(1234567890, null, fixedFormatManager.load(Issue9.class, text10)); 50 | assertText(123456789, null, fixedFormatManager.load(Issue9.class, text9)); 51 | 52 | } 53 | 54 | private void assertText(int expectedNumber, String expectedString, Issue9 actual) { 55 | assertEquals(expectedNumber, actual.getNumber()); 56 | assertEquals(expectedString, actual.getString()); 57 | } 58 | 59 | 60 | @Record 61 | public static class Issue9 { 62 | 63 | private int number; 64 | private String string; 65 | 66 | 67 | 68 | @Field(offset = 1, length = 10, align = Align.RIGHT, paddingChar = '0') 69 | public int getNumber() { 70 | return number; 71 | } 72 | 73 | public void setNumber(int number) { 74 | this.number = number; 75 | } 76 | 77 | 78 | @Field(offset = 11, length = 10) 79 | public String getString() { 80 | return string; 81 | } 82 | 83 | public void setString(String string) { 84 | this.string = string; 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /fixedformat4j/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | Fixed Format for Java - root 6 | com.ancientprogramming.fixedformat4j 7 | 1.4.0-SNAPSHOT 8 | fixedformat4j-root 9 | pom 10 | 11 | 2008 12 | 13 | ancientprogramming 14 | http://www.ancientprogramming.com 15 | 16 | 17 | 18 | 19 | http://www.ancientprogramming.com/fixedformat4j 20 | 21 | 22 | 2.0.7 23 | 24 | 25 | 26 | scm:svn:http://fixedformat4j.googlecode.com/svn/trunk 27 | scm:svn:https://fixedformat4j.googlecode.com/svn/trunk 28 | http://code.google.com/p/fixedformat4j/source/browse/trunk 29 | 30 | 31 | 32 | 33 | Jacob von Eyben 34 | jeyben 35 | jacobvoneyben@gmail.com 36 | ancientprogramming.com 37 | 38 | 39 | 40 | 41 | fixedformat4j 42 | samples 43 | 44 | 45 | 46 | install 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-release-plugin 51 | 52 | https://fixedformat4j.googlecode.com/svn/tags 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /samples/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | Fixed Format for Java Samples 6 | com.ancientprogramming.fixedformat4j 7 | 8 | 1.4.0-SNAPSHOT 9 | fixedformat4j-samples 10 | jar 11 | 12 | 2008 13 | 14 | ancientprogramming.com 15 | http://www.ancientprogramming.com 16 | 17 | 18 | 19 | 20 | http://fixedformat4j.ancientprogramming.com 21 | 22 | 23 | 2.0.7 24 | 25 | 26 | 27 | 28 | The Apache Software License, Version 2.0 29 | http://www.apache.org/licenses/LICENSE-2.0.txt 30 | repo 31 | 32 | 33 | 34 | 35 | scm:svn:http://fixedformat4j.googlecode.com/svn/trunk/samples 36 | scm:svn:https://fixedformat4j.googlecode.com/svn/trunk/samples 37 | http://code.google.com/p/fixedformat4j/source/browse/trunk/samples 38 | 39 | 40 | 41 | 42 | Jacob von Eyben 43 | jeyben 44 | jacobvoneyben@gmail.com 45 | 46 | 47 | http://www.ancientprogramming.com 48 | 2 49 | 50 | Developer 51 | 52 | 53 | 54 | 55 | 56 | 57 | ancientprogramming 58 | scp://ancientprogramming.com/home/ancientp/www/fixedformat4j 59 | 60 | 61 | 62 | 63 | 64 | ${project.groupId} 65 | fixedformat4j 66 | 1.4.0-SNAPSHOT 67 | 68 | 69 | 70 | 71 | install 72 | 73 | 74 | org.apache.maven.plugins 75 | maven-compiler-plugin 76 | 77 | 1.8 78 | 1.8 79 | UTF-8 80 | true 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-release-plugin 86 | 87 | https://fixedformat4j.googlecode.com/svn/tags/samples 88 | 89 | 90 | 91 | org.apache.maven.plugins 92 | maven-assembly-plugin 93 | 94 | 95 | bin 96 | src 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic/BasicRecord.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.samples.basic; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Record; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Align; 21 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 22 | 23 | import java.util.Date; 24 | 25 | /** 26 | * A record containing some simple datatypes to show basic parsing and formatting. 27 | * 28 | * @author Jacob von Eyben - http://www.ancientprogramming.com 29 | * @since 1.2.0 30 | */ 31 | //START-SNIPPET: basicrecord 32 | @Record 33 | public class BasicRecord { 34 | 35 | private String stringData; 36 | private Integer integerData; 37 | private Date dateData; 38 | 39 | 40 | @Field(offset = 1, length = 10) 41 | public String getStringData() { 42 | return stringData; 43 | } 44 | 45 | public void setStringData(String stringData) { 46 | this.stringData = stringData; 47 | } 48 | 49 | @Field(offset = 11, length = 5, align = Align.RIGHT, paddingChar = '0') 50 | public Integer getIntegerData() { 51 | return integerData; 52 | } 53 | 54 | public void setIntegerData(Integer integerData) { 55 | this.integerData = integerData; 56 | } 57 | 58 | @Field(offset = 16, length = 10) 59 | @FixedFormatPattern("yyyy-MM-dd") 60 | public Date getDateData() { 61 | return dateData; 62 | } 63 | 64 | public void setDateData(Date dateData) { 65 | this.dateData = dateData; 66 | } 67 | } 68 | //END-SNIPPET: basicrecord 69 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/basic/BasicUsage.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.samples.basic; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 19 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 20 | 21 | /** 22 | * Shows the basic usage 23 | * 24 | * @author Jacob von Eyben - http://www.ancientprogramming.com 25 | * @since 1.2.0 26 | */ 27 | //START-SNIPPET: basicusage 28 | public class BasicUsage { 29 | 30 | private static FixedFormatManager manager = new FixedFormatManagerImpl(); 31 | 32 | public static void main(String[] args) { 33 | String string = "string 001232008-05-29"; 34 | BasicRecord record = manager.load(BasicRecord.class, string); 35 | 36 | System.out.println("The parsed string: " + record.getStringData()); 37 | System.out.println("The parsed integer: " + record.getIntegerData()); 38 | System.out.println("The parsed date: " + record.getDateData()); 39 | 40 | record.setIntegerData(100); 41 | System.out.println("Exported: " + manager.export(record)); 42 | } 43 | } 44 | //END-SNIPPET: basicusage 45 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/BasicUsageManager.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.samples.usage; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 19 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 20 | import com.ancientprogramming.fixedformat4j.samples.basic.BasicRecord; 21 | 22 | /** 23 | * Shows the basic usage 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.2.0 27 | */ 28 | public class BasicUsageManager { 29 | 30 | public static void main(String[] args) { 31 | 32 | //START-SNIPPET: basicusagemanager 33 | FixedFormatManager manager = new FixedFormatManagerImpl(); 34 | 35 | //load the string into an object representation 36 | BasicUsageRecord record = manager.load(BasicUsageRecord.class, "initial string"); 37 | 38 | //operate the record in object representation 39 | record.setStringData("some other string"); 40 | 41 | //export back to string representation 42 | String exportedString = manager.export(record); 43 | //... do something with the new string 44 | //END-SNIPPET: basicusagemanager 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/BasicUsageRecord.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.samples.usage; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Record; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Align; 21 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 22 | 23 | import java.util.Date; 24 | 25 | /** 26 | * A record containing some simple datatypes to show basic parsing and formatting. 27 | * 28 | * @author Jacob von Eyben - http://www.ancientprogramming.com 29 | * @since 1.2.0 30 | */ 31 | //START-SNIPPET: basicusage 32 | @Record 33 | public class BasicUsageRecord { 34 | 35 | private String stringData; 36 | 37 | @Field(offset = 1, length = 35) 38 | public String getStringData() { 39 | return stringData; 40 | } 41 | 42 | public void setStringData(String stringData) { 43 | this.stringData = stringData; 44 | } 45 | } 46 | //END-SNIPPET: basicusage 47 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/NestedRecord.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.samples.usage; 17 | 18 | import com.ancientprogramming.fixedformat4j.annotation.Record; 19 | import com.ancientprogramming.fixedformat4j.annotation.Field; 20 | import com.ancientprogramming.fixedformat4j.annotation.Align; 21 | import com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern; 22 | import com.ancientprogramming.fixedformat4j.samples.basic.BasicRecord; 23 | 24 | import java.util.Date; 25 | 26 | 27 | /** 28 | * A record containing a simple datatype as well as a nested record annotated datatype. 29 | * 30 | * @author Jacob von Eyben - http://www.ancientprogramming.com 31 | * @since 1.3.0 32 | */ 33 | //START-SNIPPET: nestedrecord 34 | @Record 35 | public class NestedRecord { 36 | 37 | private String stringData; 38 | private BasicRecord record; //this instance is @Record annotated 39 | 40 | @Field(offset = 1, length = 5) 41 | public String getStringData() { 42 | return stringData; 43 | } 44 | 45 | public void setStringData(String stringData) { 46 | this.stringData = stringData; 47 | } 48 | 49 | @Field(offset = 6, length = 25) 50 | public BasicRecord getRecord() { 51 | return record; 52 | } 53 | 54 | public void setRecord(BasicRecord record) { 55 | this.record = record; 56 | } 57 | } 58 | //END-SNIPPET: nestedrecord 59 | -------------------------------------------------------------------------------- /samples/src/main/java/com/ancientprogramming/fixedformat4j/samples/usage/NestedRecordUsage.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.samples.usage; 17 | 18 | import com.ancientprogramming.fixedformat4j.format.FixedFormatManager; 19 | import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl; 20 | import com.ancientprogramming.fixedformat4j.samples.basic.BasicRecord; 21 | 22 | /** 23 | * Shows how to access nested record data 24 | * 25 | * @author Jacob von Eyben - http://www.ancientprogramming.com 26 | * @since 1.3.0 27 | */ 28 | public class NestedRecordUsage { 29 | 30 | public static void main(String[] args) { 31 | 32 | //START-SNIPPET: nestedrecordusage 33 | FixedFormatManager manager = new FixedFormatManagerImpl(); 34 | 35 | //load the string into an object representation 36 | String text = "foo bar 001232008-10-21"; 37 | System.out.println(text); 38 | NestedRecord record = manager.load(NestedRecord.class, text); 39 | 40 | //print the output 41 | System.out.println(record.getStringData()); 42 | BasicRecord nestedRecord = record.getRecord(); 43 | System.out.println(nestedRecord.getStringData()); 44 | System.out.println(nestedRecord.getIntegerData()); 45 | System.out.println(nestedRecord.getDateData()); 46 | 47 | //modify the nested record and export 48 | nestedRecord.setStringData("fubar"); 49 | nestedRecord.setIntegerData(9876); 50 | 51 | String exportedString = manager.export(record); 52 | System.out.println(exportedString); 53 | //END-SNIPPET: nestedrecordusage 54 | } 55 | } -------------------------------------------------------------------------------- /samples/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /todo.txt: -------------------------------------------------------------------------------- 1 | Bug 2 | ------------------------------------------ 3 | - ensure writing null values should not print null as string 4 | - ensure that we can read a string that is to short for the offsets defined 5 | 6 | Features 7 | ------------------------------------------ 8 | - Let the manager answer on format/parse errors. 9 | - support round, ceil, floor etc. when handling decimal data. 10 | - specific exception for errors that occurs because of wrong annotation usage 11 | - Having Beloeb and OreBeloeb, KrBeloeb it would be nice to be able to represent the setters and getters as Beloeb and use a specific formatter for the two supclasses: 12 | 13 | 14 | 15 | @Field(offset = 18, length = 5, align = Align.RIGHT, paddingChar = '0', formatter = OereBeloebFormatter.class) 16 | public Beloeb getTimepris() { 17 | return timepris; 18 | } 19 | 20 | public void setTimepris(OereBeloeb timepris) { 21 | this.timepris = timepris; 22 | } 23 | 24 | @Field(offset = 38, length = 9, align = Align.RIGHT, paddingChar = '0', formatter = OereBeloebFormatter.class) 25 | public Beloeb getTotalBeloeb() { 26 | return totalBeloeb; 27 | } 28 | 29 | public void setTotalBeloeb(OereBeloeb totalBeloeb) { //<-- This should be a Bleob and not a specific OereBeloeb 30 | this.totalBeloeb = totalBeloeb; 31 | } 32 | 33 | public abstract class Beloeb implements Serializable, Comparable { 34 | 35 | private Long oereBeloeb; 36 | 37 | protected Beloeb() { 38 | } 39 | 40 | protected Long getOereBeloeb() { 41 | return oereBeloeb; 42 | } 43 | 44 | protected void setOereBeloeb(Long oereBeloeb) { 45 | this.oereBeloeb = oereBeloeb; 46 | } 47 | 48 | public double asKr() { 49 | return oereBeloeb / 100.0; 50 | } 51 | 52 | public long asOere() { 53 | return oereBeloeb; 54 | } 55 | 56 | public String formattedTwoDecimals() { 57 | return NumberUtil.formatAsDecimal(asKr()); 58 | } 59 | 60 | public int compareTo(Beloeb b) { 61 | return oereBeloeb.compareTo(b.oereBeloeb); 62 | } 63 | 64 | public boolean lessThan(Beloeb beloeb) { 65 | return compareTo(beloeb) < 0; 66 | } 67 | 68 | public boolean greaterThan(Beloeb beloeb) { 69 | return compareTo(beloeb) > 0; 70 | } 71 | 72 | public boolean equals(Object o) { 73 | if (this == o) return true; 74 | if (!(o instanceof Beloeb)) return false; 75 | 76 | Beloeb beloeb = (Beloeb) o; 77 | 78 | return compareTo(beloeb) == 0; 79 | } 80 | 81 | public int hashCode() { 82 | return (oereBeloeb != null ? oereBeloeb.hashCode() : 0); 83 | } 84 | } 85 | 86 | @Embeddable 87 | public class OereBeloeb extends Beloeb { 88 | 89 | protected OereBeloeb() { 90 | } 91 | 92 | public OereBeloeb(Integer oerebeloeb) { 93 | setBeloeb((long) oerebeloeb); 94 | } 95 | 96 | public OereBeloeb(Long oerebeloeb) { 97 | setBeloeb(oerebeloeb); 98 | } 99 | 100 | protected Long getBeloeb() { 101 | return super.getOereBeloeb(); 102 | } 103 | 104 | protected void setBeloeb(Long beloeb) { 105 | super.setOereBeloeb(beloeb); 106 | } 107 | 108 | @Override 109 | public String toString() { 110 | return String.format("OereBeloeb [ore = %d]", getOereBeloeb()); 111 | } 112 | } 113 | 114 | @Embeddable 115 | public class KrBeloeb extends Beloeb { 116 | 117 | protected KrBeloeb() { 118 | } 119 | 120 | public KrBeloeb(Integer beloeb) { 121 | setBeloeb(beloeb.longValue()); 122 | } 123 | 124 | protected Long getBeloeb() { 125 | return super.getOereBeloeb() / 100; 126 | } 127 | 128 | protected void setBeloeb(Long beloeb) { 129 | super.setOereBeloeb(beloeb * 100); 130 | } 131 | 132 | @Override 133 | public String toString() { 134 | return String.format("KrBeloeb [ore = %d]", getOereBeloeb()); 135 | } 136 | } 137 | 138 | 139 | Site 140 | ------------------------------------------ 141 | - Include reports 142 | - Include http://cobertura.sourceforge.net/ 143 | - document --------------------------------------------------------------------------------