info.aduna.lang
Class FileFormat

java.lang.Object
  extended by info.aduna.lang.FileFormat

public class FileFormat
extends Object

Abstract representation of a file format. File formats are identified by a name and can have one or more associated MIME types, zero or more associated file extensions and can specify a (default) character encoding.

Author:
Arjohn Kampman

Constructor Summary
FileFormat(String name, Collection<String> mimeTypes, Charset charset, Collection<String> fileExtensions)
          Creates a new FileFormat object.
FileFormat(String name, String mimeType, Charset charset, Collection<String> fileExtensions)
          Creates a new FileFormat object.
FileFormat(String name, String mimeType, Charset charset, String fileExtension)
          Creates a new FileFormat object.
 
Method Summary
 boolean equals(Object other)
          Compares FileFormat objects based on their name, ignoring case.
 Charset getCharset()
          Get the (default) charset for this file format.
 String getDefaultFileExtension()
          Gets the default file name extension for this file format.
 String getDefaultMIMEType()
          Gets the default MIME type for this file format.
 List<String> getFileExtensions()
          Gets the file format's file extensions.
 List<String> getMIMETypes()
          Gets the file format's MIME types.
 String getName()
          Gets the name of this file format.
 boolean hasCharset()
          Checks if the FileFormat has a (default) charset.
 boolean hasDefaultFileExtension(String extension)
          Checks if the specified file name extension matches the FileFormat's default file name extension.
 boolean hasDefaultMIMEType(String mimeType)
          Checks if the specified MIME type matches the FileFormat's default MIME type.
 boolean hasFileExtension(String extension)
          Checks if the FileFormat's file extension is equal to the specified file extension.
 int hashCode()
           
 boolean hasMIMEType(String mimeType)
          Checks if specified MIME type matches one of the FileFormat's MIME types.
static
<FF extends FileFormat>
FF
matchFileName(String fileName, Iterable<FF> fileFormats)
          Tries to match the specified file name with the file extensions of the supplied file formats.
static
<FF extends FileFormat>
FF
matchFileName(String fileName, Iterable<FF> fileFormats, FF fallback)
          Tries to match the specified file name with the file extensions of the supplied file formats.
static
<FF extends FileFormat>
FF
matchMIMEType(String mimeType, Iterable<FF> fileFormats)
          Tries to match the specified MIME type with the MIME types of the supplied file formats.
static
<FF extends FileFormat>
FF
matchMIMEType(String mimeType, Iterable<FF> fileFormats, FF fallback)
          Tries to match the specified MIME type with the MIME types of the supplied file formats.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileFormat

public FileFormat(String name,
                  String mimeType,
                  Charset charset,
                  String fileExtension)
Creates a new FileFormat object.

Parameters:
name - The name of the file format, e.g. "PLAIN TEXT".
mimeType - The (default) MIME type of the file format, e.g. text/plain for plain text files.
charset - The default character encoding of the file format. Specify null if not applicable.
fileExt - The (default) file extension for the file format, e.g. txt for plain text files.

FileFormat

public FileFormat(String name,
                  String mimeType,
                  Charset charset,
                  Collection<String> fileExtensions)
Creates a new FileFormat object.

Parameters:
name - The name of the file format, e.g. "PLAIN TEXT".
mimeType - The (default) MIME type of the file format, e.g. text/plain for plain text files.
charset - The default character encoding of the file format. Specify null if not applicable.
fileExtensions - The file format's file extension(s), e.g. txt for plain text files. The first item in the list is interpreted as the default file extension for the format.

FileFormat

public FileFormat(String name,
                  Collection<String> mimeTypes,
                  Charset charset,
                  Collection<String> fileExtensions)
Creates a new FileFormat object.

Parameters:
name - The name of the file format, e.g. "PLAIN TEXT".
mimeTypes - The MIME type(s) of the file format, e.g. text/plain for theplain text files. The first item in the list is interpreted as the default MIME type for the format. The supplied list should contain at least one MIME type.
charset - The default character encoding of the file format. Specify null if not applicable.
fileExtensions - The file format's file extension(s), e.g. txt for plain text files. The first item in the list is interpreted as the default file extension for the format.
Method Detail

getName

public String getName()
Gets the name of this file format.

Returns:
A human-readable format name, e.g. "PLAIN TEXT".

getDefaultMIMEType

public String getDefaultMIMEType()
Gets the default MIME type for this file format.

Returns:
A MIME type string, e.g. "text/plain".

hasDefaultMIMEType

public boolean hasDefaultMIMEType(String mimeType)
Checks if the specified MIME type matches the FileFormat's default MIME type. The MIME types are compared ignoring upper/lower-case differences.

Parameters:
mimeType - The MIME type to compare to the FileFormat's default MIME type.
Returns:
true if the specified MIME type matches the FileFormat's default MIME type.

getMIMETypes

public List<String> getMIMETypes()
Gets the file format's MIME types.

Returns:
An unmodifiable list of MIME type strings, e.g. "text/plain".

hasMIMEType

public boolean hasMIMEType(String mimeType)
Checks if specified MIME type matches one of the FileFormat's MIME types. The MIME types are compared ignoring upper/lower-case differences.

Parameters:
mimeType - The MIME type to compare to the FileFormat's MIME types.
Returns:
true if the specified MIME type matches one of the FileFormat's MIME types.

getDefaultFileExtension

public String getDefaultFileExtension()
Gets the default file name extension for this file format.

Returns:
A file name extension (excluding the dot), e.g. "txt", or null if there is no common file extension for the format.

hasDefaultFileExtension

public boolean hasDefaultFileExtension(String extension)
Checks if the specified file name extension matches the FileFormat's default file name extension. The file name extension MIME types are compared ignoring upper/lower-case differences.

Parameters:
extension - The file extension to compare to the FileFormat's file extension.
Returns:
true if the file format has a default file name extension and if it matches the specified extension, false otherwise.

getFileExtensions

public List<String> getFileExtensions()
Gets the file format's file extensions.

Returns:
An unmodifiable list of file extension strings, e.g. "text".

hasFileExtension

public boolean hasFileExtension(String extension)
Checks if the FileFormat's file extension is equal to the specified file extension. The file extensions are compared ignoring upper/lower-case differences.

Parameters:
extension - The file extension to compare to the FileFormat's file extension.
Returns:
true if the specified file extension is equal to the FileFormat's file extension.

getCharset

public Charset getCharset()
Get the (default) charset for this file format.

Returns:
the (default) charset for this file format, or null if this format does not have a default charset.

hasCharset

public boolean hasCharset()
Checks if the FileFormat has a (default) charset.

Returns:
true if the FileFormat has a (default) charset.

equals

public boolean equals(Object other)
Compares FileFormat objects based on their name, ignoring case.

Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

matchMIMEType

public static <FF extends FileFormat> FF matchMIMEType(String mimeType,
                                                       Iterable<FF> fileFormats)
Tries to match the specified MIME type with the MIME types of the supplied file formats.

Parameters:
mimeType - A MIME type, e.g. "text/plain".
fileFormats - The file formats to match the MIME type against.
Returns:
A FileFormat object if the MIME type was recognized, or null otherwise.
See Also:
matchMIMEType(String, Iterable, FileFormat)

matchMIMEType

public static <FF extends FileFormat> FF matchMIMEType(String mimeType,
                                                       Iterable<FF> fileFormats,
                                                       FF fallback)
Tries to match the specified MIME type with the MIME types of the supplied file formats. The supplied fallback format will be returned when no matching format was found.

Parameters:
mimeType - A MIME type, e.g. "text/plain".
fileFormats - The file formats to match the MIME type against.
fallback - The file format to return if no matching format can be found.
Returns:
A FileFormat that matches the MIME type, or the fallback format if the extension was not recognized.

matchFileName

public static <FF extends FileFormat> FF matchFileName(String fileName,
                                                       Iterable<FF> fileFormats)
Tries to match the specified file name with the file extensions of the supplied file formats.

Parameters:
fileName - A file name.
fileFormats - The file formats to match the file name extension against.
Returns:
A FileFormat that matches the file name extension, or null otherwise.
See Also:
matchFileName(String, Iterable, FileFormat)

matchFileName

public static <FF extends FileFormat> FF matchFileName(String fileName,
                                                       Iterable<FF> fileFormats,
                                                       FF fallback)
Tries to match the specified file name with the file extensions of the supplied file formats. The supplied fallback format will be returned when the file name extension was not recognized.

Parameters:
fileName - A file name.
fileFormats - The file formats to match the file name extension against.
fallback - The file format to return if no matching format can be found.
Returns:
A FileFormat that matches the file name extension, or the fallback format if the extension was not recognized.


Copyright © 1997-2008 Aduna. All Rights Reserved.