JeffGrigg

BestWayToWriteMethod

Mar 25th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.04 KB | None | 0 0
  1. import junit.framework.AssertionFailedError;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.*;
  5. import java.util.logging.Logger;
  6. import java.util.stream.Stream;
  7.  
  8. public class BestWayToWriteMethod {
  9.  
  10.     private static final Logger logger = Logger.getLogger("BestWayToWriteMethod");
  11.  
  12.     private final FileSystem _fileSystem;
  13.  
  14.     public BestWayToWriteMethod() {
  15.         this(FileSystems.getDefault());
  16.     }
  17.  
  18.     public BestWayToWriteMethod(final FileSystem fileSystem) {
  19.         _fileSystem = fileSystem;
  20.     }
  21.  
  22.     public void processFile(final String fileName) throws IOException {
  23.         class LoopVariables {
  24.             int lineNumber = 0;
  25.             int detailRecordCount = 0;
  26.             boolean trailerRecordSeen = false;
  27.         }
  28.         final LoopVariables vars = new LoopVariables();
  29.         try (Stream<String> stream = Files.lines(_fileSystem.getPath(fileName))) {
  30.             stream.forEach(line -> {
  31.                 ++vars.lineNumber;
  32.                 final String recordType = line.substring(0, 2);
  33.  
  34.                 if (vars.trailerRecordSeen) {
  35.                     // Not allowed to have more records after the trailer.
  36.                     throw new AssertionFailedError("Invalid record after trailer on line " + vars.lineNumber + ": " + line);
  37.                 } else if (vars.lineNumber == 1) {
  38.                     // The first record in the file must be the header record.
  39.                     if (!recordType.equals("01")) {
  40.                         throw new AssertionFailedError("First record must be a header record. Found record type '" + recordType + "' instead. Line #1 = " + line);
  41.                     }
  42.                 } else {
  43.                     switch (recordType) {
  44.  
  45.                         case "01":  // Out of place Header Record
  46.                             throw new AssertionFailedError("Duplicate header record found at line " + vars.lineNumber + ": " + line);
  47.  
  48.                         case "02":  // Detail Record
  49.                             ++vars.detailRecordCount;
  50.                             // TODO: Presumably more "detail record procesing" would be done here.
  51.                             break;
  52.  
  53.                         case "99":  // Trailer Record
  54.                             vars.trailerRecordSeen = true;
  55.                             final String rawRecordCount = line.substring(12, 21);
  56.                             final int recordCount = Integer.parseInt(rawRecordCount);
  57.                             if (recordCount != vars.detailRecordCount) {
  58.                                 throw new AssertionFailedError("Trailer record has incorrect record count. Trailer record has " + recordCount + ". Actual detail record count " + vars.detailRecordCount + ".");
  59.                             }
  60.                             break;
  61.  
  62.                         default:
  63.                             throw new AssertionFailedError("Unrecognized record type '" + recordType + "' found at line " + vars.lineNumber + ": " + line);
  64.                     }
  65.                 }
  66.             });
  67.         }
  68.     }
  69.  
  70. }
Add Comment
Please, Sign In to add comment