Skip to content

Instantly share code, notes, and snippets.

@up1
Last active February 19, 2016 14:06
Show Gist options
  • Select an option

  • Save up1/c45429f2ec73c6ccdc33 to your computer and use it in GitHub Desktop.

Select an option

Save up1/c45429f2ec73c6ccdc33 to your computer and use it in GitHub Desktop.
Check Null
import java.io.File;
public class CallReport {
public static void main(String ... args) {
Report report = new NotNullReport(new FileNotExistsReport(new DefaultReport()));
report.export(null); //Throw error from null
report.export(new File("existing.pdf")); //Throw error from existing file
report.export(new File("new.pdf")); //Success
}
}
import java.io.File;
public interface Report {
void export(File file);
}
class DefaultReport implements Report {
@Override
public void export(File file) {
}
}
class FileNotExistsReport implements Report {
private final Report originalReport;
public FileNotExistsReport(Report report) {
this.originalReport = report;
}
@Override
public void export(File file) {
if(file.exists()) {
throw new IllegalArgumentException( "File already exists." );
}
System.out.println(file.getAbsoluteFile());
this.originalReport.export(file);
}
}
class NotNullReport extends FileNotExistsReport {
private final Report originalReport;
public NotNullReport(Report report) {
super(report);
this.originalReport = report;
}
@Override
public void export(File file) {
if(file == null) {
throw new IllegalArgumentException( "File is NULL; can't export." );
}
this.originalReport.export(file);
}
}
import java.io.File;
class Report {
void export(File file) {
if (file == null) {
throw new IllegalArgumentException( "File is NULL; can't export." );
}
if (file.exists()) {
throw new IllegalArgumentException( "File already exists." );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment