Author: bpoussin Date: 2010-12-10 03:14:05 +0100 (Fri, 10 Dec 2010) New Revision: 1969 Url: http://nuiton.org/repositories/revision/nuiton-utils/1969 Log: Evolution #1144: [ApplicationConfig] add getOptionAsList() method Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java trunk/src/test/java/org/nuiton/util/StringUtilTest.java Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-12-09 17:29:05 UTC (rev 1968) +++ trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-12-10 02:14:05 UTC (rev 1969) @@ -273,6 +273,7 @@ /** Used to know what is separator between class and method on command line. */ static final private String CLASS_METHOD_SEPARATOR = "#"; + static final public String LIST_SEPARATOR = ","; /** Configuration file key option. */ static final public String CONFIG_FILE_NAME = "config.file"; @@ -574,6 +575,143 @@ } } + static protected class OptionList { + + protected ApplicationConfig config; + protected String key; + protected String value; + + public OptionList(ApplicationConfig config, String key, String value) { + this.config = config; + this.key = key; + this.value = value; + } + + /** + * Get option value as {@link String}. + * + * @param key the option's key + * @return value as String + */ + public List<String> getOption() { + List<String> result = (List<String>)config.convertOption( + String.class, key, value, true); + return result; + } + + /** + * Get option value as {@link File}. + * + * @param key the option's key + * @return value as file + */ + public List<File> getOptionAsFile() { + List<File> tmp = (List<File>)config.convertOption( + File.class, key, value, true); + List<File> result = new ArrayList<File>(tmp.size()); + for (File file : tmp) { + result.add(file.getAbsoluteFile()); + } + return result; + } + + /** + * Get option value as {@link URL}. + * + * @param key the option's key + * @return value as URL + */ + public List<URL> getOptionAsURL() { + List<URL> result = (List<URL>)config.convertOption( + URL.class, key, value, true); + return result; + } + + /** + * Get option value as {@link Class}. + * + * @param key the option's key + * @return value as Class + */ + public List<Class<?>> getOptionAsClass() { + List<Class<?>> result = (List<Class<?>>)config.convertOption( + Class.class, key, value, true); + return result; + } + + /** + * Get option value as {@link Date}. + * + * @param key the option's key + * @return value as Date + */ + public List<Date> getOptionAsDate() { + List<Date> result = (List<Date>)config.convertOption( + Date.class, key, value, true); + return result; + } + + /** + * Get option value as {@link Time}. + * + * @param key the option's key + * @return value as Time + */ + public List<Time> getOptionAsTime() { + List<Time> result = (List<Time>)config.convertOption( + Time.class, key, value, true); + return result; + } + + /** + * Get option value as {@link Timestamp}. + * + * @param key the option's key + * @return value as Timestamp + */ + public List<Timestamp> getOptionAsTimestamp() { + List<Timestamp> result = (List<Timestamp>)config.convertOption( + Timestamp.class, key, value, true); + return result; + } + + /** + * Get option value as {@code int}. + * + * @param key the option's key + * @return value as {@code int} + */ + public List<Integer> getOptionAsInt() { + List<Integer> result = (List<Integer>)config.convertOption( + Integer.class, key, value, true); + return result; + } + + /** + * Get option value as {@code double}. + * + * @param key the option's key + * @return value as {@code double} + */ + public List<Double> getOptionAsDouble() { + List<Double> result = (List<Double>)config.convertOption( + Double.class, key, value, true); + return result; + } + + /** + * Get option value as {@code boolean}. + * + * @param key the option's key + * @return value as {@code boolean}. + */ + public List<Boolean> getOptionAsBoolean() { + List<Boolean> result = (List<Boolean>)config.convertOption( + Boolean.class, key, value, true); + return result; + } + } + /** * Init ApplicationConfig with current simple class name as config file. * @@ -1146,31 +1284,79 @@ * @return typed value */ public <T> T getOption(Class<T> clazz, String key) { - String cacheKey = key + "-" + clazz.getName(); + String value = getOption(key); + T result = (T)convertOption(clazz, key, value, false); - String value = getOption(key); + return result; + } + + /** + * Convert value in instance of clazz or List if asList is true + * + * example: + * <li> convertOption(Boolean.class, "toto", "true,true", false) => false + * <li> convertOption(Boolean.class, "toto", "true,true", true) => [true, true] + * + * @param clazz result type expected + * @param key option key + * @param value value to convert + * @param asList value is string that represente a list + * @return + */ + protected <T> Object convertOption(Class<T> clazz, String key, String value, boolean asList) { + String cacheKey = key + "-" + asList + "-" + clazz.getName(); + int hash = 0; if (value != null) { hash = value.hashCode(); } - T result; - CacheItem<T> cacheItem = (CacheItem<T>) cacheOption.get(cacheKey); + + CacheItem cacheItem = cacheOption.get(cacheKey); // compute value if value don't exist in cacheOption or // if it's modified since last computation if (cacheItem == null || cacheItem.hash != hash) { - // prefer use our convertert method (auto-register more converters) - result = ConverterUtil.convert(clazz, value); - // result = (T) ConvertUtils.convert(value, clazz); - cacheItem = new CacheItem<T>(result, hash); + if (asList) { + List<T> list = null; + if (value != null) { + list = new ArrayList<T>(); + String[] values = StringUtil.split(value, LIST_SEPARATOR); + for (String valueString : values) { + // prefer use our convertert method (auto-register more converters) + T v = ConverterUtil.convert(clazz, valueString); + list.add(v); + } + } + cacheItem = new CacheItem(list, hash); + } else { + // prefer use our convertert method (auto-register more converters) + T v = ConverterUtil.convert(clazz, value); + cacheItem = new CacheItem(v, hash); + } + // add new item to the cache cacheOption.put(cacheKey, cacheItem); - } else { - result = cacheItem.item; } + // take result in item + Object result = cacheItem.item; + return result; } /** + * Help to convert value to list of object + * @param key + * @return + */ + public OptionList getOptionAsList(String key) { + String value = getOption(key); + OptionList result = null; + if (value != null){ + result = new OptionList(this, key, value); + } + return result; + } + + /** * Get option value as {@link File}. * * @param key the option's key Modified: trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2010-12-09 17:29:05 UTC (rev 1968) +++ trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2010-12-10 02:14:05 UTC (rev 1969) @@ -233,6 +233,29 @@ } /** + * Test convertion to list + */ + @Test + public void testGetAsList() { + List<String> asString = new ArrayList<String>(); + asString.add(ApplicationConfig.class.getName()); + asString.add(ApplicationConfigTest.class.getName()); + + List<Class> asClass = new ArrayList<Class>(); + asClass.add(ApplicationConfig.class); + asClass.add(ApplicationConfigTest.class); + + ApplicationConfig instance = new ApplicationConfig(); + Assert.assertEquals(null, instance.getOptionAsList("truc")); + + instance.setOption("truc", ApplicationConfig.class.getName() + + "," + ApplicationConfigTest.class.getName()); + + Assert.assertEquals(asString, instance.getOptionAsList("truc").getOption()); + Assert.assertEquals(asClass, instance.getOptionAsList("truc").getOptionAsClass()); + } + + /** * Test of getMethods method, of class ApplicationConfig. */ @Test Modified: trunk/src/test/java/org/nuiton/util/StringUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/StringUtilTest.java 2010-12-09 17:29:05 UTC (rev 1968) +++ trunk/src/test/java/org/nuiton/util/StringUtilTest.java 2010-12-10 02:14:05 UTC (rev 1969) @@ -101,7 +101,7 @@ @Test public void testSplit() { - assertTrue(Arrays.equals(StringUtil.split("toto,titi,tutu"), new String[]{"toto", "titi", "tutu"})); + assertTrue(Arrays.equals(StringUtil.split("'toto',titi,tutu"), new String[]{"'toto'", "titi", "tutu"})); assertTrue(Arrays.equals(StringUtil.split("toto"), new String[]{"toto"})); assertTrue(Arrays.equals(StringUtil.split(""), EMPTY_STRING_ARRAY)); assertTrue(Arrays.equals(StringUtil.split(null), EMPTY_STRING_ARRAY));