org.knime.core.internal
Class ReferencedFile

java.lang.Object
  extended by org.knime.core.internal.ReferencedFile

public final class ReferencedFile
extends Object

A File wrapper with modifiable parent location. This class is used in cases in which nested elements keep a file reference and the file location (particular of the parent or the parent of the parent) may change.

Author:
Bernd Wiswedel, University of Konstanz

Constructor Summary
ReferencedFile(File rootDir)
          Creates new root element.
ReferencedFile(ReferencedFile parent, String base)
          Creates new sub-element.
 
Method Summary
 boolean equals(Object obj)
          
 File getFile()
          Get the File representing the full path of this referenced file element.
 ReferencedFile getParent()
          Get the parent of this element or null if the file's parent is not represented as a ReferencedFile object.
 int hashCode()
          
 void lock()
          Locks this file location.
 boolean rename(String newBaseName)
          Renames this (base) element as an atomic operation.
 String toString()
          Get absolute path of the represented file.
 void unlock()
          Unlocks this file hierarchy.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReferencedFile

public ReferencedFile(File rootDir)
Creates new root element.

Parameters:
rootDir - The parent directory of the referenced file location.
Throws:
NullPointerException - If the argument is null

ReferencedFile

public ReferencedFile(ReferencedFile parent,
                      String base)
Creates new sub-element.

Parameters:
parent - The parent location
base - The name of this file
Throws:
NullPointerException - If either argument is null
Method Detail

lock

public void lock()
Locks this file location. Asynchronous invocations of rename(String) will block until unlock() is called. It will also disable the renaming of any element further up the hierarchy. Parallel reading of the resource is still possibly.


unlock

public void unlock()
Unlocks this file hierarchy. (Counterpart to lock()).

Throws:
IllegalMonitorStateException - If monitor is not held by current thread.

rename

public boolean rename(String newBaseName)
Renames this (base) element as an atomic operation. "This" element refers the current element in the hierarchy. The operation will block until all read/write operations have finished. If the associated file location exists (i.e. the file returned by getFile() exists, it will also be renamed.

Parameters:
newBaseName - The new name
Returns:
whether the rename was successful: it returns true in two cases: (i) the file exists and was successfully renamed or (ii) if it does not exist (being positive that it has not been created just yet)
Throws:
NullPointerException - If argument is null

getFile

public File getFile()
Get the File representing the full path of this referenced file element. Please note that the returned file may be renamed after this method returns. In order to circumvent this, you typically write code as follows:
 ReferencedFile m_refFile = ... // this element
 m_refFile.lock();
 try {
   File file = m_refFile.getFile();
   // do something with this file
 } finally {
   m_refFile.unlock();
 }
 

Returns:
The file representing the full path of this referenced file

getParent

public ReferencedFile getParent()
Get the parent of this element or null if the file's parent is not represented as a ReferencedFile object.

Returns:
The parent or null.

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

toString

public String toString()
Get absolute path of the represented file.

Overrides:
toString in class Object


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.