Package cs2110

Class WordOccurrences

java.lang.Object
cs2110.WordOccurrences
All Implemented Interfaces:
Comparable<WordOccurrences>

public class WordOccurrences extends Object implements Comparable<WordOccurrences>
Keeps track of which line numbers of which sources (i.e., files) contain a certain word. All occurrences for a given source must be recorded before recording any occurrences in another source.

The natural ordering of `WordOccurrences` is the same as the natural ordering of their words. This is inconsistent with `equals()`.

  • Constructor Details Link icon

    • WordOccurrences Link icon

      public WordOccurrences(String word)
      Create an object to track occurrences of the word `word`. Initially has no occurrences.
  • Method Details Link icon

    • word Link icon

      public String word()
      Return the word we are tracking occurrences of.
    • sources Link icon

      public Iterable<SourceLines> sources()
      Return the sources (associated with their matching line numbers) that contain our word, in the order the sources were recorded in. Clients must not attempt to mutate the returned object.
    • addOccurrence Link icon

      public boolean addOccurrence(String sourceName, int lineNumber)
      Record that line `lineNumber` of source `sourceName` contains our word. Returns whether this line of the source was already known to contain our word. Requires `sourceName` is either the same as the last added source name or has never been added, and lineNumber is not less than last line number added with this source name (if any).
    • compareTo Link icon

      public int compareTo(WordOccurrences other)
      Specified by:
      compareTo in interface Comparable<WordOccurrences>
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • equals Link icon

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode Link icon

      public int hashCode()
      Overrides:
      hashCode in class Object