Class ResponsiveValue<T>
java.lang.Object
com.oorian.html.layout.responsive.ResponsiveValue<T>
- Type Parameters:
T- the type of value being stored
A container for values that vary by screen size breakpoint.
ResponsiveValue allows specifying different values for different screen sizes using a fluent builder API. This is useful for configuring layout properties like column counts, gaps, padding, or any value that should adapt to screen size.
Mobile-First Approach:
Values cascade upward. A value set at SM applies to SM and all larger breakpoints
until overridden. The base value (set via of(T)) applies at XS (mobile) and up.
Usage:
// Grid columns: 1 on mobile, 2 on tablets, 4 on desktop
ResponsiveValue<Integer> columns = ResponsiveValue.of(1).sm(2).lg(4);
// Gap sizes
ResponsiveValue<String> gap = ResponsiveValue.of("8px").md("16px").xl("24px");
// Get value for specific breakpoint
int tabletColumns = columns.get(Breakpoint.MD); // Returns 2 (from sm)
int desktopColumns = columns.get(Breakpoint.XL); // Returns 4 (from lg)
// Check if a breakpoint has an explicit value
boolean hasLgValue = columns.hasValue(Breakpoint.LG); // true
// Iterate over defined values
columns.forEach((breakpoint, value) -> {
System.out.println(breakpoint + ": " + value);
});
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionat(Breakpoint breakpoint, T value) Sets the value for a specific breakpoint.static <T> ResponsiveValue<T> empty()Creates an empty ResponsiveValue.voidforEach(BiConsumer<Breakpoint, T> action) Iterates over all explicitly set breakpoint-value pairs.get(Breakpoint breakpoint) Gets the effective value for a specific breakpoint.getAll()Returns all explicitly set breakpoint-value pairs.getBase()Gets the base (XS) value.getExplicit(Breakpoint breakpoint) Gets the explicitly set value for a breakpoint (no cascading).booleanChecks if any values are set.booleanhasValue(Breakpoint breakpoint) Checks if a value is explicitly set for a breakpoint.Sets the value for LG breakpoint (1024px and up).Sets the value for MD breakpoint (768px and up).static <T> ResponsiveValue<T> of(T baseValue) Creates a ResponsiveValue with a base (mobile/XS) value.intsize()Returns the number of explicitly set values.Sets the value for SM breakpoint (640px and up).toString()Sets the value for XL breakpoint (1280px and up).Sets the value for XS breakpoint (0px and up).Sets the value for XXL breakpoint (1536px and up).
-
Method Details
-
of
Creates a ResponsiveValue with a base (mobile/XS) value.This value applies at the smallest breakpoint and cascades up to larger breakpoints until overridden.
- Type Parameters:
T- the type of value- Parameters:
baseValue- the value for XS (mobile) and up- Returns:
- a new ResponsiveValue with the base value set
-
empty
Creates an empty ResponsiveValue.- Type Parameters:
T- the type of value- Returns:
- a new empty ResponsiveValue
-
xs
Sets the value for XS breakpoint (0px and up).- Parameters:
value- the value for extra small screens- Returns:
- this ResponsiveValue for method chaining
-
sm
Sets the value for SM breakpoint (640px and up).- Parameters:
value- the value for small screens- Returns:
- this ResponsiveValue for method chaining
-
md
Sets the value for MD breakpoint (768px and up).- Parameters:
value- the value for medium screens- Returns:
- this ResponsiveValue for method chaining
-
lg
Sets the value for LG breakpoint (1024px and up).- Parameters:
value- the value for large screens- Returns:
- this ResponsiveValue for method chaining
-
xl
Sets the value for XL breakpoint (1280px and up).- Parameters:
value- the value for extra large screens- Returns:
- this ResponsiveValue for method chaining
-
xxl
Sets the value for XXL breakpoint (1536px and up).- Parameters:
value- the value for 2x extra large screens- Returns:
- this ResponsiveValue for method chaining
-
at
Sets the value for a specific breakpoint.- Parameters:
breakpoint- the breakpointvalue- the value for that breakpoint- Returns:
- this ResponsiveValue for method chaining
-
get
Gets the effective value for a specific breakpoint.If no value is explicitly set for the breakpoint, returns the value from the next smaller breakpoint that has a value (cascading behavior).
- Parameters:
breakpoint- the breakpoint to get the value for- Returns:
- the effective value, or null if no value applies
-
getExplicit
Gets the explicitly set value for a breakpoint (no cascading).- Parameters:
breakpoint- the breakpoint- Returns:
- the explicit value, or null if not set
-
hasValue
Checks if a value is explicitly set for a breakpoint.- Parameters:
breakpoint- the breakpoint to check- Returns:
- true if a value is explicitly set
-
getBase
Gets the base (XS) value.- Returns:
- the base value, or null if not set
-
getAll
Returns all explicitly set breakpoint-value pairs.- Returns:
- an unmodifiable map of breakpoints to values
-
forEach
Iterates over all explicitly set breakpoint-value pairs.- Parameters:
action- the action to perform for each pair
-
size
public int size()Returns the number of explicitly set values.- Returns:
- the count of set values
-
hasAnyValue
public boolean hasAnyValue()Checks if any values are set.- Returns:
- true if at least one value is set
-
toString
-