de.hpi.fgis.dude.util.data.storage
Class FileBasedStorage<T extends Jsonable>

java.lang.Object
  extended by de.hpi.fgis.dude.util.data.storage.AbstractDuDeStorage<T>
      extended by de.hpi.fgis.dude.util.data.storage.FileBasedStorage<T>
Type Parameters:
T - The Jsonable type whose instances are stored.
All Implemented Interfaces:
DuDeStorage<T>, JsonReadable<T>, JsonWritable<T>

public class FileBasedStorage<T extends Jsonable>
extends AbstractDuDeStorage<T>

FileBasedStorage stores Jsonable instances in files.

Author:
Matthias Pohl

Nested Class Summary
 
Nested classes/interfaces inherited from class de.hpi.fgis.dude.util.data.storage.AbstractDuDeStorage
AbstractDuDeStorage.AbstractJsonableWriter<T extends Jsonable>
 
Field Summary
static String JSON_FILE_EXTENSION
          The file extension that is used for each file.
 
Constructor Summary
  FileBasedStorage(Class<T> type, File f)
          Initializes a FileBasedStorage instance with the type information, the underlying file, and the initial content.
protected FileBasedStorage(Class<T> type, File f, Collection<T> initialContent)
          Initializes a FileBasedStorage instance with the type information, the underlying file, and the initial content.
  FileBasedStorage(Class<T> type, String name)
          Initializes a FileBasedStorage instance with the passed type information and a name.
  FileBasedStorage(Class<T> type, String name, Collection<T> initialContent)
          Initializes a FileBasedStorage instance with the passed type information, its name, and the initial content.
  FileBasedStorage(Class<T> type, String dir, String name)
          Initializes a FileBasedStorage instance with the type information, a directory and its name.
  FileBasedStorage(Class<T> type, String dir, String name, Collection<T> initialContent)
          Initializes a FileBasedStorage instance with the type information, a directory, its name, and the initial content.
  FileBasedStorage(String name)
          Initializes a FileBasedStorage instance with the passed name.
  FileBasedStorage(String name, Collection<T> initialContent)
          Initializes a FileBasedStorage instance with the passed name, and the initial content.
  FileBasedStorage(String dir, String name)
          Initializes a FileBasedStorage instance with the type information, a directory and its name.
  FileBasedStorage(String dir, String name, Collection<T> initialContent)
          Initializes a FileBasedStorage instance with the type information, a directory, its name, and the initial content.
 
Method Summary
 void delete()
          Deletes the underlying file.
protected  void generateEmptyJsonFile()
          Overwrites any existing file and initializes a new one containing an empty Json array.
 File getFile()
          Returns the underlying file.
 JsonableReader<T> getReader()
          Returns the JsonableReader that can be used to access the content of this DuDeStorage.
 JsonableWriter<T> getWriter()
          Returns the JsonableWriter that can be used to add instances to this DuDeStorage.
 void renameTo(String newFilename)
          Renames the underlying file.
 int size()
          Returns the number of instances that are contained.
 
Methods inherited from class de.hpi.fgis.dude.util.data.storage.AbstractDuDeStorage
disableFormattedJson, enableFormattedJson, isFormattedJson
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JSON_FILE_EXTENSION

public static final String JSON_FILE_EXTENSION
The file extension that is used for each file.

See Also:
Constant Field Values
Constructor Detail

FileBasedStorage

public FileBasedStorage(String name)
                 throws IOException
Initializes a FileBasedStorage instance with the passed name. The underlying file will be placed in the default directory. The reader will return JsonValue instances.

Parameters:
name - The name of the underlying file.
Throws:
IOException - If an error occurs while initializing the file.
See Also:
GlobalConfig#getWorkingDirectory()}

FileBasedStorage

public FileBasedStorage(Class<T> type,
                        String name)
                 throws IOException
Initializes a FileBasedStorage instance with the passed type information and a name. The underlying file will be placed in the default directory.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
name - The name of the underlying file.
Throws:
IOException - If an error occurs while initializing the file.
See Also:
GlobalConfig#getWorkingDirectory()}

FileBasedStorage

public FileBasedStorage(String name,
                        Collection<T> initialContent)
                 throws IOException
Initializes a FileBasedStorage instance with the passed name, and the initial content. The underlying file will be placed in the default directory. The initial content will be overwritten if getWriter() is called. The return type of the JsonableReader is JsonValue.

Parameters:
name - The name of the underlying file.
initialContent - The initial content that shall be added to that storage.
Throws:
IOException - If an error occurs while initializing the file.
See Also:
GlobalConfig#getWorkingDirectory()}

FileBasedStorage

public FileBasedStorage(Class<T> type,
                        String name,
                        Collection<T> initialContent)
                 throws IOException
Initializes a FileBasedStorage instance with the passed type information, its name, and the initial content. The underlying file will be placed in the default directory. The initial content will be overwritten if getWriter() is called.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
name - The name of the underlying file.
initialContent - The initial content that shall be added to that storage.
Throws:
IOException - If an error occurs while initializing the file.
See Also:
GlobalConfig#getWorkingDirectory()}

FileBasedStorage

public FileBasedStorage(String dir,
                        String name)
                 throws IOException
Initializes a FileBasedStorage instance with the type information, a directory and its name. The return type of the JsonableReader is JsonValue since no type is passed.

Parameters:
dir - The directory where the file shall be placed in.
name - The name of the underlying file.
Throws:
IOException - If an error occurs while initializing the file.

FileBasedStorage

public FileBasedStorage(Class<T> type,
                        String dir,
                        String name)
                 throws IOException
Initializes a FileBasedStorage instance with the type information, a directory and its name.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
dir - The directory where the file shall be placed in.
name - The name of the underlying file.
Throws:
IOException - If an error occurs while initializing the file.

FileBasedStorage

public FileBasedStorage(String dir,
                        String name,
                        Collection<T> initialContent)
                 throws IOException
Initializes a FileBasedStorage instance with the type information, a directory, its name, and the initial content. The initial content will be overwritten if getWriter() is called.The return type of the JsonableReader is JsonValue since no type is passed.

Parameters:
dir - The directory where the file shall be placed in.
name - The name of the underlying file.
initialContent - The initial content that shall be added to that storage.
Throws:
IOException - If an error occurs while initializing the file.

FileBasedStorage

public FileBasedStorage(Class<T> type,
                        String dir,
                        String name,
                        Collection<T> initialContent)
                 throws IOException
Initializes a FileBasedStorage instance with the type information, a directory, its name, and the initial content. The initial content will be overwritten if getWriter() is called.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
dir - The directory where the file shall be placed in.
name - The name of the underlying file.
initialContent - The initial content that shall be added to that storage.
Throws:
IOException - If an error occurs while initializing the file.

FileBasedStorage

protected FileBasedStorage(Class<T> type,
                           File f,
                           Collection<T> initialContent)
                    throws IOException
Initializes a FileBasedStorage instance with the type information, the underlying file, and the initial content. The initial content will be overwritten getWriter() is called.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
f - The underlying file.
initialContent - The initial content that shall be added to that storage.
Throws:
IOException - If an error occurs while initializing the file.

FileBasedStorage

public FileBasedStorage(Class<T> type,
                        File f)
                 throws IOException
Initializes a FileBasedStorage instance with the type information, the underlying file, and the initial content. The initial content will be overwritten getWriter() is called.

Parameters:
type - The type of instances that are stored. This type is equal to the generic type of this instance.
f - The underlying file.
Throws:
IOException - If an error occurs while initializing the file.
Method Detail

generateEmptyJsonFile

protected void generateEmptyJsonFile()
                              throws IOException
Overwrites any existing file and initializes a new one containing an empty Json array.

Throws:
IOException - If an error occurs while initializing the new file.

getReader

public JsonableReader<T> getReader()
Description copied from interface: JsonReadable
Returns the JsonableReader that can be used to access the content of this DuDeStorage.

Returns:
The JsonableReader for iterating over the content.

getWriter

public JsonableWriter<T> getWriter()
                                             throws IOException
Description copied from interface: JsonWritable
Returns the JsonableWriter that can be used to add instances to this DuDeStorage.

Returns:
The JsonableWriter for adding content to this storage.
Throws:
IOException - If an error occurs while writing data.

size

public int size()
Description copied from interface: JsonReadable
Returns the number of instances that are contained.

Returns:
The size of this storage.

renameTo

public void renameTo(String newFilename)
Renames the underlying file.

Parameters:
newFilename - The new name of the file.

getFile

public File getFile()
Returns the underlying file.

Returns:
the underlying file

delete

public void delete()
Deletes the underlying file.



Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.