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

fr.ird.observe.dto.ObserveUtil Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.dto;

/*-
 * #%L
 * ObServe Toolkit :: Common Dto
 * %%
 * Copyright (C) 2017 - 2020 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import io.ultreia.java4all.config.ApplicationConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.config.Configurator;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Pour mettre du code util commun.
 * 

* Created on 01/09/16. * * @author Tony Chemit - [email protected] * @since 5.0 */ public class ObserveUtil { public static final String SQL_GZ_EXTENSION_PATTERN = "^.+\\.sql\\.gz|.+\\.SQL\\.GZ$"; public static final String SQL_GZ_EXTENSION = ".sql.gz"; public static final String PROPERTIES_EXTENSION_PATTERN = "^.+\\.properties|.+\\.PROPERTIES$"; public static final String PROPERTIES_EXTENSION = ".properties"; public static final String PNG_EXTENSION_PATTERN = "^.+\\.png|.+\\.PNG$"; public static final String PNG_EXTENSION = ".png"; public static > List sortTypes(Collection types, Function function, Locale locale) { List list = new ArrayList<>(types); new ClassComparator(function, locale).sort(list); return list; } public static void loadLogConfiguration(URL logInput, Path logFile, Supplier configSupplier) throws IOException { if (!Files.exists(logFile)) { List finalLogConfigurationProperties = new LinkedList<>(); ApplicationConfig applicationConfig = configSupplier.get(); try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(logInput.openStream(), StandardCharsets.UTF_8))) { String line; while ((line = bufferedReader.readLine()) != null) { finalLogConfigurationProperties.add(applicationConfig.replaceRecursiveOptions(line)); } } Files.write(logFile, finalLogConfigurationProperties); } Configurator.initialize(null, logFile.toFile().getAbsolutePath()); } public static void cleanMemory() { System.runFinalization(); System.gc(); } public static String addSqlGzExtension(String filePath) { if (!filePath.endsWith(SQL_GZ_EXTENSION)) { filePath += SQL_GZ_EXTENSION; } return filePath; } public static String removeSqlGzExtension(String filePath) { if (filePath.endsWith(SQL_GZ_EXTENSION)) { filePath = StringUtils.removeEnd(filePath, SQL_GZ_EXTENSION); } return filePath; } public static boolean withSqlGzExtension(String filePath) { return filePath.endsWith(SQL_GZ_EXTENSION); } public static InputStream openInternalStream(URL resource) { try { return Objects.requireNonNull(resource.openStream(), "Could not find internal resource " + resource); } catch (Exception e) { throw new IllegalStateException("Could not treat internal resource " + resource); } } public static void copyResource(URL resource, File file) throws IOException { try (InputStream in = openInternalStream(resource)) { Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); } } private static class ClassComparator> implements Comparator { private final Map cache; private final Function function; private final Collator collator; private ClassComparator(Function function, Locale locale) { this.cache = new HashMap<>(); this.function = function; this.collator = Collator.getInstance(locale); this.collator.setStrength(Collator.PRIMARY); } @Override public int compare(Class o1, Class o2) { String s1 = getValue(o1); String s2 = getValue(o2); return this.collator.compare(s1, s2); } String getValue(Class klass) { return cache.computeIfAbsent(klass, k -> function.apply(klass)); } public void sort(List list) { list.sort(this); cache.clear(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy