Class StringFormatter

java.lang.Object
it.unimi.di.prog2.h15.StringFormatter
All Implemented Interfaces:
Iterable<String>

public class StringFormatter extends Object implements Iterable<String>
A class to collect a sequence of strings and format them.

This class collects strings and nulls and can return the sequence both via toString() that concatenates non-null strings with a space, inserting a newline for each null, and via an iterator that returns just the non-null strings.

  • Field Details

    • string

      private final List<String> string
      The list of added strings.
  • Constructor Details

    • StringFormatter

      public StringFormatter()
      Creates an empty formatter.
  • Method Details

    • add

      public void add(String s)
      Adds a string, or null, to the formatter.
      Parameters:
      s - the string to add, or null.
    • toString

      public String toString()
      Returns a string version of this formatter, non-null strings will be concatenated with a space, and a newline will be inserted for each null.
      Overrides:
      toString in class Object
    • iterator

      public Iterator<String> iterator()
      Returns an iterator over the non-null strings in this formatter.
      Specified by:
      iterator in interface Iterable<String>