/* * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package com.sun.javafx.css; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A map of property name to the cascading styles that match a node. */ public final class StyleMap { public static final StyleMap EMPTY_MAP = new StyleMap(-1, Collections.emptyList()); /** Only StyleManager creates StyleMap */ StyleMap(int id, List selectors) { this.id = id; this.selectors = selectors; } public int getId() { return id; } public boolean isEmpty() { if (selectors != null) return selectors.isEmpty(); else if (cascadingStyles != null) return cascadingStyles.isEmpty(); else return true; } public Map> getCascadingStyles() { if (cascadingStyles == null) { if (selectors == null || selectors.isEmpty()) { cascadingStyles = Collections.emptyMap(); return cascadingStyles; } // // Creating the map is a three step process. First, create // a list of CascadingStyles. Second, sort the CascadingStyles. // And, finally, loop through the CascadingStyles to put them // into the Map by property name. // List cascadingStyleList = new ArrayList<>(); int ordinal = 0; for (int i=0, iMax=selectors.size(); i(nCascadingStyles); CascadingStyle cascadingStyle = cascadingStyleList.get(0); String property = cascadingStyle.getProperty(); for (int fromIndex=0; fromIndex value = cascadingStyles.get(property); if (value == null) { int toIndex = fromIndex; final String currentProperty = property; while (++toIndex < nCascadingStyles) { cascadingStyle = cascadingStyleList.get(toIndex); property = cascadingStyle.getProperty(); if (property.equals(currentProperty) == false) break; } cascadingStyles.put(currentProperty, cascadingStyleList.subList(fromIndex, toIndex)); fromIndex = toIndex; } else { // logic is broken! assert(false); } } selectors.clear(); selectors = null; } return cascadingStyles; } private static final Comparator cascadingStyleComparator = (o1, o2) -> { // // primary sort is on property. If the property names are the same, // then go through the cascade logic. Otherwise, sort by property // name since the cascade doesn't matter for dissimilar properties. // // What we want to end up with is a list where all the same properties // are together in the list. // String thisProperty = o1.getProperty(); String otherProperty = o2.getProperty(); int c = thisProperty.compareTo(otherProperty); if (c != 0) return c; return o1.compareTo(o2); }; private final int id; // unique per container private List selectors; private Map> cascadingStyles; }