Skip to content

Commit

Permalink
Make match fields private by moving match creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
John authored and John committed Aug 7, 2024
1 parent 70c471d commit efe1bee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
40 changes: 37 additions & 3 deletions modules/javafx.graphics/src/main/java/javafx/css/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@

import static javafx.geometry.NodeOrientation.INHERIT;

import com.sun.javafx.css.CompoundSelector;
import com.sun.javafx.css.ImmutablePseudoClassSetsCache;
import com.sun.javafx.css.PseudoClassState;
import com.sun.javafx.css.SimpleSelector;

import java.util.List;
import java.util.Objects;
import java.util.Set;

Expand All @@ -43,15 +46,46 @@
*/
public final class Match implements Comparable<Match> {

static Match of(Selector selector) {
return switch(selector) {
case SimpleSelector s -> ofSimpleSelector(s);
case CompoundSelector cs -> ofCompoundSelector(cs);
};
}

private static Match ofCompoundSelector(CompoundSelector selector) {
PseudoClassState pseudoClasses = new PseudoClassState();
int idCount = 0;
int styleClassCount = 0;
List<SimpleSelector> containedSelectors = selector.getSelectors();

for (int n = 0, max = containedSelectors.size(); n < max; n++) {
Match match = ofSimpleSelector(containedSelectors.get(n));

pseudoClasses.addAll(match.getPseudoClasses());
idCount += match.idCount;
styleClassCount += match.styleClassCount;
}

return new Match(selector, pseudoClasses, idCount, styleClassCount);
}

private static Match ofSimpleSelector(SimpleSelector selector) {
int idCount = selector.getId().isEmpty() ? 0 : 1;
int styleClassCount = selector.getStyleClassSet().size();

return new Match(selector, selector.getPseudoClassStates(), idCount, styleClassCount);
}

private final Selector selector;
private final Set<PseudoClass> pseudoClasses;

final int styleClassCount;
final int idCount;
private final int styleClassCount;
private final int idCount;

// CSS3 spec gives weight to id count, then style class count,
// then pseudoclass count, and finally matching types (i.e., java name count)
final int specificity;
private final int specificity;

@SuppressWarnings("removal")
Match(final Selector selector, Set<PseudoClass> pseudoClasses, int idCount, int styleClassCount) {
Expand Down
30 changes: 1 addition & 29 deletions modules/javafx.graphics/src/main/java/javafx/css/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@

import com.sun.javafx.css.Combinator;
import com.sun.javafx.css.CompoundSelector;
import com.sun.javafx.css.PseudoClassState;
import com.sun.javafx.css.SimpleSelector;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -111,31 +107,7 @@ public int getOrdinal() {
* @return a match, never {@code null}
*/
public final Match createMatch() {
if (this instanceof SimpleSelector s) {
int idCount = s.getId().isEmpty() ? 0 : 1;
int styleClassCount = s.getStyleClassSet().size();

return new Match(this, s.getPseudoClassStates(), idCount, styleClassCount);
}

if (this instanceof CompoundSelector cs) {
PseudoClassState pseudoClasses = new PseudoClassState();
int idCount = 0;
int styleClassCount = 0;

for (int n = 0, max = cs.getSelectors().size(); n < max; n++) {
Selector selector = cs.getSelectors().get(n);
Match match = selector.createMatch();

pseudoClasses.addAll(match.getPseudoClasses());
idCount += match.idCount;
styleClassCount += match.styleClassCount;
}

return new Match(this, pseudoClasses, idCount, styleClassCount);
}

throw new AssertionError("unreachable, class is sealed");
return Match.of(this);
}

/**
Expand Down

0 comments on commit efe1bee

Please sign in to comment.