Class ResponsiveValue<T>

java.lang.Object
com.oorian.html.layout.responsive.ResponsiveValue<T>
Type Parameters:
T - the type of value being stored

public class ResponsiveValue<T> extends Object
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 Details

    • of

      public static <T> ResponsiveValue<T> of(T baseValue)
      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

      public static <T> ResponsiveValue<T> empty()
      Creates an empty ResponsiveValue.
      Type Parameters:
      T - the type of value
      Returns:
      a new empty ResponsiveValue
    • xs

      public ResponsiveValue<T> xs(T value)
      Sets the value for XS breakpoint (0px and up).
      Parameters:
      value - the value for extra small screens
      Returns:
      this ResponsiveValue for method chaining
    • sm

      public ResponsiveValue<T> sm(T value)
      Sets the value for SM breakpoint (640px and up).
      Parameters:
      value - the value for small screens
      Returns:
      this ResponsiveValue for method chaining
    • md

      public ResponsiveValue<T> md(T value)
      Sets the value for MD breakpoint (768px and up).
      Parameters:
      value - the value for medium screens
      Returns:
      this ResponsiveValue for method chaining
    • lg

      public ResponsiveValue<T> lg(T value)
      Sets the value for LG breakpoint (1024px and up).
      Parameters:
      value - the value for large screens
      Returns:
      this ResponsiveValue for method chaining
    • xl

      public ResponsiveValue<T> xl(T value)
      Sets the value for XL breakpoint (1280px and up).
      Parameters:
      value - the value for extra large screens
      Returns:
      this ResponsiveValue for method chaining
    • xxl

      public ResponsiveValue<T> xxl(T value)
      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

      public ResponsiveValue<T> at(Breakpoint breakpoint, T value)
      Sets the value for a specific breakpoint.
      Parameters:
      breakpoint - the breakpoint
      value - the value for that breakpoint
      Returns:
      this ResponsiveValue for method chaining
    • get

      public T get(Breakpoint breakpoint)
      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

      public T getExplicit(Breakpoint breakpoint)
      Gets the explicitly set value for a breakpoint (no cascading).
      Parameters:
      breakpoint - the breakpoint
      Returns:
      the explicit value, or null if not set
    • hasValue

      public boolean hasValue(Breakpoint breakpoint)
      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

      public T getBase()
      Gets the base (XS) value.
      Returns:
      the base value, or null if not set
    • getAll

      public Map<Breakpoint,T> getAll()
      Returns all explicitly set breakpoint-value pairs.
      Returns:
      an unmodifiable map of breakpoints to values
    • forEach

      public void forEach(BiConsumer<Breakpoint,T> action)
      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

      public String toString()
      Overrides:
      toString in class Object