ComponentConf.java

  1. package swingtree.style;

  2. import com.google.errorprone.annotations.Immutable;
  3. import swingtree.UI;
  4. import swingtree.layout.Bounds;
  5. import swingtree.layout.Size;

  6. import java.util.Objects;

  7. /**
  8.  *  An immutable snapshot of all the essential component state needed for rendering
  9.  *  and configuring the component state.
  10.  */
  11. @Immutable
  12. @SuppressWarnings("Immutable")
  13. final class ComponentConf
  14. {
  15.     public static ComponentConf none() {
  16.         return new ComponentConf(
  17.                     StyleConf.none(),
  18.                     Bounds.none(),
  19.                     Outline.none()
  20.                 );
  21.     }

  22.     private final StyleConf _styleConf;
  23.     private final Bounds    _currentBounds;
  24.     private final Outline   _marginCorrection;

  25.     private boolean _wasAlreadyHashed = false;
  26.     private int     _hashCode         = 0; // cached hash code


  27.     ComponentConf(
  28.             StyleConf styleConf,
  29.             Bounds currentBounds,
  30.             Outline marginCorrection
  31.     ) {
  32.         _styleConf        = Objects.requireNonNull(styleConf);
  33.         _currentBounds    = Objects.requireNonNull(currentBounds);
  34.         _marginCorrection = Objects.requireNonNull(marginCorrection);
  35.     }

  36.     StyleConf style() { return _styleConf; }

  37.     Bounds currentBounds() { return _currentBounds; }

  38.     Outline areaMarginCorrection() { return _marginCorrection; }

  39.     ComponentConf withSize( int width, int height ) {
  40.         return new ComponentConf(
  41.                    _styleConf,
  42.                    Bounds.of(_currentBounds.location(), Size.of(width, height)),
  43.                 _marginCorrection
  44.                );
  45.     }

  46.     /**
  47.      *  Returns a new {@link ComponentConf} instance which only contains style information relevant
  48.      *  to the provided {@link UI.Layer}. Style information on other layers is discarded.
  49.      * @param layer The layer to retain.
  50.      * @return A new {@link ComponentConf} instance which only contains style information relevant to the provided {@link UI.Layer}.
  51.      */
  52.     LayerRenderConf toRenderConfFor(UI.Layer layer ) {
  53.         return LayerRenderConf.of(layer,this);
  54.     }

  55.     @Override
  56.     public String toString() {
  57.         return this.getClass().getSimpleName()+"[" +
  58.                     "style="                + _styleConf + ", "+
  59.                     "bounds="               + _currentBounds + ", "+
  60.                     "areaMarginCorrection=" + _marginCorrection + ", "+
  61.                 "]";
  62.     }

  63.     @Override
  64.     public boolean equals( Object o ) {
  65.         if ( o == this ) return true;
  66.         if ( o == null ) return false;
  67.         if ( o.getClass() != this.getClass() ) return false;
  68.         ComponentConf other = (ComponentConf) o;
  69.         return Objects.equals(_styleConf, other._styleConf)
  70.             && Objects.equals(_currentBounds, other._currentBounds)
  71.             && Objects.equals(_marginCorrection, other._marginCorrection);
  72.     }

  73.     @Override
  74.     public int hashCode() {
  75.         if ( _wasAlreadyHashed )
  76.             return _hashCode;

  77.         _hashCode = Objects.hash(_styleConf, _currentBounds, _marginCorrection);
  78.         _wasAlreadyHashed = true;
  79.         return _hashCode;
  80.     }
  81. }