ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@48: package de.intevation.artifactdatabase; sascha@48: sascha@48: import de.intevation.artifacts.CallMeta; sascha@48: import de.intevation.artifacts.PreferredLocale; sascha@48: sascha@93: import java.util.Locale; sascha@93: sascha@48: /** sascha@87: * Default implementation of CallMeta. It provides a list of sascha@87: * preferred langauages and implements an intersection mechanism sascha@87: * to figure out the best matching language given a list of server sascha@87: * provided languages. sascha@77: * @author Sascha L. Teichmann sascha@48: */ sascha@48: public class DefaultCallMeta sascha@48: implements CallMeta sascha@48: { sascha@87: /** sascha@87: * The list of preferred languages. sascha@87: */ sascha@48: protected PreferredLocale [] languages; sascha@48: sascha@87: /** sascha@87: * Default constructor. sascha@87: */ sascha@48: public DefaultCallMeta() { sascha@48: } sascha@48: sascha@87: /** sascha@87: * Creates new DefaultCallMeta with a given list of languages. sascha@87: * @param languages The list of preferred languages. sascha@87: */ sascha@48: public DefaultCallMeta(PreferredLocale [] languages) { sascha@51: this.languages = languages; sascha@48: } sascha@48: sascha@48: public PreferredLocale [] getLanguages() { sascha@48: return languages; sascha@48: } sascha@49: sascha@49: public Locale getPreferredLocale(Locale [] locales) { sascha@49: if (locales == null || locales.length == 0) { sascha@49: return null; sascha@49: } sascha@49: sascha@50: Locale best = null; sascha@50: float quality = -Float.MAX_VALUE; sascha@50: sascha@50: for (int i = 0; i < locales.length; ++i) { sascha@50: Locale wish = locales[i]; sascha@50: String wishLanguage = wish.getLanguage(); sascha@50: sascha@50: for (int j = 0; j < languages.length; ++j) { sascha@50: PreferredLocale have = languages[j]; sascha@50: Locale haveLocale = have.getLocale(); sascha@50: if (haveLocale.getLanguage().equals(wishLanguage)) { sascha@50: float haveQuality = have.getQuality(); sascha@50: if (haveQuality > quality) { sascha@50: quality = haveQuality; sascha@50: best = wish; sascha@50: } sascha@50: break; // Languages should not contain sascha@50: // same locale twice. sascha@50: } sascha@50: } sascha@50: } sascha@50: sascha@50: return best == null sascha@50: ? locales[0] sascha@50: : best; sascha@49: } sascha@48: } sascha@91: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :