1 /*
   2  * Copyright (c) 2010, 2014, 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 javafx.scene.text.Font;
  29 
  30 /**
  31  * A representation of a parsed CSS value. {@code V} is the type of the parsed
  32  * value, {@code T} is the {@code StyleableProperty} type of the converted value.
  33  * Instances of {@code ParsedValue} are created by the CSS parser. For example,
  34  * the parser creates a {@code ParsedValue<String,Color>} when it parses a
  35  * web Color. 
  36  * <p>
  37  * A ParsedValue is meaningful to the code that calculates actual values from
  38  * parsed CSS values. Elsewhere the value returned by 
  39  * {@link #getValue()} is likely to be obscure, abstruse and perplexing. 
  40  * @since JavaFX 8.0
  41  */
  42 public class ParsedValue<V, T> {
  43 
  44     /**
  45      * The CSS property value as created by the parser.
  46      */
  47     final protected V value;
  48     
  49     /**
  50      * @return The CSS property value as created by the parser, which may be null
  51      * or otherwise incomprehensible.
  52      */
  53     public final V getValue() { return value; }
  54 
  55     /**
  56      * The {@code StyleConverter} which converts the parsed value to 
  57      * the type of the {@link StyleableProperty}. This may be null, in which
  58      * case {@link #convert(javafx.scene.text.Font) convert}
  59      * will return {@link #getValue() getValue()}
  60      */
  61     final protected StyleConverter<V, T> converter;
  62     
  63     /**
  64      * A {@code StyleConverter} converts the parsed value to 
  65      * the type of the {@link StyleableProperty}. If the {@code StyleConverter}
  66      * is null, {@link #convert(javafx.scene.text.Font)}
  67      * will return {@link #getValue()}
  68      * @return The {@code StyleConverter} which converts the parsed value to 
  69      * the type of the {@link StyleableProperty}. May return null.
  70      */
  71     public final StyleConverter<V, T> getConverter() { return converter; }
  72 
  73     /**
  74      * Convenience method for calling 
  75      * {@link StyleConverter#convert(javafx.css.ParsedValue, javafx.scene.text.Font) convert}
  76      * on this {@code ParsedValue}.
  77      * @param font         The {@link Font} to use when converting a
  78      * <a href="http://www.w3.org/TR/css3-values/#relative-lengths">relative</a>
  79      * value.
  80      * @return The value converted to the type of the {@link StyleableProperty}
  81      * @see #getConverter() 
  82      */
  83     @SuppressWarnings("unchecked")
  84     public T convert(Font font) {
  85         // unchecked!
  86         return (T)((converter != null) ? converter.convert(this, font) : value);
  87     }
  88     
  89     /**
  90      * Create an instance of ParsedValue where the value type V is converted to
  91      * the target type T using the given converter.
  92      * If {@code converter} is null, then it is assumed that the type of value 
  93      * {@code V} and the type of target {@code T} are the same and
  94      * do not need converted.
  95      */
  96     protected ParsedValue(V value, StyleConverter<V, T> converter) {
  97         this.value = value;
  98         this.converter = converter;
  99     }
 100 
 101 }