View Javadoc
1   package emissary.pickup;
2   
3   import jakarta.annotation.Nullable;
4   
5   import java.io.DataInputStream;
6   import java.io.DataOutputStream;
7   import java.io.IOException;
8   
9   /**
10   * A WorkUnit is a unit of work a worker will process. The idea is to replace fileNameList. Currently, WorkBundle is set
11   * to only have one file, and so there will only be one WorkUnit.
12   */
13  public final class WorkUnit {
14      private String fileName;
15      private String transactionId;
16      // worker updates this boolean
17      private boolean failedToParse = false;
18      private boolean failedToProcess = false;
19  
20      /**
21       * Constructs WorkUnit with specified filename
22       * 
23       * @param fileName the associated filename
24       */
25      WorkUnit(@Nullable String fileName) {
26          this.fileName = fileName;
27      }
28  
29  
30      /**
31       * Constructs WorkUnit with specified filename, transactionId, failedToParse, failedToProcess
32       *
33       * @param fileName the associated filename
34       * @param transactionId the associated transactionId
35       * @param failedToParse the status of failed to parse
36       * @param failedToProcess the status of failed to process
37       */
38      WorkUnit(String fileName, String transactionId, boolean failedToParse, boolean failedToProcess) {
39          this.fileName = fileName;
40          this.transactionId = transactionId;
41          this.failedToParse = failedToParse;
42          this.failedToProcess = failedToProcess;
43      }
44  
45      public static WorkUnit readFromStream(DataInputStream in) throws IOException {
46          final WorkUnit u = new WorkUnit(null);
47          u.fileName = WorkBundle.readUtfOrNull(in);
48          u.transactionId = WorkBundle.readUtfOrNull(in);
49          u.failedToParse = in.readBoolean();
50          u.failedToProcess = in.readBoolean();
51          return u;
52      }
53  
54      public void writeToStream(DataOutputStream out) throws IOException {
55          WorkBundle.writeUtfOrNull(fileName, out);
56          WorkBundle.writeUtfOrNull(transactionId, out);
57          out.writeBoolean(failedToParse);
58          out.writeBoolean(failedToProcess);
59      }
60  
61      /**
62       * Gets the filename for the WorkUnit
63       * 
64       * @return the filename
65       */
66      public String getFileName() {
67          return fileName;
68      }
69  
70      /**
71       * Sets the filename for the WorkUnit
72       * 
73       * @param fileName the filename
74       */
75      public void setFilename(String fileName) {
76          this.fileName = fileName;
77      }
78  
79      /**
80       * Gets the transaction Id of the WorkUnit
81       * 
82       * @return the transaction Id
83       */
84      public String getTransactionId() {
85          return transactionId;
86      }
87  
88      /**
89       * Sets the transaction id of the WorkUnit
90       * 
91       * @param transactionId the transaction id to set
92       */
93      public void setTransactionId(String transactionId) {
94          this.transactionId = transactionId;
95      }
96  
97      /**
98       * Sets the WorkUnit as a failed to parse WorkUnit
99       */
100     public void setFailedToParse() {
101         this.failedToParse = true;
102     }
103 
104     /**
105      * Gets the status of whether the WorkUnit failed to parse
106      * 
107      * @return the boolean status of failed to parse
108      */
109     public boolean failedToParse() {
110         return failedToParse;
111     }
112 
113     /**
114      * Sets the status of whether the WorkUnit had an error in processing
115      */
116     public void setFailedToProcess() {
117         this.failedToProcess = true;
118     }
119 
120     /**
121      * Gets the status of whether file had an error in processing.
122      * 
123      * @return the boolean status of file process
124      */
125     public boolean failedToProcess() {
126         return failedToProcess;
127     }
128 }