Project

General

Profile

Download (3.84 KB) Statistics
| Branch: | Tag: | Revision:
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.DefaultCdmFormatter;
13
import eu.etaxonomy.cdm.format.occurrences.DerivedUnitFormatter;
14
import eu.etaxonomy.cdm.format.occurrences.FieldUnitFormatter;
15
import eu.etaxonomy.cdm.format.occurrences.MediaSpecimenFormatter;
16
import eu.etaxonomy.cdm.format.occurrences.SequenceFormatter;
17
import eu.etaxonomy.cdm.format.occurrences.SingleReadFormatter;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.model.molecular.Sequence;
20
import eu.etaxonomy.cdm.model.molecular.SingleRead;
21
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
22
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
23
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
24

    
25
/**
26
 * Factory class that instantiates a matching ICdmFormatter for the given object
27
 * and configures the format according to the given formatKeys.
28
 * 
29
 * @author pplitzner
30
 * @date Nov 30, 2015
31
 *
32
 */
33
public class CdmFormatterFactory {
34

    
35
	/**
36
	 * Returns a matching ICdmFormatter for the given object configured with the
37
	 * given formatKeys
38
	 * 
39
	 * @param object
40
	 *            the object which should be formatted as a string
41
	 * @param formatKeys
42
	 *            the formatKeys specifying the parts of which the string is
43
	 *            built
44
	 * @return an ICdmFormatter for the given object configured with the given
45
	 *         formatKeys
46
	 */
47
	public static ICdmFormatter getFormatter(Object object,
48
			FormatKey... formatKeys) {
49
		ICdmFormatter formatter = null;
50
		if (object instanceof CdmBase) {
51
			CdmBase cdmBase = (CdmBase) object;
52
			if (cdmBase.isInstanceOf(Sequence.class)) {
53
				return new SequenceFormatter(object, formatKeys);
54
			}
55
			else if (cdmBase.isInstanceOf(SingleRead.class)) {
56
				return new SingleReadFormatter(object, formatKeys);
57
			}
58
			else if (cdmBase.isInstanceOf(MediaSpecimen.class)) {
59
				return new MediaSpecimenFormatter(object, formatKeys);
60
			}
61
			else if (cdmBase.isInstanceOf(DerivedUnit.class)) {
62
				return new DerivedUnitFormatter(object, formatKeys);
63
			}
64
			else if (cdmBase.isInstanceOf(FieldUnit.class)) {
65
				return new FieldUnitFormatter(object, formatKeys);
66
			}
67
		}
68
		if (formatter == null) {
69
			formatter = new DefaultCdmFormatter(object, formatKeys);
70
		}
71
		return formatter;
72
	}
73

    
74
	/**
75
	 * Convenience method which directly formats the given object according to
76
	 * the given formatKeys.
77
	 * 
78
	 * @param object
79
	 *            the object which should be formatted as a string
80
	 * @param formatKeys
81
	 *            the formatKeys specifying the parts of which the string is
82
	 *            built
83
	 * @return a string representation of the given object according to the
84
	 *         given formatKeys
85
	 */
86
	public static String format(Object object, FormatKey... formatKeys) {
87
		ICdmFormatter formatter = null;
88
		if (object instanceof CdmBase) {
89
			CdmBase cdmBase = (CdmBase) object;
90
			if (cdmBase.isInstanceOf(Sequence.class)) {
91
				formatter = new SequenceFormatter(object, formatKeys);
92
			}
93
			else if (cdmBase.isInstanceOf(SingleRead.class)) {
94
				formatter = new SingleReadFormatter(object, formatKeys);
95
			}
96
			else if (cdmBase.isInstanceOf(MediaSpecimen.class)) {
97
				formatter = new MediaSpecimenFormatter(object, formatKeys);
98
			}
99
			else if (cdmBase.isInstanceOf(DerivedUnit.class)) {
100
				formatter = new DerivedUnitFormatter(object, formatKeys);
101
			}
102
			else if (cdmBase.isInstanceOf(FieldUnit.class)) {
103
				formatter = new FieldUnitFormatter(object, formatKeys);
104
			}
105
		}
106
		if (formatter == null) {
107
			formatter = new DefaultCdmFormatter(object, formatKeys);
108
		}
109
		return formatter.format(object, formatKeys);
110
	}
111

    
112
}
(1-1/2)