Refactor formatter framework
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / format / occurrences / AbstractCdmFormatter.java
index 256f92039e53a4cde75ceabddef758e17a70d9ed..5c12581ec63fba698d547160b532e45f2575c964 100644 (file)
@@ -21,10 +21,13 @@ import eu.etaxonomy.cdm.format.ICdmFormatter;
  */
 public abstract class AbstractCdmFormatter implements ICdmFormatter {
 
+    protected FormatKey[] formatKeys;
+    
     protected Map<FormatKey, String> formatKeyMap = new HashMap<ICdmFormatter.FormatKey, String>();
 
     public AbstractCdmFormatter(Object object, FormatKey[] formatKeys) {
-        initFormatKeys(object, formatKeys);
+       this.formatKeys = formatKeys;
+        initFormatKeys(object);
     }
 
     @Override
@@ -38,8 +41,20 @@ public abstract class AbstractCdmFormatter implements ICdmFormatter {
         }
         return builder.toString().trim();
     }
+    
+    @Override
+    public String format(Object object) {
+        StringBuilder builder = new StringBuilder();
+        for (FormatKey formatKey : formatKeys) {
+            String string = formatKeyMap.get(formatKey);
+            if(string!=null){
+                builder.append(string);
+            }
+        }
+        return builder.toString().trim();
+    }
 
-    protected void initFormatKeys(Object object, FormatKey... formatKeys){
+    protected void initFormatKeys(Object object){
         formatKeyMap.put(FormatKey.CLOSE_BRACKET, ")");
         formatKeyMap.put(FormatKey.OPEN_BRACKET, "(");
         formatKeyMap.put(FormatKey.SPACE, " ");