Top Description Fields Constructors Methods
io.github.kusoroadeolu.clique.components

public Class Box

extends Object
implements Component
Class Inheritance
All Implemented Interfaces
io.github.kusoroadeolu.clique.components.Component
Annotations
@InternalApi
since:3.2.0
Imports
io.github.kusoroadeolu.clique.configuration.BoxConfiguration, .BoxType, .TextAlign, io.github.kusoroadeolu.clique.internal.BorderChars, .BoxWrapper, .WidthAwareList, io.github.kusoroadeolu.clique.internal.documentation.InternalApi, io.github.kusoroadeolu.clique.internal.exception.InvalidDimensionException, io.github.kusoroadeolu.clique.internal.utils.StringUtils, java.util.Objects

A single-cell bordered container that renders text content within a styled box.

Boxes can be sized explicitly via dimensions(int, int) or left to autosize based on content. When no dimensions are set, width and height are derived from the longest line and line count of the content respectively.

The rendered string is cached after the first call to get() and invalidated whenever content(String) or content(String, TextAlign) is called.

Note

This class is not thread-safe.

Since
1.1.0

Field Summary

Modifier and TypeField and Description
private TextAlign
private final BorderChars
private static final String
private String
private final BoxConfiguration
private String
private int
private static final String
private int

Constructor Summary

AccessConstructor and Description
public
Box(BoxType type, BoxConfiguration configuration)

public
Box(BoxType type)

public
Box(BoxConfiguration configuration)

public
Box()

Method Summary

Modifier and TypeMethod and Description
public Box

Returns:

this box instance
content
(String
the text content to display; must not be null
content
)

Sets the content of this box and invalidates the render cache.

public Box

Returns:

this box instance
content
(String
the text content to display; must not be null
content
,
TextAlign
the text alignment to apply; must not be null
align
)

Sets the content and text alignment of this box, invalidating the render cache.

public Box

Returns:

this box instance
dimensions
(int
the total outer width of the box; must be greater than 0
width
,
int
the total outer height of the box; must be greater than 0
height
)

Sets explicit dimensions for this box.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public String

Returns:

the rendered box as a string
get
()

Implements io.github.kusoroadeolu.clique.components.Component.get.

Renders and returns the box as a string.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

private void
private void
pack-priv WidthAwareList
pack-priv void
pack-priv TextAlign
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

pack-priv void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

alignback to summary
private TextAlign align
borderCharsback to summary
private final BorderChars borderChars
BOX_CONTENT_NOT_NULLback to summary
private static final String BOX_CONTENT_NOT_NULL
cachedStringback to summary
private String cachedString
configurationback to summary
private final BoxConfiguration configuration
contentback to summary
private String content
heightback to summary
private int height
TEXT_ALIGN_NOT_NULLback to summary
private static final String TEXT_ALIGN_NOT_NULL
widthback to summary
private int width

Constructor Detail

Boxback to summary
public Box(BoxType type, BoxConfiguration configuration)
Boxback to summary
public Box(BoxType type)
Boxback to summary
public Box(BoxConfiguration configuration)
Boxback to summary
public Box()

Method Detail

contentback to summary
public Box content(String content)

Sets the content of this box and invalidates the render cache.

Text alignment is inherited from the BoxConfiguration provided at construction time.

Parameters
content:String

the text content to display; must not be null

Returns:Box

this box instance

Exceptions
NullPointerException:
if content is null
contentback to summary
public Box content(String content, TextAlign align)

Sets the content and text alignment of this box, invalidating the render cache.

The provided align takes precedence over the alignment specified in the BoxConfiguration.

Parameters
content:String

the text content to display; must not be null

align:TextAlign

the text alignment to apply; must not be null

Returns:Box

this box instance

Exceptions
NullPointerException:
if content or align is null
dimensionsback to summary
public Box dimensions(int width, int height)

Sets explicit dimensions for this box.

When dimensions are set, content that exceeds the usable inner width (width minus padding) or the specified height will throw an InvalidDimensionException at render time. If this method is never called, the box auto sizes to fit its content.

Parameters
width:int

the total outer width of the box; must be greater than 0

height:int

the total outer height of the box; must be greater than 0

Returns:Box

this box instance

Exceptions
IllegalArgumentException:
if width or height is not greater than 0
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getback to summary
public String get()

Implements io.github.kusoroadeolu.clique.components.Component.get.

Renders and returns the box as a string.

The result is cached after the first invocation and reused on subsequent calls until the content is updated. Text alignment is resolved from the per-call override set via content(String, TextAlign) if present, falling back to the value in BoxConfiguration.

Returns:String

the rendered box as a string

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
nullCachedStringback to summary
private void nullCachedString()
resolveDimensionsback to summary
private void resolveDimensions(WidthAwareList cells)
resolveLinesback to summary
pack-priv WidthAwareList resolveLines()
styleBordersback to summary
pack-priv void styleBorders()
textAlignback to summary
pack-priv TextAlign textAlign()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
validateTypeAndConfigback to summary
pack-priv void validateTypeAndConfig(BoxType type, BoxConfiguration config)