package fi.vincit.jmobster.converter.valueconverters;
/*
 * Copyright 2012 Juha Siponen
 *
 * 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.
*/

/**
 * Base implementation for value converters.
 */
public abstract class BaseValueConverter implements ValueConverter {
    /**
     * Return handled type default value. Returned value should
     * be formatted as it appears in target platform. E.g. string for
     * JavaScript should contain the quotation marks around the value.
     * @return Default value as string. Null must not be returned from this method.
     */
    protected abstract String getTypeDefaultValue();

    /**
     * <p>Converts the value to string format. Default implementation uses
     * toString() method and it works well for many cases like BigDecimals.
     * </p>
     * <p>Override this method to implement custom value converter.</p>
     * @param value
     * @return Value as string. If null is returned the default value is used.
     */
    protected String getValueAsString(Object value) {
        return value.toString();
    }

    @Override
    public String convertValue(Object value) {
        if( value != null ) {
            String valueAsString = getValueAsString(value);
            return valueAsString != null ? valueAsString : getTypeDefaultValue();
        } else {
            return getTypeDefaultValue();
        }
    }
}

@@ -649,16 +649,27 @@
 %0A%0A/**%0A *
+ %3Cp%3E%0A *    
  Base im
@@ -702,16 +702,781 @@
 verters.
+ The class can be%0A * extended to create custom value converters if the default%0A * ones are not enough. The default implementation will%0A * try to convert the value with Java's toString() method%0A * and in some cases it won't give correct results.%0A * %3C/p%3E%0A *%0A * %3Cp%3E%0A *     All extended classes have to implement method%0A * %7B@link fi.vincit.jmobster.converter.valueconverters.BaseValueConverter#getTypeDefaultValue()%7D%0A * which will be used for default values as well as, in the default%0A * implementation, for null values. Additionally method%0A * %7B@link BaseValueConverter#getValueAsString(Object)%7D can be overridden%0A * to format the value as required. Note that string and similar%0A * values (like enums) may need to have quotation marks added%0A * around the value.%0A * %3C/p%3E
 %0A */%0Apub

Document BaseValueConverter usage