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

Report this snippet  

You need to login to post a comment.