001/*
002 * Copyright 2009-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.item.file.transform;
017
018import java.text.DateFormat;
019import java.text.NumberFormat;
020
021/**
022 * Default implementation of {@link FieldSetFactory} with no special knowledge
023 * of the {@link FieldSet} required. Returns a {@link DefaultFieldSet} from both
024 * factory methods.
025 * 
026 * @author Dave Syer
027 * 
028 */
029public class DefaultFieldSetFactory implements FieldSetFactory {
030
031        private DateFormat dateFormat;
032
033        private NumberFormat numberFormat;
034
035        /**
036         * The {@link NumberFormat} to use for parsing numbers. If unset the default
037         * locale will be used.
038         * @param numberFormat the {@link NumberFormat} to use for number parsing
039         */
040        public void setNumberFormat(NumberFormat numberFormat) {
041                this.numberFormat = numberFormat;
042        }
043
044        /**
045         * The {@link DateFormat} to use for parsing numbers. If unset the default
046         * pattern is ISO standard <code>yyyy/MM/dd</code>.
047         * @param dateFormat the {@link DateFormat} to use for date parsing
048         */
049        public void setDateFormat(DateFormat dateFormat) {
050                this.dateFormat = dateFormat;
051        }
052
053        /**
054         * {@inheritDoc}
055         */
056    @Override
057        public FieldSet create(String[] values, String[] names) {
058                DefaultFieldSet fieldSet = new DefaultFieldSet(values, names);
059                return enhance(fieldSet);
060        }
061
062        /**
063         * {@inheritDoc}
064         */
065    @Override
066        public FieldSet create(String[] values) {
067                DefaultFieldSet fieldSet = new DefaultFieldSet(values);
068                return enhance(fieldSet);
069        }
070
071        private FieldSet enhance(DefaultFieldSet fieldSet) {
072                if (dateFormat!=null) {
073                        fieldSet.setDateFormat(dateFormat);
074                }
075                if (numberFormat!=null) {
076                        fieldSet.setNumberFormat(numberFormat);
077                }       
078                return fieldSet;
079        }
080
081}