view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultCallMeta.java @ 92:73d0ebae81d7

Last bunch of javadoc artifacts/trunk@848 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Mar 2010 14:16:05 +0000
parents 730ff077a58c
children e27cf9c84eb8
line wrap: on
line source
package de.intevation.artifactdatabase;

import java.util.Locale;

import de.intevation.artifacts.CallMeta;
import de.intevation.artifacts.PreferredLocale;

/**
 * Default implementation of CallMeta. It provides a list of
 * preferred langauages and implements an intersection mechanism
 * to figure out the best matching language given a list of server
 * provided languages.
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class DefaultCallMeta
implements   CallMeta
{
    /**
     * The list of preferred languages.
     */
    protected PreferredLocale [] languages;

    /**
     * Default constructor.
     */
    public DefaultCallMeta() {
    }

    /**
     * Creates new DefaultCallMeta with a given list of languages.
     * @param languages The list of preferred languages.
     */
    public DefaultCallMeta(PreferredLocale [] languages) {
        this.languages = languages;
    }

    public PreferredLocale [] getLanguages() {
        return languages;
    }

    public Locale getPreferredLocale(Locale [] locales) {
        if (locales == null || locales.length == 0) {
            return null;
        }

        Locale best    = null;
        float  quality = -Float.MAX_VALUE;

        for (int i = 0; i < locales.length; ++i) {
            Locale wish         = locales[i];
            String wishLanguage = wish.getLanguage();

            for (int j = 0; j < languages.length; ++j) {
                PreferredLocale have        = languages[j];
                Locale          haveLocale  = have.getLocale();
                if (haveLocale.getLanguage().equals(wishLanguage)) {
                    float haveQuality = have.getQuality();
                    if (haveQuality > quality) {
                        quality = haveQuality;
                        best    = wish;
                    }
                    break; // Languages should not contain
                           // same locale twice.
                }
            }
        }

        return best == null
            ? locales[0]
            : best;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org