com.googlecode.blaisemath.style.AttributeSets Maven / Gradle / Ivy
/**
* AttributeSets.java
* Created Summer 2014
*/
package com.googlecode.blaisemath.style;
/*
* #%L
* BlaiseGraphics
* --
* Copyright (C) 2014 - 2017 Elisha Peterson
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.google.common.annotations.Beta;
import com.google.common.base.Converter;
import com.google.common.base.Joiner;
import com.google.common.base.Joiner.MapJoiner;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Splitter;
import com.google.common.base.Splitter.MapSplitter;
import com.google.common.collect.Maps;
import com.googlecode.blaisemath.util.Colors;
import com.googlecode.blaisemath.util.xml.FontAdapter;
import com.googlecode.blaisemath.util.xml.Point2DAdapter;
import com.googlecode.blaisemath.util.xml.PointAdapter;
import com.googlecode.blaisemath.util.xml.RectAdapter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
*
* Utility library for {@link AttributeSet}.
*
*
* Provides a way to convert {@link AttributeSet} to/from a string. The string is intended to be
* compatible with html/css, but some features of the {@code AttributeSet} cannot
* be encoded this way, so the operation is not invertible. Conversion to a
* string uses the following rules:
*
*
* - The attribute name is not used for conversion.
* - Only values of type Number, String, Color, Marker, and Anchor are supported.
* - Number, String, and Anchor values are converted in the obvious way.
* - Colors are converted to #RRGGBB or #AARRGGBB notation, using {@link Colors#stringConverter()}.
* - Marker values are persisted using their class name.
* - Null values are converted to the string "none".
*
*
* Conversion from a string uses the following rules:
*
*
* - The attribute name is not used for conversion.
* - If the value matches #RGB, #RRGGBB, or #AARRGGBB it is converted to a color.
* - A string value "none" is converted to a null value.
* - If a value can be parsed as an integer or double, it is converted to that type.
* - Otherwise, values are left as strings.
*
*
* Note that values of type Marker and Anchor are deserialized as strings rather
* than their previous type. Blaise supports having string values for these attributes
* wherever they are used.
*
*
* @author Elisha
*/
public final class AttributeSets {
/** String used to represent null explicitly. */
private static final String NULL_STRING = "none";
private static final AttributeSetConverter CONVERTER_INST = new AttributeSetConverter();
private static final AttributeValueConverter VALUE_CONVERTER_INST = new AttributeValueConverter();
private static final MapSplitter KEYVAL_SPLITTER = Splitter.on(";")
.omitEmptyStrings().trimResults()
.withKeyValueSeparator(Splitter.on(":").trimResults());
private static final MapJoiner KEYVAL_JOINER = Joiner.on("; ")
.withKeyValueSeparator(":");
// non-instantiable utility class
private AttributeSets() {
}
/**
* Return object that can be used to convert an {@link AttributeSet} to/from a string.
* The resulting style uses ";" to separate entries, and ":" to separate a key and value.
* @return converter instance
*/
@Beta
public static Converter stringConverter() {
return CONVERTER_INST;
}
/**
* Return object that can be used to convert an {@link AttributeSet} to/from a string.
* The resulting style uses ";" to separate entries, and ":" to separate a key and value.
* @param types explicit desired types
* @return converter instance
*/
@Beta
public static Converter stringConverter(Map> types) {
return new AttributeSetConverter(checkNotNull(types));
}
/**
* Return object that can be used to convert string values to/from Java object values.
* Supports numeric and color types.
* @return converter instance
*/
@Beta
public static Converter © 2015 - 2025 Weber Informatics LLC | Privacy Policy