Record Class CssSpecificity

java.lang.Object
java.lang.Record
dev.jcputney.mjml.css.CssSpecificity
Record Components:
a - the number of ID selectors
b - the number of class, attribute, and pseudo-class selectors
c - the number of type and pseudo-element selectors
All Implemented Interfaces:
Comparable<CssSpecificity>

public record CssSpecificity(int a, int b, int c) extends Record implements Comparable<CssSpecificity>
CSS specificity as an (a, b, c) tuple where:
  • a = number of ID selectors
  • b = number of class, attribute, and pseudo-class selectors
  • c = number of type and pseudo-element selectors
Inline styles have a virtual specificity of (1, 0, 0, 0) which always wins, handled separately by the inliner.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final CssSpecificity
    Represents CSS specificity of zero, defined as (0, 0, 0).
  • Constructor Summary

    Constructors
    Constructor
    Description
    CssSpecificity(int a, int b, int c)
    Creates an instance of a CssSpecificity record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    a()
    Returns the value of the a record component.
    Adds two specificity values together (used for compound selectors).
    int
    b()
    Returns the value of the b record component.
    int
    c()
    Returns the value of the c record component.
    int
    Compares this CSS specificity with another based on the specificity values (a, b, c).
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns a string representation of the CSS specificity as a tuple (a, b, c).

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ZERO

      public static final CssSpecificity ZERO
      Represents CSS specificity of zero, defined as (0, 0, 0). This is typically used to indicate the absence of any selector specificity.
  • Constructor Details

    • CssSpecificity

      public CssSpecificity(int a, int b, int c)
      Creates an instance of a CssSpecificity record class.
      Parameters:
      a - the value for the a record component
      b - the value for the b record component
      c - the value for the c record component
  • Method Details

    • add

      public CssSpecificity add(CssSpecificity other)
      Adds two specificity values together (used for compound selectors).
      Parameters:
      other - the specificity to add to this one
      Returns:
      new specificity with each component summed
    • compareTo

      public int compareTo(CssSpecificity other)
      Compares this CSS specificity with another based on the specificity values (a, b, c). The comparison is performed in order of significance: first by `a` (ID selectors), then by `b` (class, attribute, and pseudo-class selectors), and finally by `c` (type and pseudo-element selectors).
      Specified by:
      compareTo in interface Comparable<CssSpecificity>
      Parameters:
      other - the other CssSpecificity object to compare with this instance
      Returns:
      a negative integer, zero, or a positive integer as this specificity is less than, equal to, or greater than the specified specificity
    • toString

      public String toString()
      Returns a string representation of the CSS specificity as a tuple (a, b, c).
      Specified by:
      toString in class Record
      Returns:
      a string in the format "(a,b,c)" where: `a` represents the number of ID selectors, `b` represents the number of class, attribute, and pseudo-class selectors, and `c` represents the number of type and pseudo-element selectors.
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • a

      public int a()
      Returns the value of the a record component.
      Returns:
      the value of the a record component
    • b

      public int b()
      Returns the value of the b record component.
      Returns:
      the value of the b record component
    • c

      public int c()
      Returns the value of the c record component.
      Returns:
      the value of the c record component