fork download
  1. import java.util.*;
  2.  
  3. class CodeTable extends Hashtable
  4. {
  5. private int totalToDo = 0;
  6. private int totalInWait = 0;
  7.  
  8. public int getTotalInWait()
  9. {
  10. return totalInWait;
  11. }
  12.  
  13. public int getTotalToDo()
  14. {
  15. return totalToDo;
  16. }
  17.  
  18. /*
  19.   * Override put() method of Hashtable
  20.   */
  21. public synchronized Object put(Object lineNumber, Object value)
  22. {
  23. Command command = (Command)value;
  24. Object old = super.put(lineNumber, command);
  25. if (old == null)
  26. {
  27. totalToDo += command.numToDo;
  28. return null;
  29. }
  30. else
  31. {
  32. Command oldCommand = (Command)old;
  33. totalToDo += command.numToDo - oldCommand.numToDo;
  34. return oldCommand;
  35. }
  36. }
  37.  
  38. public boolean getN(int lineNum)
  39. {
  40. Integer lineNumber = new Integer(lineNum);
  41. return getN(lineNumber);
  42. }
  43.  
  44. public boolean getN(Integer lineNumber)
  45. {
  46. Command command = (Command)this.get(lineNumber);
  47. if (command == null)
  48. return false;
  49. return command.numToDo > 0;
  50. }
  51.  
  52. public int getNumToDo(int lineNum)
  53. {
  54. Integer lineNumber = new Integer(lineNum);
  55. return getNumToDo(lineNumber);
  56. }
  57.  
  58. public int getNumToDo(Integer lineNumber)
  59. {
  60. Command command = (Command)this.get(lineNumber);
  61. if (command == null)
  62. return 0;
  63. return command.numToDo;
  64. }
  65.  
  66. public int changeNumToDo(int lineNum, int change)
  67. {
  68. Integer lineNumber = new Integer(lineNum);
  69. return changeNumToDo(lineNumber, change);
  70. }
  71.  
  72. public int changeNumToDo(Integer lineNumber, int change)
  73. {
  74. if (lineNumber.intValue() < 0)
  75. {
  76. lineNumber = new Integer(-lineNumber.intValue());
  77. change = -change;
  78. }
  79. Command command = (Command)this.get(lineNumber);
  80. if (command == null) // need to do something here to implement adding extra line numbers
  81. return 0;
  82. command.numToDo += change;
  83. totalToDo += change;
  84. if (command.numToDo < 0)
  85. {
  86. totalToDo -= command.numToDo;
  87. command.numToDo = 0;
  88. }
  89. this.put(lineNumber, command);
  90. return command.numToDo;
  91. }
  92.  
  93. public void doCommand(int instanceToDo) throws NoSuchElementException
  94. {
  95. // get the actual command to be executed by iterating through the commands until we pass the required number
  96. Enumeration enumm = this.keys();
  97. Integer lineNumber = (Integer)enumm.nextElement();
  98. Command command = (Command)this.get(lineNumber);
  99. while (!(command.available) || (instanceToDo > command.numToDo))
  100. {
  101. if (command.available)
  102. {
  103. instanceToDo -= command.numToDo;
  104. }
  105. lineNumber = (Integer)enumm.nextElement();
  106. command = (Command)this.get(lineNumber);
  107. }
  108. if (WheneverCode.traceLevel >= 2)
  109. System.out.println("[Attempting line " + lineNumber.toString() + "]");
  110. // execute the command, passing the line number so it know what its line number is
  111. if (!command.execute(lineNumber))
  112. {
  113. // command was deferred.
  114. // remove command from the list of things to do.
  115. if (WheneverCode.traceLevel >= 2)
  116. System.out.println("[Will not try deferred line " + lineNumber.toString() + " again for now]");
  117. command.available = false;
  118. totalToDo -= command.numToDo;
  119. totalInWait += command.numToDo;
  120. }
  121. else
  122. {
  123. if (WheneverCode.traceLevel == 1)
  124. System.out.println("[Succeeded line " + lineNumber.toString() + "]");
  125. // command was executed.
  126. // set all commands as available.
  127. enumm = this.keys();
  128. while (enumm.hasMoreElements())
  129. {
  130. lineNumber = (Integer)enumm.nextElement();
  131. command = (Command)this.get(lineNumber);
  132. if (!command.available)
  133. {
  134. command.available = true;
  135. if (WheneverCode.traceLevel >= 2)
  136. System.out.println("[Making deferred line " + lineNumber.toString() + " available again for now]");
  137. }
  138. totalToDo += totalInWait;
  139. totalInWait = 0;
  140. }
  141. }
  142. }
  143. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty