View Javadoc

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<Character> {
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  }