From: <jrb...@us...> - 2011-11-21 17:59:18
|
Revision: 1249 http://cishell.svn.sourceforge.net/cishell/?rev=1249&view=rev Author: jrbibers Date: 2011-11-21 17:59:11 +0000 (Mon, 21 Nov 2011) Log Message: ----------- New class org.cishell.utilities.DataFactory with static factory methods for creating Data instances. This should reduce the need for those pervasive 5-to-7 line "wrapWithMetadata"-type methods in almost every algorithm. Convenience methods for four common Data creation cases: * With prescribed format and type. * With a prescribed type, but format = {wrapped object}.getClass().getName(). * Inheriting the parent's format and type. * For a file with prescribed MIME-type (format) and type. This is just an alias for the first. Currently uses BasicData as the Data implementation, but this can be changed later. Reviewed by Thomas. Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java 2011-11-21 17:59:11 UTC (rev 1249) @@ -0,0 +1,83 @@ +package org.cishell.utilities; + +import java.io.File; +import java.util.Dictionary; +import java.util.Hashtable; + +import org.cishell.framework.data.BasicData; +import org.cishell.framework.data.Data; +import org.cishell.framework.data.DataProperty; + +public final class DataFactory { + private DataFactory() { + // Static factory methods only. + } + + /** + * Creates a Data wrapping {@code object}. + * + * @param datum Object to wrap + * @param format The {@link Data#getFormat() format} of {@code object} + * @param type The {@link DataProperty#TYPE type} of {@code object} + * See {@link DataProperty}.*_TYPE for possible values + * @param parent The {@link Data} from which {@code object} was derived + * @param label A concise String describing {@code object} in relation to {@code parent} + */ + public static Data forObject(Object object, String format, String type, Data parent, String label) { + Dictionary<String, Object> metadata = new Hashtable<String, Object>(); + metadata.put(DataProperty.TYPE, type); + metadata.put(DataProperty.PARENT, parent); + metadata.put(DataProperty.LABEL, label); + + return new BasicData(metadata, object, format); + } + /** + * Creates a Data wrapping {@code object} having the same {@link Data#getFormat() format} and + * {@link DataProperty#TYPE type} as its {@code parent}. + * + * @param object Object to wrap + * @param parent The {@link Data} from which {@code object} was derived + * @param label A concise String describing {@code object} in relation to {@code parent} + * @return A Data wrapping {@code object} using {@code parent}'s {@code format} + * and {@code type} + */ + public static Data likeParent(Object object, Data parent, String label) { + return forObject( + object, + parent.getFormat(), + (String) parent.getMetadata().get(DataProperty.TYPE), + parent, + label); + } + /** + * Creates a Data wrapping {@code object} using {@code object.getClass().getName()} as its + * format. + * + * @param object Object to wrap as Data + * @param type The {@link DataProperty#TYPE type} of {@code object} + * See {@link DataProperty}.*_TYPE for possible values + * @param parent The {@link Data} from which {@code object} was derived + * @param label A concise String describing {@code object} in relation to {@code parent} + * @return A Data wrapping {@code object} using {@code object.getClass().getName()} as its + * format + */ + public static Data withClassNameAsFormat( + Object object, String type, Data parent, String label) { + return forObject(object, object.getClass().getName(), type, parent, label); + } + /** + * Creates a Data wrapping {@code file} using {@code mimeType} as its format. + * + * @param file File to wrap as Data + * @param mimeType {@code file}'s MIME type, e.g. "file:text/plain". + * @param type The {@link DataProperty#TYPE type} of {@code object} + * See {@link DataProperty}.*_TYPE for possible values + * @param parent The {@link Data} from which {@code object} was derived + * @param label A concise String describing {@code object} in relation to {@code parent} + * @return A Data wrapping {@code file} using {@code mimeType} as its format + */ + public static Data forFile( + File file, String mimeType, String type, Data parent, String label) { + return forObject(file, mimeType, type, parent, label); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jrb...@us...> - 2011-11-21 18:02:40
|
Revision: 1250 http://cishell.svn.sourceforge.net/cishell/?rev=1250&view=rev Author: jrbibers Date: 2011-11-21 18:02:33 +0000 (Mon, 21 Nov 2011) Log Message: ----------- Punctuation in Javadoc. Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java 2011-11-21 17:59:11 UTC (rev 1249) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/DataFactory.java 2011-11-21 18:02:33 UTC (rev 1250) @@ -18,7 +18,7 @@ * * @param datum Object to wrap * @param format The {@link Data#getFormat() format} of {@code object} - * @param type The {@link DataProperty#TYPE type} of {@code object} + * @param type The {@link DataProperty#TYPE type} of {@code object}. * See {@link DataProperty}.*_TYPE for possible values * @param parent The {@link Data} from which {@code object} was derived * @param label A concise String describing {@code object} in relation to {@code parent} @@ -35,11 +35,11 @@ * Creates a Data wrapping {@code object} having the same {@link Data#getFormat() format} and * {@link DataProperty#TYPE type} as its {@code parent}. * - * @param object Object to wrap - * @param parent The {@link Data} from which {@code object} was derived - * @param label A concise String describing {@code object} in relation to {@code parent} + * @param object Object to wrap. + * @param parent The {@link Data} from which {@code object} was derived. + * @param label A concise String describing {@code object} in relation to {@code parent}. * @return A Data wrapping {@code object} using {@code parent}'s {@code format} - * and {@code type} + * and {@code type}. */ public static Data likeParent(Object object, Data parent, String label) { return forObject( @@ -53,13 +53,13 @@ * Creates a Data wrapping {@code object} using {@code object.getClass().getName()} as its * format. * - * @param object Object to wrap as Data - * @param type The {@link DataProperty#TYPE type} of {@code object} - * See {@link DataProperty}.*_TYPE for possible values - * @param parent The {@link Data} from which {@code object} was derived - * @param label A concise String describing {@code object} in relation to {@code parent} + * @param object Object to wrap as Data. + * @param type The {@link DataProperty#TYPE type} of {@code object}. + * See {@link DataProperty}.*_TYPE for possible values. + * @param parent The {@link Data} from which {@code object} was derived. + * @param label A concise String describing {@code object} in relation to {@code parent}. * @return A Data wrapping {@code object} using {@code object.getClass().getName()} as its - * format + * format. */ public static Data withClassNameAsFormat( Object object, String type, Data parent, String label) { @@ -68,13 +68,13 @@ /** * Creates a Data wrapping {@code file} using {@code mimeType} as its format. * - * @param file File to wrap as Data + * @param file File to wrap as Data. * @param mimeType {@code file}'s MIME type, e.g. "file:text/plain". - * @param type The {@link DataProperty#TYPE type} of {@code object} - * See {@link DataProperty}.*_TYPE for possible values - * @param parent The {@link Data} from which {@code object} was derived - * @param label A concise String describing {@code object} in relation to {@code parent} - * @return A Data wrapping {@code file} using {@code mimeType} as its format + * @param type The {@link DataProperty#TYPE type} of {@code object}. + * See {@link DataProperty}.*_TYPE for possible values. + * @param parent The {@link Data} from which {@code object} was derived. + * @param label A concise String describing {@code object} in relation to {@code parent}. + * @return A Data wrapping {@code file} using {@code mimeType} as its format. */ public static Data forFile( File file, String mimeType, String type, Data parent, String label) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |