package de.gaalop;
import java.nio.charset.Charset;
/**
* This interface represents a single source file that has
* been generated by a {@link CodeGenerator}.
* <p/>
* Please note that implementing classes must not be thread hostile.
* If possible they should be immutable and thus inherently thread-safe.
*
* @author Sebastian Hartte
* @version 1.0
* @since 1.0
*/
public final class OutputFile {
private final String name;
private final String content;
private final Charset encoding;
/**
* Constructs a new output file.
*
* @param name The filename of the output file.
* @param content The content of the output file.
* @param encoding The character set that should be used to encode the content of the output file to a binary
* stream.
*/
public OutputFile(String name, String content, Charset encoding) {
this.name = name;
this.content = content;
this.encoding = encoding;
}
/**
* This method provides the filename of the generated file. Please note
* that this is a <em>virtual</em> file and thus the returned filename
* does not denote a file that really exists.
*
* @return The filename as a string. Must not be null.
*/
public String getName() {
return name;
}
/**
* Returns the encoding that should be used to save this file to disk.
*
* @return The character set that this file should be saved in.
*/
public Charset getEncoding() {
return encoding;
}
/**
* Returns the text content of this file.
* @return A string that contains the source code of this output file.
*/
public String getContent() {
return content;
}
/**
* Gets the content of the generated file as a human readable string.
* Calls {@link #getContent} internally.
*/
public String toString() {
return getContent();
}
/**
* Compares this output files to another for equality.
*
* Output files are equal if and only if their class is the same and their content, encoding and name are equal.
*
* @param o The other object.
* @return True if and only if this object is equal to the other object.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OutputFile that = (OutputFile) o;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
if (encoding != null ? !encoding.equals(that.encoding) : that.encoding != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (content != null ? content.hashCode() : 0);
result = 31 * result + (encoding != null ? encoding.hashCode() : 0);
return result;
}
}