/trunk/FAHView-v6project/src/main/java/com/googlecode/fahview/v6project/model/WorkUnitImpl.java

https://gitlab.com/BGCX067/fahview-svn-to-git · Java · 233 lines · 126 code · 23 blank · 84 comment · 1 complexity · 121b89632fb491c3f8a557b32bb42e91 MD5 · raw file

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