Last active
February 19, 2016 14:06
-
-
Save up1/c45429f2ec73c6ccdc33 to your computer and use it in GitHub Desktop.
Check Null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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