Posted By

obrienm on 08/29/14


Tagged


Versions (?)

Doublets - Chain


 / Published in: Java
 

Doublets Project

  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3.  
  4. public class Chain implements Iterable<String> {
  5.  
  6. // A HashMap that has individual words as keys and position numbers as values in the chain.
  7. // This structure preserves order while allowing for a constant time "contains" method.
  8. HashMap<String, Integer> wordChain;
  9. int length; // The length of the chain.
  10. String lastWord; // Last word in the current chain.
  11.  
  12. /**
  13. * Creates a new HashMap for the given chain, sets its length to 0 and its
  14. * last word to nothing.
  15. */
  16. public Chain() {
  17. wordChain = new HashMap<String, Integer>();
  18. length = 0;
  19. lastWord = "";
  20. }
  21.  
  22. /**
  23. * Constructor used when returning a new chain after adding since they are
  24. * immutable.
  25. *
  26. * @param Takes the current list as a parameter.
  27. */
  28. private Chain(HashMap<String, Integer> list) {
  29. wordChain = list;
  30. }
  31.  
  32. /**
  33. * Since they are immutable, uses a clone and the second constructor to get
  34. * a new chain and then add on the new word.
  35. *
  36. * @param Word to be added onto current chain.
  37. * @return A new chain with the added word.
  38. */
  39. public Chain addLast(String word) {
  40. @SuppressWarnings("unchecked")
  41. HashMap<String, Integer> newList = (HashMap<String, Integer>) this.wordChain
  42. .clone();
  43. newList.put(word, this.length);
  44. Chain newChain = new Chain(newList);
  45. newChain.length = this.length + 1;
  46. newChain.lastWord = word;
  47. return newChain;
  48. }
  49.  
  50. /**
  51. * Returns the last word in the chain.
  52. *
  53. * @return the last word in the chain.
  54. */
  55. public String getLast() {
  56. return lastWord;
  57. }
  58.  
  59. /**
  60. * Returns the last word in the chain.
  61. *
  62. * @return the length of the chain.
  63. */
  64. public int length() {
  65. return this.length;
  66. }
  67.  
  68. /**
  69. * Checks to see if the word chain contains the given word.
  70. *
  71. * @param Word
  72. * that is checked to see if it is contained.
  73. * @return a boolean indicating whether or not it is contained.
  74. */
  75. public boolean contains(String s) {
  76. return this.wordChain.containsKey(s);
  77. }
  78.  
  79. /**
  80. * Returns a chain iterator that is used in the to string method.
  81. */
  82. @Override
  83. public Iterator<String> iterator() {
  84. return new ChainIterator(this.wordChain, this.length);
  85.  
  86. }
  87.  
  88. /**
  89. * To string method that uses a chain iterator and string builder.
  90. *
  91. * @return the string version of the chain.
  92. */
  93. public String toString() {
  94. StringBuilder build = new StringBuilder();
  95. build.append("[");
  96. Iterator<String> iter = iterator();
  97. while (iter.hasNext()) {
  98. build.append(iter.next() + ", ");
  99. }
  100. return build.substring(0, build.length() - 2) + "]";
  101. }
  102.  
  103. }
  104.  
  105. class ChainIterator implements Iterator<String> {
  106.  
  107. String[] wordList; // An array that is used to put the words proper order.
  108. int current; // Used to indicate which word we are currently on in iteration.
  109.  
  110. /**
  111. * Fills out an array using the position values from the map to put the
  112. * words in order so we can iterate through them.
  113. *
  114. * @param The HashMap that represents the chain words along with its
  115. * position.
  116. * @param Thelength of the chain.
  117. */
  118. public ChainIterator(HashMap<String, Integer> map, int length) {
  119. wordList = new String[length];
  120. for (String s : map.keySet()) {
  121. wordList[map.get(s)] = s;
  122. }
  123. current = 0;
  124. }
  125.  
  126. /**
  127. * @return A boolean that determines if it has a next or not based on where
  128. * the current index is pointing.
  129. */
  130. @Override
  131. public boolean hasNext() {
  132. return current < wordList.length;
  133. }
  134.  
  135. /**
  136. * Returns the current word and moves the current index up one so it is
  137. * ready for the next call.
  138. *
  139. * @return A word at the given index
  140. */
  141. @Override
  142. public String next() {
  143. String s = wordList[current];
  144. current++;
  145. return s;
  146. }
  147.  
  148. @Override
  149. public void remove() {
  150. // Do not remove things!
  151. }
  152.  
  153. }

Report this snippet  

You need to login to post a comment.