This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git commit c189a712eb510900688a3fd04b61745053a3b87c Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 17:42:34 2016 +0200 clean code --- .../java/org/nuiton/config/ApplicationConfig.java | 51 +++++++++------------- .../org/nuiton/config/ApplicationConfigHelper.java | 12 ++--- .../nuiton/config/ArgumentsParserException.java | 2 +- .../nuiton/config/OverwriteApplicationConfig.java | 6 +-- .../org/nuiton/config/SubApplicationConfig.java | 5 +-- .../org/nuiton/config/ApplicationConfigTest.java | 32 +++++--------- .../nuiton/config/ChangeApplicationNameTest.java | 6 +-- 7 files changed, 45 insertions(+), 69 deletions(-) diff --git a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java index 3b9a616..e375e5e 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java @@ -436,27 +436,27 @@ public class ApplicationConfig { /** * TODO */ - protected Map<String, CacheItem<?>> cacheOption = new HashMap<String, CacheItem<?>>(); + protected Map<String, CacheItem<?>> cacheOption = new HashMap<>(); /** * TODO */ - protected Map<Class<?>, Object> cacheAction = new HashMap<Class<?>, Object>(); + protected Map<Class<?>, Object> cacheAction = new HashMap<>(); /** * contient apres l'appel de parse, la liste des arguments non utilises */ - protected List<String> unparsed = new ArrayList<String>(); + protected List<String> unparsed = new ArrayList<>(); /** * TODO */ - protected Map<String, List<String>> aliases = new HashMap<String, List<String>>(); + protected Map<String, List<String>> aliases = new HashMap<>(); /** * TODO */ - protected Map<Integer, List<Action>> actions = new HashMap<Integer, List<Action>>(); + protected Map<Integer, List<Action>> actions = new HashMap<>(); /** * Internal state to manage with masse operations on option and control @@ -482,7 +482,7 @@ public class ApplicationConfig { /** * permet de conserver des objets associe avec ce ApplicationConfig */ - protected Map<String, Object> context = new HashMap<String, Object>(); + protected Map<String, Object> context = new HashMap<>(); /** * Init ApplicationConfig with current simple class name as config file. @@ -803,7 +803,7 @@ public class ApplicationConfig { */ public void cleanUserConfig(String... excludeKeys) throws ApplicationConfigSaveException { - Set<String> keys = new HashSet<String>(homefile.stringPropertyNames()); + Set<String> keys = new HashSet<>(homefile.stringPropertyNames()); List<String> toExclude = Arrays.asList(excludeKeys); @@ -861,7 +861,7 @@ public class ApplicationConfig { Integer step = action.step; List<Action> list = actions.get(step); if (list == null) { - list = new LinkedList<Action>(); + list = new LinkedList<>(); actions.put(step, list); } list.add(action); @@ -876,7 +876,7 @@ public class ApplicationConfig { * @since 2.4 */ public List<Integer> getActionStep() { - List<Integer> result = new ArrayList<Integer>(actions.keySet()); + List<Integer> result = new ArrayList<>(actions.keySet()); Collections.sort(result); return result; } @@ -1554,7 +1554,7 @@ public class ApplicationConfig { // if it's modified since last computation if (cacheItem == null || cacheItem.hash != hash) { if (asList) { - List<T> list = new ArrayList<T>(); + List<T> list = new ArrayList<>(); if (value != null) { String[] values = StringUtils.split(value, LIST_SEPARATOR); for (String valueString : values) { @@ -1563,11 +1563,11 @@ public class ApplicationConfig { list.add(v); } } - cacheItem = new CacheItem<List<T>>(list, hash); + cacheItem = new CacheItem<>(list, hash); } else { // prefer use our converter method (auto-register more converters) T v = ConverterUtil.convert(clazz, value); - cacheItem = new CacheItem<T>(v, hash); + cacheItem = new CacheItem<>(v, hash); } // add new item to the cache cacheOption.put(cacheKey, cacheItem); @@ -1630,11 +1630,8 @@ public class ApplicationConfig { public Properties getOptionAsProperties(String key) throws IOException { File file = getOptionAsFile(key); Properties prop = new RecursiveProperties(); - FileReader reader = new FileReader(file); - try { + try (FileReader reader = new FileReader(file)) { prop.load(reader); - } finally { - reader.close(); } return prop; } @@ -1896,7 +1893,7 @@ public class ApplicationConfig { protected Map<String, Method> getMethods() { // looking for all methods set on ApplicationConfig Method[] allMethods = getClass().getMethods(); - Map<String, Method> methods = new HashMap<String, Method>(); + Map<String, Method> methods = new HashMap<>(); for (Method m : allMethods) { String methodName = m.getName(); if (methodName.startsWith("set")) { @@ -1916,7 +1913,7 @@ public class ApplicationConfig { * @return the arguments found for the given method */ protected String[] getParams(Method m, ListIterator<String> args) { - List<String> result = new ArrayList<String>(); + List<String> result = new ArrayList<>(); if (m.isVarArgs()) { while (args.hasNext()) { String p = args.next(); @@ -2024,7 +2021,7 @@ public class ApplicationConfig { try { Map<String, Method> methods = getMethods(); - List<String> arguments = new ArrayList<String>(args.length); + List<String> arguments = new ArrayList<>(args.length); for (String arg : args) { if (aliases.containsKey(arg)) { arguments.addAll(aliases.get(arg)); @@ -2070,7 +2067,7 @@ public class ApplicationConfig { // classpath String filename = getConfigFileName(); Enumeration<URL> enumInClasspath = ClassLoader.getSystemClassLoader().getResources(filename); - Set<URL> urlsInClasspath = new HashSet<URL>(EnumerationUtils.toList(enumInClasspath)); + Set<URL> urlsInClasspath = new HashSet<>(EnumerationUtils.toList(enumInClasspath)); enumInClasspath = ApplicationConfig.class.getClassLoader().getResources(filename); urlsInClasspath.addAll(EnumerationUtils.toList(enumInClasspath)); @@ -2205,12 +2202,8 @@ public class ApplicationConfig { * @since 2.3 */ protected void loadResource(URI uri, Properties properties) throws IOException { - InputStreamReader reader = - new InputStreamReader(uri.toURL().openStream(), getEncoding()); - try { + try (InputStreamReader reader = new InputStreamReader(uri.toURL().openStream(), getEncoding())) { properties.load(reader); - } finally { - reader.close(); } } @@ -2228,12 +2221,8 @@ public class ApplicationConfig { protected void saveResource(File file, Properties properties, String comment) throws IOException { - Writer reader = - new OutputStreamWriter(new FileOutputStream(file), getEncoding()); - try { + try (Writer reader = new OutputStreamWriter(new FileOutputStream(file), getEncoding())) { properties.store(reader, comment); - } finally { - reader.close(); } } @@ -2457,7 +2446,7 @@ public class ApplicationConfig { */ public List<File> getOptionAsFile() { List<File> tmp = convertListOption(File.class); - List<File> result = new ArrayList<File>(tmp.size()); + List<File> result = new ArrayList<>(tmp.size()); for (File file : tmp) { result.add(file.getAbsoluteFile()); } diff --git a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigHelper.java b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigHelper.java index 23dd74e..036e7f7 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigHelper.java +++ b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigHelper.java @@ -68,7 +68,7 @@ public class ApplicationConfigHelper { } Set<ApplicationConfigProvider> result = - new HashSet<ApplicationConfigProvider>(); + new HashSet<>(); for (ApplicationConfigProvider configProvider : loader) { String name = configProvider.getName(); @@ -162,7 +162,7 @@ public class ApplicationConfigHelper { * @since 2.6.7 */ public static Set<ConfigOptionDef> getTransientOptions(Set<ApplicationConfigProvider> providers) { - Set<ConfigOptionDef> result = new HashSet<ConfigOptionDef>(); + Set<ConfigOptionDef> result = new HashSet<>(); for (ApplicationConfigProvider provider : providers) { for (ConfigOptionDef def : provider.getOptions()) { if (def.isTransient()) { @@ -182,7 +182,7 @@ public class ApplicationConfigHelper { * @since 2.6.7 */ public static Set<ConfigOptionDef> getFinalOptions(Set<ApplicationConfigProvider> providers) { - Set<ConfigOptionDef> result = new HashSet<ConfigOptionDef>(); + Set<ConfigOptionDef> result = new HashSet<>(); for (ApplicationConfigProvider provider : providers) { for (ConfigOptionDef def : provider.getOptions()) { if (def.isFinal()) { @@ -206,7 +206,7 @@ public class ApplicationConfigHelper { * @since 2.6.11 */ public static Set<String> getTransientOrFinalOptionKey(Set<ApplicationConfigProvider> providers) { - Set<String> result = new HashSet<String>(); + Set<String> result = new HashSet<>(); result.addAll(getTransientOptionKeys(providers)); result.addAll(getFinalOptionKeys(providers)); return result; @@ -221,7 +221,7 @@ public class ApplicationConfigHelper { * @since 2.6.11 */ public static Set<String> getTransientOptionKeys(Set<ApplicationConfigProvider> providers) { - Set<String> result = new HashSet<String>(); + Set<String> result = new HashSet<>(); for (ApplicationConfigProvider provider : providers) { for (ConfigOptionDef def : provider.getOptions()) { if (def.isTransient()) { @@ -241,7 +241,7 @@ public class ApplicationConfigHelper { * @since 2.6.7 */ public static Set<String> getFinalOptionKeys(Set<ApplicationConfigProvider> providers) { - Set<String> result = new HashSet<String>(); + Set<String> result = new HashSet<>(); for (ApplicationConfigProvider provider : providers) { for (ConfigOptionDef def : provider.getOptions()) { if (def.isFinal()) { diff --git a/nuiton-config/src/main/java/org/nuiton/config/ArgumentsParserException.java b/nuiton-config/src/main/java/org/nuiton/config/ArgumentsParserException.java index dbad7ee..90ae844 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/ArgumentsParserException.java +++ b/nuiton-config/src/main/java/org/nuiton/config/ArgumentsParserException.java @@ -4,7 +4,7 @@ package org.nuiton.config; * #%L * Nuiton Config :: API * %% - * Copyright (C) 2016 Code Lutin, Tony Chemit + * Copyright (C) 2016 Code Lutin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as diff --git a/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java index 11aa139..3754b27 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java @@ -4,7 +4,7 @@ package org.nuiton.config; * #%L * Nuiton Config :: API * %% - * Copyright (C) 2016 Code Lutin, Tony Chemit + * Copyright (C) 2016 Code Lutin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -35,10 +35,6 @@ import org.apache.commons.logging.LogFactory; * @see ApplicationConfig#getConfig(Map) * * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ */ public class OverwriteApplicationConfig extends ApplicationConfig { diff --git a/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java index 708c79c..9f3134b 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java @@ -4,7 +4,7 @@ package org.nuiton.config; * #%L * Nuiton Config :: API * %% - * Copyright (C) 2016 Code Lutin, Tony Chemit + * Copyright (C) 2016 Code Lutin * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -81,8 +81,7 @@ public class SubApplicationConfig extends ApplicationConfig { @Override public boolean hasOption(String key) { - boolean result = getOption(key) != null; - return result; + return getOption(key) != null; } @Override diff --git a/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java b/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java index 92bb784..3e90c98 100644 --- a/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java +++ b/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java @@ -134,9 +134,7 @@ public class ApplicationConfigTest { } finally { - if (oldHome != null) { - System.setProperty("user.home", oldHome); - } + System.setProperty("user.home", oldHome); } } @@ -223,9 +221,7 @@ public class ApplicationConfigTest { Assert.assertEquals("toto", property); } finally { - if (oldHome != null) { - System.setProperty("user.home", oldHome); - } + System.setProperty("user.home", oldHome); } } @@ -233,7 +229,7 @@ public class ApplicationConfigTest { @Test public void getUnparsed() throws Exception { ApplicationConfig instance = new ApplicationConfig(); - List<String> expResult = new ArrayList<String>(); + List<String> expResult = new ArrayList<>(); List<String> result = instance.getUnparsed(); Assert.assertEquals(expResult, result); @@ -248,13 +244,12 @@ public class ApplicationConfigTest { @Test public void addAction() throws Exception { - Action action = null; ApplicationConfig instance = new ApplicationConfig(); // test add null Action - instance.addAction(action); + instance.addAction(null); - action = new Action(1, new DummyAction(), DummyAction.class.getMethod("dummyAction", String.class, Integer.TYPE), "coucou", "12"); + Action action = new Action(1, new DummyAction(), DummyAction.class.getMethod("dummyAction", String.class, Integer.TYPE), "coucou", "12"); instance.addAction(action); } @@ -291,7 +286,7 @@ public class ApplicationConfigTest { instance.addAlias("toto", "totochange"); instance.addAlias("titi", "titichange"); - List<String> expResult = new ArrayList<String>(); + List<String> expResult = new ArrayList<>(); List<String> result = instance.getUnparsed(); Assert.assertEquals(expResult, result); @@ -344,11 +339,11 @@ public class ApplicationConfigTest { @Test public void getAsList() { - List<String> asString = new ArrayList<String>(); + List<String> asString = new ArrayList<>(); asString.add(ApplicationConfig.class.getName()); asString.add(ApplicationConfigTest.class.getName()); - List<Class> asClass = new ArrayList<Class>(); + List<Class> asClass = new ArrayList<>(); asClass.add(ApplicationConfig.class); asClass.add(ApplicationConfigTest.class); @@ -373,7 +368,7 @@ public class ApplicationConfigTest { @Test public void getParams() throws Exception { Method m = DummyAction.class.getMethod("dummyAction", String.class, Integer.TYPE); - List<String> list = new ArrayList<String>(Arrays.asList("toto", "10", "/tmp", "9")); + List<String> list = new ArrayList<>(Arrays.asList("toto", "10", "/tmp", "9")); ListIterator<String> args = list.listIterator(); ApplicationConfig instance = new ApplicationConfig(); @@ -385,7 +380,7 @@ public class ApplicationConfigTest { @Test public void testCreateAction() throws Exception { - List<String> list = new ArrayList<String>(Arrays.asList("dummy", "toto", "10", "/tmp", "9")); + List<String> list = new ArrayList<>(Arrays.asList("dummy", "toto", "10", "/tmp", "9")); ListIterator<String> args = list.listIterator(); args.next(); ApplicationConfig instance = new ApplicationConfig(); @@ -535,11 +530,8 @@ public class ApplicationConfigTest { // get content of printConfig ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - try { + try (PrintStream ps = new PrintStream(baos)) { instance.printConfig(ps); - } finally { - ps.close(); } String content = baos.toString("UTF-8"); @@ -596,7 +588,7 @@ public class ApplicationConfigTest { // test long is not null long actual = instance.getOptionAsLong("toto"); Assert.assertNotNull(actual); - Assert.assertEquals(0l, actual); + Assert.assertEquals(0L, actual); // not null version long expected = System.currentTimeMillis(); diff --git a/nuiton-config/src/test/java/org/nuiton/config/ChangeApplicationNameTest.java b/nuiton-config/src/test/java/org/nuiton/config/ChangeApplicationNameTest.java index 1a9019f..7c5b61f 100644 --- a/nuiton-config/src/test/java/org/nuiton/config/ChangeApplicationNameTest.java +++ b/nuiton-config/src/test/java/org/nuiton/config/ChangeApplicationNameTest.java @@ -35,7 +35,7 @@ import org.junit.Test; */ public class ChangeApplicationNameTest { - enum WaoApplicationConfigOption implements ConfigOptionDef { + private enum WaoApplicationConfigOption implements ConfigOptionDef { INSTANCE_URL("wao.instanceUrl", "URL à laquelle on peut accéder à l'instance de WAO", "http://localhost:8080/wao", String.class); @@ -48,7 +48,7 @@ public class ChangeApplicationNameTest { protected String defaultValue; - private WaoApplicationConfigOption(String key, String description, + WaoApplicationConfigOption(String key, String description, String defaultValue, Class<?> type) { this.key = key; this.description = description; @@ -116,7 +116,7 @@ public class ChangeApplicationNameTest { applicationConfig.parse(); - // This option is overidde by the config file + // This option is overridden by the config file String option = applicationConfig.getOption(String.class, WaoApplicationConfigOption.INSTANCE_URL.key); Assert.assertEquals("http://localhost:9090/wao", option); } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.