All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.types.converters.LocaleStringConverter Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.7
Show newest version
/**********************************************************************
Copyright (c) 2008 Andy Jefferson and others. All rights reserved.
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.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.store.types.converters;

import java.util.Locale;

import org.datanucleus.util.I18nUtils;

/**
 * Class to handle the conversion between java.util.Locale and a String form.
 * Locale should be stored in colums from 2 to 20 characters. Normaly, we will have a string no longer than
 * 5 characters, but variants, in general, are vendor specific and can be longer than expected. 
 * The Variant codes are vendor and browser-specific. For example, use WIN for Windows, MAC for Macintosh,
 * and POSIX for POSIX. Where there are two variants, separate them with an underscore, and put the most
 * important one first. For example, a Traditional Spanish collation might construct a locale with
 * parameters for language, country and variant as: "es", "ES", "Traditional_WIN".  
 * language_country_variant
 * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC"
 * @see java.util.Locale
 */
public class LocaleStringConverter implements TypeConverter, ColumnLengthDefiningTypeConverter
{
    private static final long serialVersionUID = -5566584819761013454L;

    public Locale toMemberType(String str)
    {
        if (str == null)
        {
            return null;
        }

        return I18nUtils.getLocaleFromString(str);
    }

    public String toDatastoreType(Locale loc)
    {
        return loc != null ? loc.toString() : null;
    }

    public int getDefaultColumnLength(int columnPosition)
    {
        if (columnPosition != 0)
        {
            return -1;
        }
        // Locales require 20 characters.
        return 20;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy