io.ultreia.java4all.i18n.spi.I18nApplicationDefinition Maven / Gradle / Ivy
package io.ultreia.java4all.i18n.spi;
/*-
* #%L
* I18n :: Spi
* %%
* Copyright (C) 2018 Code Lutin, Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Describe a i18n application definition.
*
* Created by tchemit on 31/10/2018.
*
* @author Tony Chemit - [email protected]
*/
public class I18nApplicationDefinition extends I18nCoordinate {
/**
* Encoding used to load data.
*/
private final Charset encoding;
/**
* Application version.
*/
private final String version;
/**
* Application locales.
*/
private final List locales;
/**
* Dictionary of categorized keys.
*/
private final Map keyCategories;
/**
* Main translations.
*/
private final List applicationTranslationDefinitions;
/**
* Dependencies translations.
*/
private final List dependenciesTranslationDefinitions;
/**
* Application templates.
*/
private final List applicationTemplateDefinitions;
public I18nApplicationDefinition(String packageName,
String name,
Charset encoding,
String version,
String[] locales,
Map keyCategories,
String[] moduleTranslations,
String[] dependenciesTranslations,
String[] applicationTemplates) {
super(packageName, name);
this.encoding = encoding;
this.version = version;
this.locales = Collections.unmodifiableList(Arrays.stream(locales).map(I18nLocaleHelper::newLocale).collect(Collectors.toList()));
this.keyCategories = keyCategories;
this.applicationTranslationDefinitions = new LinkedList<>();
this.dependenciesTranslationDefinitions = new LinkedList<>();
this.applicationTemplateDefinitions = new LinkedList<>();
Arrays.stream(moduleTranslations).map(I18nTranslationSetDefinition::translationSetListFromCoordinate).forEachOrdered(this.applicationTranslationDefinitions::addAll);
Arrays.stream(dependenciesTranslations).map(I18nTranslationSetDefinition::translationSetListFromCoordinate).forEachOrdered(this.dependenciesTranslationDefinitions::addAll);
Arrays.stream(applicationTemplates).map(I18nTemplateDefinition::templateListFromCoordinate).forEachOrdered(this.applicationTemplateDefinitions::addAll);
}
public I18nApplicationDefinition(String packageName,
String name,
Charset encoding,
String version,
List moduleDefinitions,
Map keyCategories) {
super(packageName, name);
this.encoding = encoding;
this.version = version;
List translationSets = new LinkedList<>();
List templates = new LinkedList<>();
Set locales = new LinkedHashSet<>();
for (I18nModuleDefinition i18nModuleDefinition : moduleDefinitions) {
for (I18nTranslationSetDefinition moduleTranslationSetDefinition : i18nModuleDefinition.getModuleTranslationSetDefinitions()) {
locales.add(moduleTranslationSetDefinition.getLocale());
}
translationSets.addAll(i18nModuleDefinition.getModuleTranslationSetDefinitions());
templates.addAll(i18nModuleDefinition.getModuleTemplateSetDefinitions());
}
this.locales = Collections.unmodifiableList(new LinkedList<>(locales));
this.keyCategories = keyCategories;
this.applicationTranslationDefinitions = new LinkedList<>();
this.dependenciesTranslationDefinitions = new LinkedList<>(translationSets);
this.applicationTemplateDefinitions = new LinkedList<>(templates);
}
public static Optional detect(ClassLoader classLoader) {
List result = new LinkedList<>();
for (I18nApplicationDefinition i18nModuleDefinition : ServiceLoader.load(I18nApplicationDefinition.class, classLoader)) {
result.add(i18nModuleDefinition);
}
if (result.size() == 1) {
return Optional.of(result.get(0));
}
return Optional.empty();
}
public Charset getEncoding() {
return encoding;
}
public String getVersion() {
return version;
}
public List getLocales() {
return locales;
}
public List getApplicationTranslationDefinitions() {
return applicationTranslationDefinitions;
}
public List getDependenciesTranslationDefinitions() {
return dependenciesTranslationDefinitions;
}
public List getApplicationTemplateDefinitions() {
return applicationTemplateDefinitions;
}
public Set getTemplateList() {
return applicationTemplateDefinitions.stream().map(I18nCoordinate::getName).collect(Collectors.toSet());
}
public Map getKeyCategories() {
return keyCategories;
}
}