NamedConf.java

  1. package swingtree.style;

  2. import java.util.Objects;

  3. /**
  4.  *  An immutable value container that pairs a name with a
  5.  *  style where the name is a string that is supposed to uniquely identify
  6.  *  the style in a collection of named styles.
  7.  *
  8.  * @param <S> The type of the style.
  9.  */
  10. final class NamedConf<S> implements Simplifiable<NamedConf<S>>
  11. {
  12.     static <S> NamedConf<S> of(String name, S style ) {
  13.         return new NamedConf<>( name, style );
  14.     }

  15.     private final String _name;
  16.     private final S      _style;


  17.     private NamedConf(String name, S style ) {
  18.         _name = Objects.requireNonNull(name);
  19.         _style = Objects.requireNonNull(style);
  20.     }

  21.     String name() { return _name; }

  22.     S style() { return _style; }


  23.     @Override
  24.     public int hashCode() { return Objects.hash(_name, _style); }

  25.     @Override
  26.     public boolean equals( Object obj ) {
  27.         if ( obj == null ) return false;
  28.         if ( obj == this ) return true;
  29.         if ( obj.getClass() != getClass() ) return false;
  30.         NamedConf<?> rhs = (NamedConf<?>) obj;
  31.         return Objects.equals(_name, rhs._name) &&
  32.                Objects.equals(_style, rhs._style);
  33.     }

  34.     @Override
  35.     public String toString() {
  36.         return this.getClass().getSimpleName()+"[" +
  37.                     "name="  + _name  +", "+
  38.                     "style=" + _style +
  39.                 "]";
  40.     }

  41.     @Override
  42.     public NamedConf<S> simplified() {
  43.         if ( _style instanceof Simplifiable ) {
  44.             S simplifiedStyle = ((Simplifiable<S>)_style).simplified();
  45.             if (simplifiedStyle == _style)
  46.                 return this;
  47.             return new NamedConf<>(_name, simplifiedStyle);
  48.         }
  49.         return this;
  50.     }
  51. }