import java.util.HashSet; import java.util.Set; /* WordInfo represents a word and its set of associations. * Associations are unlabeled, one directional connections between words * * For example, the abstraction for an example WordInfo object may be as follows: * * Computer ----> {"code","hello world", "programming","fun"} * */ public class WordInfo { // Represents the source of the word association private String word; // Represents the collection of associations from the source private Set associations; /* Initializes a word info without any exisiting associations */ public WordInfo(String word) { this.word = word; this.associations = new HashSet(); } // Returns the source of the WordInfo public String getWord() { return this.word; } // Creates a new WordInfo with the given source word and an existing set of associations public WordInfo(String word, Set associations) { this.word = word; this.associations = associations; } // Adds an association between the source word and the given String public boolean addAssociation(String association) { return associations.add(association); } // Removes an association between the source and the given string public void removeAssociation(String association) { associations.remove(association); } // Returns set of all association from the source word public Set getAssociations() { return associations; } // Returns string representation of this WordInfo public String toString() { return word + ": " + associations; } // Returns whether this is equal to o. // Equality for two WordInfo classes is defined by having the same // word. public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof WordInfo)) { return false; } WordInfo other = (WordInfo) o; return this.getWord().equals(other.getWord()); } // Returns an int representing the hashCode of this WordInfo object. // Given two WordInfo objects with the same data, each object will // return the same hashCode. public int hashCode() { return Math.abs(this.word.hashCode() * 31); } }