LayoutConf.java

  1. package swingtree.style;

  2. import com.google.errorprone.annotations.Immutable;
  3. import org.jspecify.annotations.Nullable;
  4. import swingtree.api.Layout;

  5. import java.util.Objects;
  6. import java.util.Optional;

  7. /**
  8.  *  A style that defines the layout of a component.
  9.  *  The layout manager of a component will use this information
  10.  *  to determine the actual layout of the component in the layout.
  11.  **/
  12. @Immutable
  13. @SuppressWarnings("Immutable")
  14. final class LayoutConf
  15. {
  16.     private static final LayoutConf _NONE = new LayoutConf(Layout.unspecific(), null, null, null);

  17.     static LayoutConf none() { return _NONE; }


  18.     private final Layout          _layout;

  19.     private final @Nullable Object _constraint;
  20.     private final @Nullable Float  _alignmentX;
  21.     private final @Nullable Float  _alignmentY;


  22.     private LayoutConf(
  23.         Layout           layout,
  24.         @Nullable Object constraint,
  25.         @Nullable Float  alignmentX,
  26.         @Nullable Float  alignmentY
  27.     ) {
  28.         _layout     = Objects.requireNonNull(layout);
  29.         _constraint = constraint;
  30.         _alignmentX = alignmentX;
  31.         _alignmentY = alignmentY;
  32.     }

  33.     Layout layout() { return _layout; }

  34.     Optional<Object> constraint() { return Optional.ofNullable(_constraint); }

  35.     Optional<Float> alignmentX() { return Optional.ofNullable(_alignmentX); }

  36.     Optional<Float> alignmentY() { return Optional.ofNullable(_alignmentY); }

  37.     LayoutConf layout(Layout installer ) { return new LayoutConf(installer, _constraint, _alignmentX, _alignmentY); }

  38.     LayoutConf constraint(Object constraint ) { return new LayoutConf(_layout, constraint, _alignmentX, _alignmentY); }

  39.     LayoutConf alignmentX(Float alignmentX ) { return new LayoutConf(_layout, _constraint, alignmentX, _alignmentY); }

  40.     LayoutConf alignmentY(Float alignmentY ) { return new LayoutConf(_layout, _constraint, _alignmentX, alignmentY); }

  41.     @Override
  42.     public int hashCode() { return Objects.hash(_layout, _constraint, _alignmentX, _alignmentY); }

  43.     @Override
  44.     public boolean equals( Object obj ) {
  45.         if ( obj == null ) return false;
  46.         if ( obj == this ) return true;
  47.         if ( obj.getClass() != getClass() ) return false;
  48.         LayoutConf other = (LayoutConf) obj;
  49.         return Objects.equals(_layout,            other._layout)         &&
  50.                Objects.equals(_constraint,        other._constraint)     &&
  51.                Objects.equals(_alignmentX,        other._alignmentX)     &&
  52.                Objects.equals(_alignmentY,        other._alignmentY);
  53.     }

  54.     @Override
  55.     public String toString() {
  56.         if ( this.equals(_NONE) )
  57.             return this.getClass().getSimpleName() + "[NONE]";
  58.         return this.getClass().getSimpleName() + "[" +
  59.                     "layout="     + _layout                                   + ", " +
  60.                     "constraint=" + (_constraint == null ? "?" : _constraint) + ", " +
  61.                     "alignmentX=" + (_alignmentX == null ? "?" : _alignmentX) + ", " +
  62.                     "alignmentY=" + (_alignmentY == null ? "?" : _alignmentY) +
  63.                "]";
  64.     }

  65. }