Class TerminalSize


  • public class TerminalSize
    extends java.lang.Object
    Terminal dimensions in 2-d space, measured in number of rows and columns. This class is immutable and cannot change its internal state after creation.
    • Constructor Summary

      Constructors 
      Constructor Description
      TerminalSize​(int columns, int rows)
      Creates a new terminal size representation with a given width (columns) and height (rows)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      int getColumns()  
      int getRows()  
      int hashCode()  
      TerminalSize max​(TerminalSize other)
      Takes a different TerminalSize and returns a new TerminalSize that has the largest dimensions of the two, measured separately.
      TerminalSize min​(TerminalSize other)
      Takes a different TerminalSize and returns a new TerminalSize that has the smallest dimensions of the two, measured separately.
      java.lang.String toString()  
      TerminalSize with​(TerminalSize size)
      Returns itself if it is equal to the supplied size, otherwise the supplied size.
      TerminalSize withColumns​(int columns)
      Creates a new size based on this size, but with a different width
      TerminalSize withRelative​(int deltaColumns, int deltaRows)
      Creates a new TerminalSize object representing a size based on this object's size but with a delta applied.
      TerminalSize withRelative​(TerminalSize delta)
      Creates a new TerminalSize object representing a size based on this object's size but with a delta applied.
      TerminalSize withRelativeColumns​(int delta)
      Creates a new TerminalSize object representing a size with the same number of rows, but with a column size offset by a supplied value.
      TerminalSize withRelativeRows​(int delta)
      Creates a new TerminalSize object representing a size with the same number of columns, but with a row size offset by a supplied value.
      TerminalSize withRows​(int rows)
      Creates a new size based on this size, but with a different height
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • columns

        private final int columns
      • rows

        private final int rows
    • Constructor Detail

      • TerminalSize

        public TerminalSize​(int columns,
                            int rows)
        Creates a new terminal size representation with a given width (columns) and height (rows)
        Parameters:
        columns - Width, in number of columns
        rows - Height, in number of columns
    • Method Detail

      • getColumns

        public int getColumns()
        Returns:
        Returns the width of this size representation, in number of columns
      • withColumns

        public TerminalSize withColumns​(int columns)
        Creates a new size based on this size, but with a different width
        Parameters:
        columns - Width of the new size, in columns
        Returns:
        New size based on this one, but with a new width
      • getRows

        public int getRows()
        Returns:
        Returns the height of this size representation, in number of rows
      • withRows

        public TerminalSize withRows​(int rows)
        Creates a new size based on this size, but with a different height
        Parameters:
        rows - Height of the new size, in rows
        Returns:
        New size based on this one, but with a new height
      • withRelativeColumns

        public TerminalSize withRelativeColumns​(int delta)
        Creates a new TerminalSize object representing a size with the same number of rows, but with a column size offset by a supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return a terminal size delta number of columns wider and for negative numbers shorter.
        Parameters:
        delta - Column offset
        Returns:
        New terminal size based off this one but with an applied transformation
      • withRelativeRows

        public TerminalSize withRelativeRows​(int delta)
        Creates a new TerminalSize object representing a size with the same number of columns, but with a row size offset by a supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return a terminal size delta number of rows longer and for negative numbers shorter.
        Parameters:
        delta - Row offset
        Returns:
        New terminal size based off this one but with an applied transformation
      • withRelative

        public TerminalSize withRelative​(TerminalSize delta)
        Creates a new TerminalSize object representing a size based on this object's size but with a delta applied. This is the same as calling withRelativeColumns(delta.getColumns()).withRelativeRows(delta.getRows())
        Parameters:
        delta - Column and row offset
        Returns:
        New terminal size based off this one but with an applied resize
      • withRelative

        public TerminalSize withRelative​(int deltaColumns,
                                         int deltaRows)
        Creates a new TerminalSize object representing a size based on this object's size but with a delta applied. This is the same as calling withRelativeColumns(deltaColumns).withRelativeRows(deltaRows)
        Parameters:
        deltaColumns - How many extra columns the new TerminalSize will have (negative values are allowed)
        deltaRows - How many extra rows the new TerminalSize will have (negative values are allowed)
        Returns:
        New terminal size based off this one but with an applied resize
      • max

        public TerminalSize max​(TerminalSize other)
        Takes a different TerminalSize and returns a new TerminalSize that has the largest dimensions of the two, measured separately. So calling 3x5 on a 5x3 will return 5x5.
        Parameters:
        other - Other TerminalSize to compare with
        Returns:
        TerminalSize that combines the maximum width between the two and the maximum height
      • min

        public TerminalSize min​(TerminalSize other)
        Takes a different TerminalSize and returns a new TerminalSize that has the smallest dimensions of the two, measured separately. So calling 3x5 on a 5x3 will return 3x3.
        Parameters:
        other - Other TerminalSize to compare with
        Returns:
        TerminalSize that combines the minimum width between the two and the minimum height
      • with

        public TerminalSize with​(TerminalSize size)
        Returns itself if it is equal to the supplied size, otherwise the supplied size. You can use this if you have a size field which is frequently recalculated but often resolves to the same size; it will keep the same object in memory instead of swapping it out every cycle.
        Parameters:
        size - Size you want to return
        Returns:
        Itself if this size equals the size passed in, otherwise the size passed in
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object