View Javadoc
1   package info.mikethomas.fahview.v6project.model;
2   
3   /*
4    * #%L
5    * This file is part of FAHView-v6project.
6    * %%
7    * Copyright (C) 2011 - 2017 Mike Thomas <mikepthomas@outlook.com>
8    * %%
9    * FAHView is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * %
14   * FAHView is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * %
19   * You should have received a copy of the GNU General Public License
20   * along with FAHView.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import info.mikethomas.fahview.v6project.utilities.QueueReader;
25  import java.util.Date;
26  
27  /**
28   * Class to represent data stored about a Folding@home work unit.
29   *
30   * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
31   * @version 6.00
32   */
33  public class WorkUnitImpl implements WorkUnit {
34      private int proj; // 0 Project number (LE)
35      private int run; // 2 Run (LE)
36      private int clone; // 4 Clone (LE)
37      private int gen; // 6 Generation (LE)
38      private Date issue; // 8 WU issue time (LE)
39  
40      private String protein, percentage;
41      private int completed, steps;
42      
43      private int indexNumber, position;
44      private QueueReader reader;
45  
46      /**
47       * Work Unit constructor. Sets the initial values.
48       *
49       * @param indexNumber a int.
50       * @param reader a {@link info.mikethomas.fahview.v6project.utilities.QueueReader} object.
51       * @throws java.lang.InstantiationException if any.
52       */
53      public WorkUnitImpl(int indexNumber, QueueReader reader) throws InstantiationException {
54          percentage = "0%";
55          this.indexNumber = indexNumber;
56          switch (indexNumber) {
57              case 0:
58                  position = QueueImpl.QUEUE_INDEX_0_POS + QueueIndexImpl.WUID_POS;
59                  break;
60              case 1:
61                  position = QueueImpl.QUEUE_INDEX_1_POS + QueueIndexImpl.WUID_POS;
62                  break;
63              case 2:
64                  position = QueueImpl.QUEUE_INDEX_2_POS + QueueIndexImpl.WUID_POS;
65                  break;
66              case 3:
67                  position = QueueImpl.QUEUE_INDEX_3_POS + QueueIndexImpl.WUID_POS;
68                  break;
69              case 4:
70                  position = QueueImpl.QUEUE_INDEX_4_POS + QueueIndexImpl.WUID_POS;
71                  break;
72              case 5:
73                  position = QueueImpl.QUEUE_INDEX_5_POS + QueueIndexImpl.WUID_POS;
74                  break;
75              case 6:
76                  position = QueueImpl.QUEUE_INDEX_6_POS + QueueIndexImpl.WUID_POS;
77                  break;
78              case 7:
79                  position = QueueImpl.QUEUE_INDEX_7_POS + QueueIndexImpl.WUID_POS;
80                  break;
81              case 8:
82                  position = QueueImpl.QUEUE_INDEX_8_POS + QueueIndexImpl.WUID_POS;
83                  break;
84              case 9:
85                  position = QueueImpl.QUEUE_INDEX_9_POS + QueueIndexImpl.WUID_POS;
86                  break;
87              default:
88                  throw new InstantiationException("Queue index should be 0-9.");
89          }
90          this.reader = reader;
91          update();
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public int getProj() {
97          return proj;
98      }
99      
100     /** {@inheritDoc} */
101     @Override
102     public int getRun() {
103         return run;
104     }
105     
106     /** {@inheritDoc} */
107     @Override
108     public int getClone() {
109         return clone;
110     }
111     
112     /** {@inheritDoc} */
113     @Override
114     public int getGen() {
115         return gen;
116     }
117     
118     /** {@inheritDoc} */
119     @Override
120     public Date getIssue() {
121         return issue;
122     }
123 
124     /**
125      * Set the value of Project Number
126      */
127     protected void setProj() {
128         proj = reader.readLEUShort(PROJ_POS + position, PROJ_LENGTH);
129     }
130 
131     /**
132      * Set the value of Run
133      */
134     protected void setRun() {
135         run = reader.readLEUShort(RUN_POS + position, RUN_LENGTH);
136     }
137 
138     /**
139      * Set the value of Clone
140      */
141     protected void setClone() {
142         clone = reader.readLEUShort(CLONE_POS + position, CLONE_LENGTH);
143     }
144 
145     /**
146      * Set the value of Generation
147      */
148     protected void setGen() {
149         gen = reader.readLEUShort(GEN_POS + position, GEN_LENGTH);
150     }
151 
152     /**
153      * Set the value of Issued
154      */
155     protected void setIssue() {
156         long epoch = reader.readLEUInt(ISSUE_POS + position, ISSUE_LENGTH);
157         issue = new Date(epoch * 1000);
158     }
159 
160     /**
161      * Set the number of steps completed
162      *
163      * @param line a {@link java.lang.String} object.
164      */
165     protected void setCompleted(String line) {
166         String[] values = line.split(" ");
167         completed = Integer.parseInt(values[2]);
168         steps = Integer.parseInt(values[5]);
169         percentage = values[8].substring(1, values[8].length() - 1);
170     }
171 
172     /**
173      * <p>Getter for the field <code>completed</code>.</p>
174      *
175      * @return a int.
176      */
177     public int getCompleted() {
178         return completed;
179     }
180 
181     /**
182      * <p>Getter for the field <code>percentage</code>.</p>
183      *
184      * @return a {@link java.lang.String} object.
185      */
186     public String getPercentage() {
187         return percentage;
188     }
189 
190     /**
191      * <p>Getter for the field <code>protein</code>.</p>
192      *
193      * @return a {@link java.lang.String} object.
194      */
195     public String getProtein() {
196         return protein;
197     }
198 
199     /**
200      * <p>Getter for the field <code>steps</code>.</p>
201      *
202      * @return a int.
203      */
204     public int getSteps() {
205         return steps;
206     }
207     
208     /**
209      * {@inheritDoc}
210      *
211      * Generate a String representation of the WorkUnitImpl
212      */
213     @Override
214     public String toString() {
215         String result = "";
216         result += "queue.index[" + indexNumber + "].wuid.proj\t" + getProj();
217         result += "\nqueue.index[" + indexNumber + "].wuid.run\t" + getRun();
218         result += "\nqueue.index[" + indexNumber + "].wuid.clone\t" + getClone();
219         result += "\nqueue.index[" + indexNumber + "].wuid.gen\t" + getGen();
220         result += "\nqueue.index[" + indexNumber + "].wuid.issue\t" + getIssue();
221         return result;
222     }
223     
224     /** {@inheritDoc} */
225     @Override
226     public final void update() {
227         setProj();
228         setRun();
229         setClone();
230         setGen();
231         setIssue();
232     }
233 }