package com.basilv.core;


import java.io.File;
import java.io.IOException;

/**
 * Contains utility methods for working with Files.
 */
public class FileUtilities
{

	/**
	 * Return the canonical path, or the absolute path if an error occurs.
	 * @param file  Cannot be null.
	 * @return the full path of the file.
	 */
	public static String getCanonicalPath(File file) {
		Assert.notNull("file", file);
		try {
			return file.getCanonicalPath();
		} catch (IOException e) {
			return file.getAbsolutePath();
		}
	}
	
	
	/**
	 * Delete the specified directory and all its contents. 
	 * @param dir  Cannot be null. If a file is specified, it is deleted.
	 */
	public static void deleteRecursively(File dir) {
		Assert.notNull("dir", dir);
		
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				deleteRecursively(files[i]);
			}
			dir.delete();
		} else {
			// Just a file so delete normally.
			dir.delete();
		}
	}
}
