Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.css;
027
028import javafx.scene.text.Font;
029
030/**
031 * A representation of a parsed CSS value. {@code V} is the type of the parsed
032 * value, {@code T} is the {@code StyleableProperty} type of the converted value.
033 * Instances of {@code ParsedValue} are created by the CSS parser. For example,
034 * the parser creates a {@code ParsedValue<String,Color>} when it parses a 
035 * web Color. 
036 * <p>
037 * A ParsedValue is meaningful to the code that calculates actual values from
038 * parsed CSS values. Elsewhere the value returned by 
039 * {@link #getValue()} is likely to be obscure, abstruse and perplexing. 
040 */
041public class ParsedValue<V, T> {
042
043    /**
044     * The CSS property value as created by the parser.
045     */
046    final protected V value;
047    
048    /**
049     * @return The CSS property value as created by the parser, which may be null
050     * or otherwise incomprehensible.
051     */
052    public final V getValue() { return value; }
053
054    /**
055     * The {@code StyleConverter} which converts the parsed value to 
056     * the type of the {@link StyleableProperty}. This may be null, in which
057     * case {@link #convert(javafx.scene.text.Font) convert}
058     * will return {@link #getValue() getValue()}
059     */
060    final protected StyleConverter<V, T> converter;
061    
062    /**
063     * A {@code StyleConverter} converts the parsed value to 
064     * the type of the {@link StyleableProperty}. If the {@code StyleConverter}
065     * is null, {@link #convert(javafx.scene.text.Font)}
066     * will return {@link #getValue()}
067     * @return The {@code StyleConverter} which converts the parsed value to 
068     * the type of the {@link StyleableProperty}. May return null.
069     */
070    public final StyleConverter<V, T> getConverter() { return converter; }
071
072    /**
073     * Convenience method for calling 
074     * {@link StyleConverter#convert(javafx.css.ParsedValue, javafx.scene.text.Font) convert}
075     * on this {@code ParsedValue}.
076     * @param font         The {@link Font} to use when converting a
077     * <a href="http://www.w3.org/TR/css3-values/#relative-lengths">relative</a>
078     * value.
079     * @return The value converted to the type of the {@link StyleableProperty}
080     * @see #getConverter() 
081     */
082    @SuppressWarnings("unchecked")
083    public T convert(Font font) {
084        // unchecked!
085        return (T)((converter != null) ? converter.convert(this, font) : value);
086    }
087    
088    /**
089     * Create an instance of ParsedValue where the value type V is converted to
090     * the target type T using the given converter.
091     * If {@code converter} is null, then it is assumed that the type of value 
092     * {@code V} and the type of target {@code T} are the same and
093     * do not need converted.
094     */
095    protected ParsedValue(V value, StyleConverter<V, T> converter) {
096        this.value = value;
097        this.converter = converter;
098    }
099
100}