From 16f965a13f8d57c964279b1e05bcf2c55a1ba229 Mon Sep 17 00:00:00 2001 From: "p.plitzner" Date: Mon, 22 Feb 2016 16:22:44 +0100 Subject: [PATCH] Refactor formatter framework - Simplify interface - added javadoc - added static methods --- .../eu/etaxonomy/cdm/format/CdmFormatter.java | 69 ----------- .../cdm/format/CdmFormatterFactory.java | 113 ++++++++++++++++++ .../etaxonomy/cdm/format/ICdmFormatter.java | 24 +++- .../occurrences/AbstractCdmFormatter.java | 19 ++- .../occurrences/DefaultCdmFormatter.java | 5 + .../occurrences/DerivedUnitFormatter.java | 2 +- .../occurrences/FieldUnitFormatter.java | 2 +- .../IdentifiableEntityFormatter.java | 2 +- .../occurrences/MediaSpecimenFormatter.java | 2 +- .../format/occurrences/SequenceFormatter.java | 2 +- .../occurrences/SingleReadFormatter.java | 2 +- .../SpecimenOrObservationBaseFormatter.java | 2 +- 12 files changed, 165 insertions(+), 79 deletions(-) delete mode 100644 cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatter.java create mode 100644 cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatterFactory.java diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatter.java deleted file mode 100644 index 49794fda9c..0000000000 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatter.java +++ /dev/null @@ -1,69 +0,0 @@ -// $Id$ -/** -* Copyright (C) 2015 EDIT -* European Distributed Institute of Taxonomy -* http://www.e-taxonomy.eu -* -* The contents of this file are subject to the Mozilla Public License Version 1.1 -* See LICENSE.TXT at the top of this package for the full license terms. -*/ -package eu.etaxonomy.cdm.format; - -import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey; -import eu.etaxonomy.cdm.format.occurrences.DefaultCdmFormatter; -import eu.etaxonomy.cdm.format.occurrences.DerivedUnitFormatter; -import eu.etaxonomy.cdm.format.occurrences.FieldUnitFormatter; -import eu.etaxonomy.cdm.format.occurrences.MediaSpecimenFormatter; -import eu.etaxonomy.cdm.format.occurrences.SequenceFormatter; -import eu.etaxonomy.cdm.format.occurrences.SingleReadFormatter; -import eu.etaxonomy.cdm.model.common.CdmBase; -import eu.etaxonomy.cdm.model.molecular.Sequence; -import eu.etaxonomy.cdm.model.molecular.SingleRead; -import eu.etaxonomy.cdm.model.occurrence.DerivedUnit; -import eu.etaxonomy.cdm.model.occurrence.FieldUnit; -import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen; - -/** - * @author pplitzner - * @date Nov 30, 2015 - * - */ -public class CdmFormatter { - - private FormatKey[] formatKeys; - - public CdmFormatter(FormatKey... formatKeys) { - this.formatKeys = formatKeys; - } - - public String format(Object object){ - return format(object, formatKeys); - } - - public String format(Object object, FormatKey... formatKeys){ - ICdmFormatter formatter = null; - if(object instanceof CdmBase){ - CdmBase cdmBase = (CdmBase)object; - if(cdmBase.isInstanceOf(Sequence.class)){ - formatter = new SequenceFormatter(object, formatKeys); - } - if(cdmBase.isInstanceOf(SingleRead.class)){ - formatter = new SingleReadFormatter(object, formatKeys); - } - if(cdmBase.isInstanceOf(MediaSpecimen.class)){ - formatter = new MediaSpecimenFormatter(object, formatKeys); - } - if(cdmBase.isInstanceOf(DerivedUnit.class)){ - formatter = new DerivedUnitFormatter(object, formatKeys); - } - if(cdmBase.isInstanceOf(FieldUnit.class)){ - formatter = new FieldUnitFormatter(object, formatKeys); - } - } - if(formatter==null){ - formatter = new DefaultCdmFormatter(object, formatKeys); - } - return formatter.format(object, formatKeys); - } - -} diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatterFactory.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatterFactory.java new file mode 100644 index 0000000000..4ccbe0204c --- /dev/null +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/CdmFormatterFactory.java @@ -0,0 +1,113 @@ +// $Id$ +/** + * Copyright (C) 2015 EDIT + * European Distributed Institute of Taxonomy + * http://www.e-taxonomy.eu + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * See LICENSE.TXT at the top of this package for the full license terms. + */ +package eu.etaxonomy.cdm.format; + +import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey; +import eu.etaxonomy.cdm.format.occurrences.DefaultCdmFormatter; +import eu.etaxonomy.cdm.format.occurrences.DerivedUnitFormatter; +import eu.etaxonomy.cdm.format.occurrences.FieldUnitFormatter; +import eu.etaxonomy.cdm.format.occurrences.MediaSpecimenFormatter; +import eu.etaxonomy.cdm.format.occurrences.SequenceFormatter; +import eu.etaxonomy.cdm.format.occurrences.SingleReadFormatter; +import eu.etaxonomy.cdm.model.common.CdmBase; +import eu.etaxonomy.cdm.model.molecular.Sequence; +import eu.etaxonomy.cdm.model.molecular.SingleRead; +import eu.etaxonomy.cdm.model.occurrence.DerivedUnit; +import eu.etaxonomy.cdm.model.occurrence.FieldUnit; +import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen; + +/** + * Factory class that instantiates a matching ICdmFormatter for the given object + * and configures the format according to the given formatKeys. + * + * @author pplitzner + * @date Nov 30, 2015 + * + */ +public class CdmFormatterFactory { + + /** + * Returns a matching ICdmFormatter for the given object configured with the + * given formatKeys + * + * @param object + * the object which should be formatted as a string + * @param formatKeys + * the formatKeys specifying the parts of which the string is + * built + * @return an ICdmFormatter for the given object configured with the given + * formatKeys + */ + public static ICdmFormatter getFormatter(Object object, + FormatKey... formatKeys) { + ICdmFormatter formatter = null; + if (object instanceof CdmBase) { + CdmBase cdmBase = (CdmBase) object; + if (cdmBase.isInstanceOf(Sequence.class)) { + formatter = new SequenceFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(SingleRead.class)) { + formatter = new SingleReadFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(MediaSpecimen.class)) { + formatter = new MediaSpecimenFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(DerivedUnit.class)) { + formatter = new DerivedUnitFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(FieldUnit.class)) { + formatter = new FieldUnitFormatter(object, formatKeys); + } + } + if (formatter == null) { + formatter = new DefaultCdmFormatter(object, formatKeys); + } + return formatter; + } + + /** + * Convenience method which directly formats the given object according to + * the given formatKeys. + * + * @param object + * the object which should be formatted as a string + * @param formatKeys + * the formatKeys specifying the parts of which the string is + * built + * @return a string representation of the given object according to the + * given formatKeys + */ + public static String format(Object object, FormatKey... formatKeys) { + ICdmFormatter formatter = null; + if (object instanceof CdmBase) { + CdmBase cdmBase = (CdmBase) object; + if (cdmBase.isInstanceOf(Sequence.class)) { + formatter = new SequenceFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(SingleRead.class)) { + formatter = new SingleReadFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(MediaSpecimen.class)) { + formatter = new MediaSpecimenFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(DerivedUnit.class)) { + formatter = new DerivedUnitFormatter(object, formatKeys); + } + if (cdmBase.isInstanceOf(FieldUnit.class)) { + formatter = new FieldUnitFormatter(object, formatKeys); + } + } + if (formatter == null) { + formatter = new DefaultCdmFormatter(object, formatKeys); + } + return formatter.format(object, formatKeys); + } + +} diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/ICdmFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/ICdmFormatter.java index 60e3d2b33a..69bb637093 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/ICdmFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/ICdmFormatter.java @@ -11,7 +11,8 @@ package eu.etaxonomy.cdm.format; /** * Implementing classes provide a string representation for a given object. - * The format of the string can be configured by using the {@link FormatKey} enum. + * How the the string is built can be configured + * by using the {@link FormatKey} enum.
* @author pplitzner * @date Nov 30, 2015 * @@ -53,6 +54,27 @@ public interface ICdmFormatter { AMPLIFICATION_LABEL, } + /** + * Returns a string representation of the given object.
+ * The is built according to the formatKeys passed as arguments.
+ * E.g. + * + * format(derivedUnit, GATHERING_COUNTRY, COMMA, GATHERING_COLLECTOR, COMMA, OPEN_BRACKET, COLLECTION_CODE, CLOSE_BRACKET + * will result in something like Peru, L. (B) + * + * @param object the object which should be formatted as a string representation + * @param formatKeys a list of enum values specifying the parts of which the string consists + * @return a string representation of the given object according to the chosen enum values + */ public String format(Object object, FormatKey... formatKeys); + + /** + * Returns a string representation of the given object.
+ * Note: Only use this method if the formatKeys for this + * ICdmFormatter have been set before. Otherwise the string might be empty. + * @param object the object which should be formatted as a string representation + * @return a string representation of the given object + */ + public String format(Object object); } diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/AbstractCdmFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/AbstractCdmFormatter.java index 256f92039e..5c12581ec6 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/AbstractCdmFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/AbstractCdmFormatter.java @@ -21,10 +21,13 @@ import eu.etaxonomy.cdm.format.ICdmFormatter; */ public abstract class AbstractCdmFormatter implements ICdmFormatter { + protected FormatKey[] formatKeys; + protected Map formatKeyMap = new HashMap(); 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, " "); diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DefaultCdmFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DefaultCdmFormatter.java index b7c2fc3911..8272c96529 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DefaultCdmFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DefaultCdmFormatter.java @@ -25,4 +25,9 @@ public class DefaultCdmFormatter extends AbstractCdmFormatter { public String format(Object object, FormatKey... formatKeys) { return object.toString(); } + + @Override + public String format(Object object) { + return object.toString(); + } } diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DerivedUnitFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DerivedUnitFormatter.java index eec79b2747..d2457bdbd0 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DerivedUnitFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/DerivedUnitFormatter.java @@ -24,7 +24,7 @@ public class DerivedUnitFormatter extends SpecimenOrObservationBaseFormatter{ } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); DerivedUnit derivedUnit = (DerivedUnit)object; for (FormatKey formatKey : formatKeys) { diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/FieldUnitFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/FieldUnitFormatter.java index ad397c337a..25a93d387b 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/FieldUnitFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/FieldUnitFormatter.java @@ -24,7 +24,7 @@ public class FieldUnitFormatter extends SpecimenOrObservationBaseFormatter { } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); FieldUnit fieldUnit = (FieldUnit)object; GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent(); diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/IdentifiableEntityFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/IdentifiableEntityFormatter.java index 134c8f3867..5e2bff329a 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/IdentifiableEntityFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/IdentifiableEntityFormatter.java @@ -22,7 +22,7 @@ public class IdentifiableEntityFormatter extends AbstractCdmFormatter { } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); // IdentifiableEntity identifiableEntity = (IdentifiableEntity)object; // List identifiers = identifiableEntity.getIdentifiers(); diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/MediaSpecimenFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/MediaSpecimenFormatter.java index 06a1d20b9d..3cebb42c77 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/MediaSpecimenFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/MediaSpecimenFormatter.java @@ -24,7 +24,7 @@ public class MediaSpecimenFormatter extends DerivedUnitFormatter { } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); MediaSpecimen mediaSpecimen = (MediaSpecimen)object; Media media = mediaSpecimen.getMediaSpecimen(); diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SequenceFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SequenceFormatter.java index 4235d3b073..970cfffca6 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SequenceFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SequenceFormatter.java @@ -23,7 +23,7 @@ public class SequenceFormatter extends AbstractCdmFormatter { } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); Sequence sequence = (Sequence)object; if(sequence.getDnaMarker()!=null){ diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SingleReadFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SingleReadFormatter.java index 9a60bdf9fd..a72d2500f3 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SingleReadFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SingleReadFormatter.java @@ -23,7 +23,7 @@ public class SingleReadFormatter extends AbstractCdmFormatter { } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); SingleRead singleRead = (SingleRead)object; if(singleRead.getPrimer()!=null){ diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SpecimenOrObservationBaseFormatter.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SpecimenOrObservationBaseFormatter.java index 062a0a42b0..d2e6a8b7bf 100644 --- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SpecimenOrObservationBaseFormatter.java +++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/occurrences/SpecimenOrObservationBaseFormatter.java @@ -23,7 +23,7 @@ public class SpecimenOrObservationBaseFormatter extends IdentifiableEntityFormat } @Override - protected void initFormatKeys(Object object, FormatKey... formatKeys) { + protected void initFormatKeys(Object object) { super.initFormatKeys(object); SpecimenOrObservationBase specimenOrObservationBase = (SpecimenOrObservationBase)object; if(specimenOrObservationBase.getRecordBasis()!=null){ -- 2.34.1