view artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultCallMeta.java @ 57:cf9848c85755 0.2

RELEASE 0.2: Summarized NEWS and Changes artifacts/trunk@338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Hans Plum <hans.plum@intevation.de>
date Fri, 13 Nov 2009 18:37:51 +0000
parents 3a668da76463
children 48d1a9a082c2
line wrap: on
line source
package de.intevation.artifactdatabase;

import java.util.Locale;

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

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public class DefaultCallMeta
implements   CallMeta
{
    protected PreferredLocale [] languages;

    public DefaultCallMeta() {
    }

    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