Class Size

java.lang.Object
swingtree.layout.Size

@Immutable public final class Size extends Object
An immutable value object that represents a size in the form of a width and height or lack thereof. This is used to represent the size and layout dimensions of components in the SwingTree style API, as well as the size of icons as part of IconDeclarations. See UIForAnySwing.withStyle(Styler), ComponentStyleDelegate.image(swingtree.api.Configurator) and ImageConf.size(int, int) for examples of where and how this class is used.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
     
    boolean
    Allows you to check if the height value of this Size instance is both specified (non-negative) and also positive.
    boolean
    Allows you to check if the width value of this Size instance is both specified (non-negative) and also positive.
    The height of this Size instance may not be specified, in which case this method returns Optional.empty() and the thing that this configuration is applied to should resort to its default height.
    static Size
    of(double width, double height)
    A factory method that creates a Size instance from a width and height.
    static Size
    of(float width, float height)
    A factory method that creates a Size instance from a width and height.
    static Size
    of(@Nullable Dimension dimension)
    A factory method that creates a Size instance from a Dimension.
    Rounds the float based width and height of this Size to the nearest integer value and returns these as a new Size.
    scale(double scaleFactor)
    Scales the width and height of this Size instance by the given factor.
     
     
    static Size
    Exposes the UNKNOWN size instance, which is a null object that represents an unknown size.
    The width of this Size instance may not be specified, in which case this method returns Optional.empty() and the thing that this configuration is applied to should resort to its default width.
    withHeight(double height)
    Creates an updated Size instance with the given height.
    withWidth(double width)
    Creates an updated Size instance with the given width.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • unknown

      public static Size unknown()
      Exposes the UNKNOWN size instance, which is a null object that represents an unknown size. It uses -1 for both width and height and will return Optional.empty() for both width and height.
      Returns:
      A Size instance that represents an unknown size.
    • of

      public static Size of(float width, float height)
      A factory method that creates a Size instance from a width and height. If the width or height is negative, the returned size will be the UNKNOWN size constant with a width or height of -1.
      Parameters:
      width - The width of the size in the form of a float.
      height - The height of the size in the form of a float.
      Returns:
      A Size instance that represents the given width and height.
    • of

      public static Size of(double width, double height)
      A factory method that creates a Size instance from a width and height. If the width or height is negative, the returned size will be the UNKNOWN size constant with a width or height of -1.
      Parameters:
      width - The width of the size in the form of a double.
      height - The height of the size in the form of a double.
      Returns:
      A Size instance that represents the given width and height.
    • of

      public static Size of(@Nullable Dimension dimension)
      A factory method that creates a Size instance from a Dimension.
      Parameters:
      dimension - The dimension to convert to a Size instance.
      Returns:
      A Size instance that represents the given dimension.
    • width

      public Optional<Float> width()
      The width of this Size instance may not be specified, in which case this method returns Optional.empty() and the thing that this configuration is applied to should resort to its default width.
      Returns:
      The width of this Size instance or Optional.empty() if unknown.
    • height

      public Optional<Float> height()
      The height of this Size instance may not be specified, in which case this method returns Optional.empty() and the thing that this configuration is applied to should resort to its default height.
      Returns:
      The height of this Size instance or Optional.empty() if unknown.
    • hasPositiveWidth

      public boolean hasPositiveWidth()
      Allows you to check if the width value of this Size instance is both specified (non-negative) and also positive.
      Returns:
      True if the width is both specified and positive, false otherwise.
    • hasPositiveHeight

      public boolean hasPositiveHeight()
      Allows you to check if the height value of this Size instance is both specified (non-negative) and also positive.
      Returns:
      True if the height is both specified and positive, false otherwise.
    • withWidth

      public Size withWidth(double width)
      Creates an updated Size instance with the given width. If the width is negative, the width of the returned size will be -1.
      Parameters:
      width - The width of the size to create.
      Returns:
      A new Size instance with the given width.
    • withHeight

      public Size withHeight(double height)
      Creates an updated Size instance with the given height. If the height is negative, the height of the returned size will be -1.
      Parameters:
      height - The height of the size to create.
      Returns:
      A new Size instance with the given height.
    • toDimension

      public Dimension toDimension()
    • scale

      public Size scale(double scaleFactor)
      Scales the width and height of this Size instance by the given factor. If the width or height not specified (i.e. negative), they will remain negative. A negative scale factor will result in a negative width and height.
      Parameters:
      scaleFactor - The factor to scale the width and height by.
      Returns:
      A new Size instance with the scaled width and height.
    • round

      public Size round()
      Rounds the float based width and height of this Size to the nearest integer value and returns these as a new Size.
      Returns:
      A Size objects whose width and height values does not have fractions.
    • toString

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object