/*
* Copyright 2013 Lei CHEN (raistlic@gmail.com)
*
* 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.
*/
package org.raist.config;
import org.raist.common.codec.CodingException;
import java.awt.*;
/**
* This class defines a set of "commonly used" encoders and decoders, for
* encoding and decoding values of the "supported by default" types of the
* configuration library.
*
* <p/>
* It is a set of "static" utilities, served as a "singleton" object.
*
* @author Lei.C (2013-11-02)
*/
public final class ConfigCodec {
private static final ConfigCodec INSTANCE = new ConfigCodec();
/**
* This method returns the singleton instance of the class.
*
* @return the singleton instance of the class
*/
public static ConfigCodec getInstance() {
return INSTANCE;
}
/*
* The class is designed not to be instantiated or inherited.
*/
private ConfigCodec() {}
/*---------------------------------------------------------------------------
* boolean
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code boolean} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code boolean} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code boolean} value.
*/
public boolean decodeBoolean(String val) {
if (val.equalsIgnoreCase("true"))
return Boolean.TRUE;
else if (val.equalsIgnoreCase("false"))
return Boolean.FALSE;
else
throw new CodingException("Invalid boolean value expression: " + val);
}
/**
* This method encodes the specified {@code boolean} value into a {@code String}.
*
* @param val the specified {@code boolean} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeBoolean(boolean val) {
return Boolean.toString(val);
}
/*---------------------------------------------------------------------------
* char
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code char} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code char} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code char} value.
*/
public char decodeChar(String val) {
if (val.length() == 1)
return val.charAt(0);
else
throw new CodingException("Invalid char value expression: " + val);
}
/**
* This method encodes the specified {@code char} value into a {@code String}.
*
* @param val the specified {@code char} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeChar(char val) {
return String.valueOf(val);
}
/*---------------------------------------------------------------------------
* int
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code int} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code int} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code int} value.
*/
public int decodeInt(String val) {
try {
return Integer.parseInt(val);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code int} value into a {@code String}.
*
* @param val the specified {@code int} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeInt(int val) {
return Integer.toString(val);
}
/*----------------------------------------------------------------------------
* long
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code long} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code long} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code long} value.
*/
public long decodeLong(String val) {
if (val == null)
throw new NullPointerException("String 'val' is null.");
try {
return Long.parseLong(val);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code long} value into a {@code String}.
*
* @param val the specified {@code long} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeLong(long val) {
return Long.toString(val);
}
/*----------------------------------------------------------------------------
* float
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code float} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code float} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code float} value.
*/
public float decodeFloat(String val) {
if (val == null)
throw new NullPointerException("String 'val' is null.");
try {
return Float.parseFloat(val);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code float} value into a {@code String}.
*
* @param val the specified {@code float} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeFloat(float val) {
return Float.toString(val);
}
/*----------------------------------------------------------------------------
* double
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code double} value from a {@code String} value.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code double} value.
* @throws NullPointerException if the specified {@code String} value is
* {@code null}.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code double} value.
*/
public double decodeDouble(String val) {
if (val == null)
throw new NullPointerException("String 'val' is null.");
try {
return Double.parseDouble(val);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code double} value into a {@code String}.
*
* @param val the specified {@code double} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeDouble(double val) {
return Double.toString(val);
}
/*----------------------------------------------------------------------------
* java.awt.Color
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code Color} value from a {@code String} value, note
* that if the specified {@code String} representation is {@code null}, a
* {@code null Color} is returned, indicating that the value is 'not set'.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code Color} value.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code Color} value.
*/
public Color decodeColor(String val) {
// null is a valid value here, and should be decoded as a null Color, indicating
// the value is not set in the configuration.
if (val == null)
return null;
try {
int rgba = Integer.parseInt(val, 16);
return new Color(rgba);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code Color} value into a {@code String},
* note that for a {@code null Color}, a {@code null String} is returned,
* representing a 'not set' value.
*
* @param val the specified {@code Color} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeColor(Color val) {
// null is valid value here, and should be encoded as a null String,
// indicating the value is not set in the configuration.
if (val == null)
return null;
int rgb = val.getRGB();
return String.format(ConsText.FormatColor.v, rgb);
}
/*----------------------------------------------------------------------------
* java.awt.Dimension
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code Dimension} value from a {@code String} value,
* note that if the specified {@code String} representation is {@code null}, a
* {@code null Dimension} is returned, indicating that the value is 'not set'.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code Dimension} value.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code Dimension} value.
*/
public Dimension decodeDimension(String val) {
// a null String indicates the value is not set, in which case a null Dimension
// is returned as the decoded value.
if (val == null)
return null;
String[] tokens = val.split(ConsText.DilimDimension.v);
if (tokens.length != 2)
throw new CodingException("Invalid String representation for dimension.");
try {
int width = Integer.parseInt(tokens[0]);
int height = Integer.parseInt(tokens[1]);
return new Dimension(width, height);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code Dimension} value into a {@code String},
* note that for a {@code null Dimension}, a {@code null String} is returned,
* representing a 'not set' value.
*
* @param val the specified {@code Dimension} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodeDimension(Dimension val) {
return val == null ? null : val.width + ConsText.DilimDimension.v + val.height;
}
/*----------------------------------------------------------------------------
* java.awt.Point
---------------------------------------------------------------------------*/
/**
* This method decodes a {@code Point} value from a {@code String} value,
* note that if the specified {@code String} representation is {@code null}, a
* {@code null Point} is returned, indicating that the value is 'not set'.
*
* @param val the {@code String} representation of the value.
* @return the converted {@code Point} value.
* @throws CodingException if the specified {@code String} value cannot be
* converted to a {@code Point} value.
*/
public Point decodePoint(String val) {
if (val == null)
return null;
String[] tokens = val.split(ConsText.DilimPoint.v);
if (tokens.length != 2)
throw new CodingException("Invalid string representation of a Point value: " + val);
try {
int x = Integer.parseInt(tokens[0]);
int y = Integer.parseInt(tokens[1]);
return new Point(x, y);
}
catch (NumberFormatException ex) {
throw new CodingException(ex.getMessage(), ex);
}
}
/**
* This method encodes the specified {@code Point} value into a {@code String},
* note that for a {@code null Point}, a {@code null String} is returned,
* representing a 'not set' value.
*
* @param val the specified {@code Point} value to convert.
* @return the converted {@code String} representation of the specified value.
*/
public String encodePoint(Point val) {
return val == null ? null : val.getX() + ConsText.DilimPoint.v + val.getY();
}
private static enum ConsText {
FormatColor ("%08X"),
DilimDimension (","),
DilimPoint (","),
;
private final String v;
ConsText(String v) { this.v = v; }
}
}