1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.css;
  27 
  28 import com.sun.javafx.css.PseudoClassState;
  29 
  30 import static javafx.geometry.NodeOrientation.INHERIT;
  31 
  32 
  33 /**
  34  * Used by {@link Rule} to determine whether or not the selector applies to a
  35  * given object.
  36  *
  37  * Returned by {@link Selector} matches in the event of a match.
  38  *
  39  * @since 9
  40  */
  41 public final class Match implements Comparable<Match> {
  42 
  43     final Selector selector;
  44     final PseudoClassState pseudoClasses;
  45     final int idCount;
  46     final int styleClassCount;
  47 
  48     // CSS3 spec gives weight to id count, then style class count,
  49     // then pseudoclass count, and finally matching types (i.e., java name count)
  50     final int specificity;
  51 
  52     Match(final Selector selector, PseudoClassState pseudoClasses, int idCount, int styleClassCount) {
  53         assert selector != null;
  54         this.selector = selector;
  55         this.idCount = idCount;
  56         this.styleClassCount = styleClassCount;
  57         this.pseudoClasses = pseudoClasses;
  58         int nPseudoClasses = pseudoClasses != null ? pseudoClasses.size() : 0;
  59         if (selector instanceof SimpleSelector) {
  60             final SimpleSelector simple = (SimpleSelector)selector;
  61             if (simple.getNodeOrientation() != INHERIT) {
  62                 nPseudoClasses += 1;
  63             }
  64         }
  65         specificity = (idCount << 8) | (styleClassCount << 4) | nPseudoClasses;
  66     }
  67 
  68     public Selector getSelector() {
  69         return selector;
  70     }
  71 
  72     public PseudoClassState getPseudoClasses() {
  73         return pseudoClasses;
  74     }
  75 
  76     public int getSpecificity() {
  77         return specificity;
  78     }
  79 
  80     @Override public int compareTo(Match o) {
  81         return specificity - o.specificity;
  82     }
  83 
  84 }