1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.format;
|
10
|
|
11
|
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
|
12
|
import eu.etaxonomy.cdm.format.occurrences.DerivedUnitFormatter;
|
13
|
import eu.etaxonomy.cdm.format.occurrences.FieldUnitFormatter;
|
14
|
import eu.etaxonomy.cdm.format.occurrences.MediaSpecimenFormatter;
|
15
|
import eu.etaxonomy.cdm.format.occurrences.SequenceFormatter;
|
16
|
import eu.etaxonomy.cdm.format.occurrences.SingleReadFormatter;
|
17
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
18
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
19
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
20
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
21
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
22
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
23
|
|
24
|
/**
|
25
|
* Factory class that instantiates a matching ICdmFormatter for the given object
|
26
|
* and configures the format according to the given formatKeys.
|
27
|
*
|
28
|
* @author pplitzner
|
29
|
* @since Nov 30, 2015
|
30
|
*/
|
31
|
public class CdmFormatterFactory {
|
32
|
|
33
|
/**
|
34
|
* Returns a matching ICdmFormatter for the given object configured with the
|
35
|
* given formatKeys
|
36
|
*
|
37
|
* @param object
|
38
|
* the object which should be formatted as a string
|
39
|
* @param formatKeys
|
40
|
* the formatKeys specifying the parts of which the string is
|
41
|
* built
|
42
|
* @return an ICdmFormatter for the given object configured with the given
|
43
|
* formatKeys
|
44
|
*/
|
45
|
public static ICdmFormatter getFormatter(Object object,
|
46
|
FormatKey... formatKeys) {
|
47
|
if (object instanceof CdmBase) {
|
48
|
CdmBase cdmBase = (CdmBase) object;
|
49
|
if (cdmBase.isInstanceOf(Sequence.class)) {
|
50
|
return new SequenceFormatter(object, formatKeys);
|
51
|
}
|
52
|
else if (cdmBase.isInstanceOf(SingleRead.class)) {
|
53
|
return new SingleReadFormatter(object, formatKeys);
|
54
|
}
|
55
|
else if (cdmBase.isInstanceOf(MediaSpecimen.class)) {
|
56
|
return new MediaSpecimenFormatter(object, formatKeys);
|
57
|
}
|
58
|
else if (cdmBase.isInstanceOf(DerivedUnit.class)) {
|
59
|
return new DerivedUnitFormatter(object, formatKeys);
|
60
|
}
|
61
|
else if (cdmBase.isInstanceOf(FieldUnit.class)) {
|
62
|
return new FieldUnitFormatter(object, formatKeys);
|
63
|
}
|
64
|
}
|
65
|
return new DefaultCdmFormatter(object, formatKeys);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Convenience method which directly formats the given object according to
|
70
|
* the given formatKeys.
|
71
|
*
|
72
|
* @param object
|
73
|
* the object which should be formatted as a string
|
74
|
* @param formatKeys
|
75
|
* the formatKeys specifying the parts of which the string is
|
76
|
* built
|
77
|
* @return a string representation of the given object according to the
|
78
|
* given formatKeys
|
79
|
*/
|
80
|
public static String format(Object object, FormatKey... formatKeys) {
|
81
|
ICdmFormatter formatter = null;
|
82
|
if (object instanceof CdmBase) {
|
83
|
CdmBase cdmBase = (CdmBase) object;
|
84
|
if (cdmBase.isInstanceOf(Sequence.class)) {
|
85
|
formatter = new SequenceFormatter(object, formatKeys);
|
86
|
}
|
87
|
else if (cdmBase.isInstanceOf(SingleRead.class)) {
|
88
|
formatter = new SingleReadFormatter(object, formatKeys);
|
89
|
}
|
90
|
else if (cdmBase.isInstanceOf(MediaSpecimen.class)) {
|
91
|
formatter = new MediaSpecimenFormatter(object, formatKeys);
|
92
|
}
|
93
|
else if (cdmBase.isInstanceOf(DerivedUnit.class)) {
|
94
|
formatter = new DerivedUnitFormatter(object, formatKeys);
|
95
|
}
|
96
|
else if (cdmBase.isInstanceOf(FieldUnit.class)) {
|
97
|
formatter = new FieldUnitFormatter(object, formatKeys);
|
98
|
}
|
99
|
}
|
100
|
if (formatter == null) {
|
101
|
formatter = new DefaultCdmFormatter(object, formatKeys);
|
102
|
}
|
103
|
return formatter.format(object, formatKeys);
|
104
|
}
|
105
|
|
106
|
}
|