Refactor formatter framework
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / format / occurrences / AbstractCdmFormatter.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.format.occurrences;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import eu.etaxonomy.cdm.format.ICdmFormatter;
16
17 /**
18 * @author pplitzner
19 * @date Nov 30, 2015
20 *
21 */
22 public abstract class AbstractCdmFormatter implements ICdmFormatter {
23
24 protected FormatKey[] formatKeys;
25
26 protected Map<FormatKey, String> formatKeyMap = new HashMap<ICdmFormatter.FormatKey, String>();
27
28 public AbstractCdmFormatter(Object object, FormatKey[] formatKeys) {
29 this.formatKeys = formatKeys;
30 initFormatKeys(object);
31 }
32
33 @Override
34 public String format(Object object, FormatKey... formatKeys) {
35 StringBuilder builder = new StringBuilder();
36 for (FormatKey formatKey : formatKeys) {
37 String string = formatKeyMap.get(formatKey);
38 if(string!=null){
39 builder.append(string);
40 }
41 }
42 return builder.toString().trim();
43 }
44
45 @Override
46 public String format(Object object) {
47 StringBuilder builder = new StringBuilder();
48 for (FormatKey formatKey : formatKeys) {
49 String string = formatKeyMap.get(formatKey);
50 if(string!=null){
51 builder.append(string);
52 }
53 }
54 return builder.toString().trim();
55 }
56
57 protected void initFormatKeys(Object object){
58 formatKeyMap.put(FormatKey.CLOSE_BRACKET, ")");
59 formatKeyMap.put(FormatKey.OPEN_BRACKET, "(");
60 formatKeyMap.put(FormatKey.SPACE, " ");
61 formatKeyMap.put(FormatKey.COMMA, ",");
62 }
63
64 }