TAMAYA-307: Readded collections module.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/4c259384 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/4c259384 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/4c259384 Branch: refs/heads/master Commit: 4c25938401a26f780cd76659e69331679352b792 Parents: 636dbb5 Author: anatole <[email protected]> Authored: Thu Sep 21 18:23:30 2017 +0200 Committer: anatole <[email protected]> Committed: Thu Sep 21 18:23:30 2017 +0200 ---------------------------------------------------------------------- collections/pom.xml | 53 +++++ .../collections/AdaptiveCombinationPolicy.java | 122 ++++++++++ .../tamaya/collections/ArrayListConverter.java | 62 +++++ .../tamaya/collections/CollectionConverter.java | 63 +++++ .../collections/ConcurrentHashMapConverter.java | 63 +++++ .../tamaya/collections/HashMapConverter.java | 63 +++++ .../tamaya/collections/HashSetConverter.java | 62 +++++ .../tamaya/collections/ItemTokenizer.java | 171 ++++++++++++++ .../tamaya/collections/LinkedListConverter.java | 63 +++++ .../tamaya/collections/ListConverter.java | 55 +++++ .../apache/tamaya/collections/MapConverter.java | 58 +++++ .../apache/tamaya/collections/SetConverter.java | 55 +++++ .../tamaya/collections/SortedMapConverter.java | 36 +++ .../tamaya/collections/SortedSetConverter.java | 36 +++ .../tamaya/collections/TreeMapConverter.java | 62 +++++ .../tamaya/collections/TreeSetConverter.java | 63 +++++ .../org.apache.tamaya.spi.PropertyConverter | 31 +++ ...he.tamaya.spi.PropertyValueCombinationPolicy | 19 ++ .../collections/CollectionAdvancedTests.java | 117 ++++++++++ .../collections/CollectionsBaseTests.java | 227 +++++++++++++++++++ .../CollectionsTypedReadOnlyTests.java | 173 ++++++++++++++ .../collections/CollectionsTypedTests.java | 208 +++++++++++++++++ .../collections/MyUpperCaseConverter.java | 33 +++ .../META-INF/javaconfiguration.properties | 73 ++++++ 24 files changed, 1968 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/pom.xml ---------------------------------------------------------------------- diff --git a/collections/pom.xml b/collections/pom.xml new file mode 100644 index 0000000..b11c807 --- /dev/null +++ b/collections/pom.xml @@ -0,0 +1,53 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy current the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-extensions</artifactId> + <version>0.4-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>tamaya-collections</artifactId> + <name>Apache Tamaya Modules - Collections Support</name> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/AdaptiveCombinationPolicy.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/AdaptiveCombinationPolicy.java b/collections/src/main/java/org/apache/tamaya/collections/AdaptiveCombinationPolicy.java new file mode 100644 index 0000000..f10f7ec --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/AdaptiveCombinationPolicy.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.apache.tamaya.spi.PropertyValueCombinationPolicy; + +import javax.annotation.Priority; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyValueCombinationPolicy that allows to configure a PropertyValueCombinationPolicy + * for each key individually, by adding a configured entry of the form + * {@code _key.combination-policy=collect|override|fqPolicyClassName}. + */ +@Priority(100) +public class AdaptiveCombinationPolicy implements PropertyValueCombinationPolicy { + /** Logger. */ + private static final Logger LOG = Logger.getLogger(AdaptiveCombinationPolicy.class.getName()); + + /** + * Collecting combination policy using (optional) {@code item-separator} parameter for determining the sparator + * to combine multiple config entries found. + */ + private static final PropertyValueCombinationPolicy COLLECTING_POLICY = new PropertyValueCombinationPolicy(){ + @Override + public PropertyValue collect(PropertyValue currentValue, String key, PropertySource propertySource) { + // check for default collection combination policies for lists, sets, maps etc. + final String separator = ConfigurationProvider.getConfiguration().getOrDefault('_' + key+".item-separator", ","); + PropertyValue newValue = propertySource.get(key); + if(newValue!=null){ + if(currentValue==null){ + return newValue; + } + String oldVal = currentValue.getValue(); + newValue = newValue.toBuilder() + .setValue(oldVal + ',' + newValue.getValue()) + .addMetaEntry("sources", currentValue.getSource() + "\n" + newValue.getSource()) + .build(); + return newValue; + }else{ + if(currentValue!=null){ + return currentValue; + } + return null; + } + } + }; + + /** Cache for loaded custom combination policies. */ + private Map<Class, PropertyValueCombinationPolicy> configuredPolicies = new ConcurrentHashMap<>(); + + @Override + public PropertyValue collect(PropertyValue currentValue, String key, PropertySource propertySource){ + if(key.startsWith("_")){ + PropertyValue newValue = propertySource.get(key); + if(newValue!=null){ + return newValue; + } + return currentValue; + } + String adaptiveCombinationPolicyClass = ConfigurationProvider.getConfiguration().getOrDefault( + '_' + key+".combination-policy", "override"); + PropertyValueCombinationPolicy combinationPolicy = null; + switch(adaptiveCombinationPolicyClass){ + case "collect": + case "COLLECT": + if(LOG.isLoggable(Level.FINEST)){ + LOG.finest("Using collecting combination policy for key: " + key + ""); + } + combinationPolicy = COLLECTING_POLICY; + break; + case "override": + case "OVERRIDE": + if(LOG.isLoggable(Level.FINEST)){ + LOG.finest("Using default (overriding) combination policy for key: " + key + ""); + } + combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + break; + default: + try{ + Class<PropertyValueCombinationPolicy> clazz = (Class<PropertyValueCombinationPolicy>) + Class.forName(adaptiveCombinationPolicyClass); + combinationPolicy = configuredPolicies.get(clazz); + if(combinationPolicy==null){ + combinationPolicy = clazz.newInstance(); + configuredPolicies.put(clazz, combinationPolicy); + } + if(LOG.isLoggable(Level.FINEST)){ + LOG.finest("Using custom combination policy "+adaptiveCombinationPolicyClass+" for " + + "key: " + key + ""); + } + } catch(Exception e){ + LOG.log(Level.SEVERE, "Error loading configured PropertyValueCombinationPolicy for " + + "key: " + key + ", using default (overriding) policy.", e); + combinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR; + } + } + return combinationPolicy.collect(currentValue, key, propertySource); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java b/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java new file mode 100644 index 0000000..189c4a0 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating ArrayList representation of a values. + */ +public class ArrayListConverter implements PropertyConverter<ArrayList> { + + private static final Logger LOG = Logger.getLogger(ArrayListConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final ArrayListConverter INSTANCE = new ArrayListConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static ArrayListConverter getInstance(){ + return INSTANCE; + } + + @Override + public ArrayList convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + ArrayList<Object> mlist = new ArrayList<>(); + for(String raw:rawList){ + Object convValue = ItemTokenizer.convertValue(raw, context); + if (convValue != null) { + mlist.add(convValue); + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return mlist; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java b/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java new file mode 100644 index 0000000..91443ea --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collection; +import java.util.Collections; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class CollectionConverter implements PropertyConverter<Collection> { + + @Override + public Collection convert(String value, ConversionContext context) { + String collectionType = context.getConfiguration().getOrDefault('_' + context.getKey()+".collection-type", "List"); + if(collectionType.startsWith("java.util.")){ + collectionType = collectionType.substring("java.util.".length()); + } + Collection result = null; + switch(collectionType){ + case "LinkedList": + result = LinkedListConverter.getInstance().convert(value, context); + break; + case "Set": + case "HashSet": + result = HashSetConverter.getInstance().convert(value, context); + break; + case "SortedSet": + case "TreeSet": + result = TreeSetConverter.getInstance().convert(value, context); + break; + case "List": + case "ArrayList": + default: + result = ArrayListConverter.getInstance().convert(value, context); + break; + } + if(context.getConfiguration().getOrDefault('_' + context.getKey()+".read-only", + Boolean.class, Boolean.TRUE)){ + return Collections.unmodifiableCollection(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java b/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java new file mode 100644 index 0000000..b19fd0c --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating ConcurrentHashMap representation of a values. + */ +public class ConcurrentHashMapConverter implements PropertyConverter<ConcurrentHashMap> { + private static final Logger LOG = Logger.getLogger(ConcurrentHashMapConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final ConcurrentHashMapConverter INSTANCE = new ConcurrentHashMapConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static ConcurrentHashMapConverter getInstance(){ + return INSTANCE; + } + + @Override + public ConcurrentHashMap convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + ConcurrentHashMap result = new ConcurrentHashMap(rawList.size()); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.put(items[0], convValue); + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java b/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java new file mode 100644 index 0000000..d223145 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.HashMap; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating HashMap representation of a values. + */ +public class HashMapConverter implements PropertyConverter<HashMap> { + private static final Logger LOG = Logger.getLogger(HashMapConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final HashMapConverter INSTANCE = new HashMapConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static HashMapConverter getInstance(){ + return INSTANCE; + } + + @Override + public HashMap convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + HashMap result = new HashMap(rawList.size()); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.put(items[0], convValue); + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java new file mode 100644 index 0000000..a8e70cf --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.HashSet; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating HashSet representation of a values. + */ +public class HashSetConverter implements PropertyConverter<HashSet> { + + private static final Logger LOG = Logger.getLogger(HashSetConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final HashSetConverter INSTANCE = new HashSetConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static HashSetConverter getInstance(){ + return INSTANCE; + } + + @Override + public HashSet convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + HashSet<Object> result = new HashSet<>(); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.add(convValue); + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java b/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java new file mode 100644 index 0000000..60e6e7f --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Helper class that implements the tokenizing of the entries of a configuration value. + */ +final class ItemTokenizer { + + private static final Logger LOG = Logger.getLogger(ItemTokenizer.class.getName()); + + /** + * Private singleton. + */ + private ItemTokenizer(){} + + /** + * Splits the given value using the given separator. Matcjhing is done by traversing the String value using + * {@code indexOf} calls, one by one. The last unresolvable item (without any next separator token) + * is added at the end of the list. + * @param value the value, not null. + * @param context the conversion context. + * @return the tokenized value as list, in order of occurrence. + */ + public static List<String> split(String value, ConversionContext context){ + return split(value, ConfigurationProvider.getConfiguration().getOrDefault( + '_' + context.getKey()+ "" + + "item-separator", ",")); + } + + /** + * Splits the given value using the given separator. Matcjhing is done by traversing the String value using + * {@code indexOf} calls, one by one. The last unresolvable item (without any next separator token) + * is added at the end of the list. + * @param value the value, not null. + * @param separator the separator to be used. + * @return the tokenized value as list, in order of occurrence. + */ + public static List<String> split(String value, final String separator) { + ArrayList<String> result = new ArrayList<>(); + int start = 0; + int end = value.indexOf(separator,start); + while(end>0) { + if (value.charAt(end - 1) != '\\') { + result.add(value.substring(start, end)); + start = end + separator.length(); + end = value.indexOf(separator,start); + }else{ + end = value.indexOf(separator,end + separator.length()); + } + end = value.indexOf(separator,start); + } + if(start < value.length()){ + result.add(value.substring(start)); + } + return result; + } + + /** + * plits the given String value as a map entry, splitting it into key and value part with the given separator. + * If the value cannot be split then {@code key = value = mapEntry} is used for further processing. key or value + * parts are normally trimmed, unless they are enmcosed with brackets {@code []}. + * @param mapEntry the entry, not null. + * @param context the conversion context. + * @return an array of length 2, with the trimmed and parsed key/value pair. + */ + public static String[] splitMapEntry(String mapEntry, ConversionContext context){ + return splitMapEntry(mapEntry, ConfigurationProvider.getConfiguration().getOrDefault( + '_' + context.getKey()+".map-entry-separator", "::")); + } + + /** + * Splits the given String value as a map entry, splitting it into key and value part with the given separator. + * If the value cannot be split then {@code key = value = mapEntry} is used for further processing. key or value + * parts are normally trimmed, unless they are enmcosed with brackets {@code []}. + * @param mapEntry the entry, not null. + * @param separator the separator, not null. + * @return an array of length 2, with the trimmed and parsed key/value pair. + */ + public static String[] splitMapEntry(final String mapEntry, final String separator) { + int index = mapEntry.indexOf(separator); + String[] items; + if(index<0) { + items = new String[]{mapEntry, mapEntry}; + }else { + items = new String[]{mapEntry.substring(0,index), + mapEntry.substring(index+separator.length())}; + } + if(items[0].trim().startsWith("[")){ + items[0]= items[0].trim(); + items[0] = items[0].substring(1); + }else{ + items[0]= items[0].trim(); + } + if(items[1].trim().endsWith("]")){ + items[1] = items[1].substring(0,items[1].length()-1); + }else{ + items[1]= items[1].trim(); + } + return items; + } + + /** + * Parses the given value into the required collection target type, defined by the context. + * @param value the raw String value. + * @param context the context + * @return the parsed value, or null. + */ + public static Object convertValue(String value, ConversionContext context) { + String converterClass = context.getConfiguration().get('_' + context.getKey() + ".item-converter"); + List<PropertyConverter<Object>> valueConverters = new ArrayList<>(1); + if (converterClass != null) { + try { + valueConverters.add((PropertyConverter<Object>) Class.forName(converterClass).newInstance()); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); + } + } + if (TypeLiteral.getTypeParameters(context.getTargetType().getType()).length>0) { + valueConverters.addAll(context.getConfigurationContext().getPropertyConverters( + TypeLiteral.of(TypeLiteral.getTypeParameters(context.getTargetType().getType())[0]))); + } + ConversionContext ctx = new ConversionContext.Builder(context.getConfiguration(), + context.getConfigurationContext(), context.getKey(), + TypeLiteral.of(context.getTargetType().getType())).build(); + Object result = null; + if (valueConverters.isEmpty()) { + return value; + } else { + for (PropertyConverter<Object> conv : valueConverters) { + try { + result = conv.convert(value, ctx); + if (result != null) { + return result; + } + } catch (Exception e) { + LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); + } + } + } + LOG.log(Level.SEVERE, "Failed to convert collection value type for '" + value + "'."); + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java b/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java new file mode 100644 index 0000000..3846ac2 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating LinkedList representation of a values. + */ +public class LinkedListConverter implements PropertyConverter<LinkedList> { + private static final Logger LOG = Logger.getLogger(LinkedListConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final LinkedListConverter INSTANCE = new LinkedListConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static LinkedListConverter getInstance(){ + return INSTANCE; + } + + @Override + public LinkedList convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + LinkedList<Object> result = new LinkedList<>(); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.add(convValue); + continue; + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java b/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java new file mode 100644 index 0000000..54aae36 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collections; +import java.util.List; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class ListConverter implements PropertyConverter<List> { + + @Override + public List convert(String value, ConversionContext context) { + String collectionType = context.getConfiguration().getOrDefault('_' + context.getKey()+".collection-type", "List"); + if(collectionType.startsWith("java.util.")){ + collectionType = collectionType.substring("java.util.".length()); + } + List result = null; + switch(collectionType){ + case "LinkedList": + result = LinkedListConverter.getInstance().convert(value, context); + break; + case "List": + case "ArrayList": + default: + result = ArrayListConverter.getInstance().convert(value, context); + break; + } + if(context.getConfiguration().getOrDefault('_' + context.getKey()+".read-only", + Boolean.class, Boolean.TRUE)){ + return Collections.unmodifiableList(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java b/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java new file mode 100644 index 0000000..559c9eb --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collections; +import java.util.Map; + +/** + * PropertyConverter for gnerating HashMap representation of a values. + */ +public class MapConverter implements PropertyConverter<Map> { + + @Override + public Map convert(String value, ConversionContext context) { + String collectionType = context.getConfiguration().getOrDefault('_' + context.getKey()+".collection-type", "Map"); + if(collectionType.startsWith("java.util.")){ + collectionType = collectionType.substring("java.util.".length()); + } + Map result = null; + switch(collectionType){ + case "TreeMap": + result = TreeMapConverter.getInstance().convert(value, context); + break; + case "ConcurrentHashMap": + result = ConcurrentHashMapConverter.getInstance().convert(value, context); + break; + case "Map": + case "HashMap": + default: + result = HashMapConverter.getInstance().convert(value, context); + break; + } + if(context.getConfiguration().getOrDefault('_' + context.getKey()+".read-only", + Boolean.class, Boolean.TRUE)){ + return Collections.unmodifiableMap(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java new file mode 100644 index 0000000..b6e9ae6 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collections; +import java.util.Set; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class SetConverter implements PropertyConverter<Set> { + + @Override + public Set convert(String value, ConversionContext context) { + String collectionType = context.getConfiguration().getOrDefault('_' + context.getKey()+".collection-type", "Set"); + if(collectionType.startsWith("java.util.")){ + collectionType = collectionType.substring("java.util.".length()); + } + Set result = null; + switch(collectionType){ + case "TreeSet": + result = TreeSetConverter.getInstance().convert(value, context); + break; + case "Set": + case "HashSet": + default: + result = HashSetConverter.getInstance().convert(value, context); + break; + } + if(context.getConfiguration().getOrDefault('_' + context.getKey()+".read-only", + Boolean.class, Boolean.TRUE)){ + return Collections.unmodifiableSet(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java b/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java new file mode 100644 index 0000000..5416e28 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collections; +import java.util.SortedMap; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class SortedMapConverter implements PropertyConverter<SortedMap> { + + @Override + public SortedMap convert(String value, ConversionContext context) { + return Collections.unmodifiableSortedMap(TreeMapConverter.getInstance().convert(value, context)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java new file mode 100644 index 0000000..5ea2a14 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Collections; +import java.util.SortedSet; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class SortedSetConverter implements PropertyConverter<SortedSet> { + + @Override + public SortedSet convert(String value, ConversionContext context) { + return Collections.unmodifiableSortedSet(TreeSetConverter.getInstance().convert(value, context)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java b/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java new file mode 100644 index 0000000..67391ab --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.List; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating HashMap representation of a values. + */ +public class TreeMapConverter implements PropertyConverter<TreeMap> { + /** Logger used. */ + private static final Logger LOG = Logger.getLogger(HashMapConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final TreeMapConverter INSTANCE = new TreeMapConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static TreeMapConverter getInstance(){ + return INSTANCE; + } + + @Override + public TreeMap convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + TreeMap result = new TreeMap(); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.put(items[0], convValue); + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java ---------------------------------------------------------------------- diff --git a/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java b/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java new file mode 100644 index 0000000..1b049c2 --- /dev/null +++ b/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.List; +import java.util.TreeSet; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating HashSet representation of a values. + */ +public class TreeSetConverter implements PropertyConverter<TreeSet> { + + private static final Logger LOG = Logger.getLogger(TreeSetConverter.class.getName()); + + /** The shared instance, used by other collection converters in this package.*/ + private static final TreeSetConverter INSTANCE = new TreeSetConverter(); + + /** + * Provide a shared instance, used by other collection converters in this package. + * @return the shared instance, never null. + */ + static TreeSetConverter getInstance(){ + return INSTANCE; + } + + @Override + public TreeSet convert(String value, ConversionContext context) { + List<String> rawList = ItemTokenizer.split(value, context); + TreeSet<Object> result = new TreeSet<>(); + for(String raw:rawList){ + String[] items = ItemTokenizer.splitMapEntry(raw, context); + Object convValue = ItemTokenizer.convertValue(items[1], context); + if(convValue!=null){ + result.add(convValue); + continue; + }else{ + LOG.log(Level.SEVERE, "Failed to convert collection value type for '"+raw+"'."); + } + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter new file mode 100644 index 0000000..167bc27 --- /dev/null +++ b/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -0,0 +1,31 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.collections.ArrayListConverter +org.apache.tamaya.collections.CollectionConverter +org.apache.tamaya.collections.HashMapConverter +org.apache.tamaya.collections.ConcurrentHashMapConverter +org.apache.tamaya.collections.HashSetConverter +org.apache.tamaya.collections.LinkedListConverter +org.apache.tamaya.collections.ListConverter +org.apache.tamaya.collections.MapConverter +org.apache.tamaya.collections.SetConverter +org.apache.tamaya.collections.SortedSetConverter +org.apache.tamaya.collections.SortedMapConverter +org.apache.tamaya.collections.TreeMapConverter +org.apache.tamaya.collections.TreeSetConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyValueCombinationPolicy ---------------------------------------------------------------------- diff --git a/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyValueCombinationPolicy b/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyValueCombinationPolicy new file mode 100644 index 0000000..277c753 --- /dev/null +++ b/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyValueCombinationPolicy @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy current the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +org.apache.tamaya.collections.AdaptiveCombinationPolicy \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java ---------------------------------------------------------------------- diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java b/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java new file mode 100644 index 0000000..43f1f4f --- /dev/null +++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.util.Currency; +import java.util.List; +import java.util.Map; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * Created by atsticks on 16.02.16. + */ +public class CollectionAdvancedTests { + + /** + * Tests if a custom separator works, Config is + * <pre> + * sep-list=a,b,c|d,e,f|g,h,i + * _sep-list.collection-type=List + * _sep-list.collection-separator=| + * </pre> + */ + @Test + public void testCustomSeparator(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = config.get("sep-list", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("a,b,c", items.get(0)); + assertEquals("d,e,f", items.get(1)); + assertEquals("g,h,i", items.get(2)); + } + + /** + * Test typed content. + * <pre> + * currency-list=CHF,USD,YEN + * _currency-list.collection-type=List + * </pre> + */ + @Test + public void testTypedContent(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<Currency> items = config.get("currency-list", new TypeLiteral<List<Currency>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("CHF", items.get(0).getCurrencyCode()); + assertEquals("USD", items.get(1).getCurrencyCode()); + assertEquals("USS", items.get(2).getCurrencyCode()); + } + + /** + * Tests if a custom parser works, Config is + * <pre> + * parser-list=a,b,c + * _parser-list.collection-type=List + * _parser-list.item-converter=org.apache.tamaya.collections.MyUpperCaseConverter + * </pre> + */ + @Test + public void testCustomParser(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = config.get("parser-list", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("(A)", items.get(0)); + assertEquals("(B)", items.get(1)); + assertEquals("(C)", items.get(2)); + } + + /** + * Redefined map format parsing, Config is as follows: + * <pre> + * redefined-map=0==none | 1==single | 2==any + * _redefined-map.map-entry-separator=== + * _redefined-map.item-separator=| + * </pre> + */ + @Test + public void testCustomMapParser(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("redefined-map", Map.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(3, items.size()); + assertEquals("none", items.get("0")); + assertEquals("single", items.get("1")); + assertEquals("any", items.get("2")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java ---------------------------------------------------------------------- diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java b/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java new file mode 100644 index 0000000..34c82cb --- /dev/null +++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java @@ -0,0 +1,227 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.util.*; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * Basic tests for Tamaya collection support. Relevant configs for this tests: + * <pre>base.items=1,2,3,4,5,6,7,8,9,0 + * base.map=1::a, 2::b, 3::c, [4:: ] + * </pre> + */ +public class CollectionsBaseTests { + + @Test + public void testList_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = config.get("base.items", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (List<String>) config.get("base.items", List.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testArrayList_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + ArrayList<String> items = config.get("base.items", new TypeLiteral<ArrayList<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (ArrayList<String>) config.get("base.items", ArrayList.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testLinkedList_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + LinkedList<String> items = config.get("base.items", new TypeLiteral<LinkedList<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (LinkedList<String>) config.get("base.items", LinkedList.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testSet_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = config.get("base.items", new TypeLiteral<Set<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (Set<String>) config.get("base.items", Set.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testSortedSet_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = config.get("base.items", new TypeLiteral<SortedSet<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (SortedSet<String>) config.get("base.items", SortedSet.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testHashSet_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = config.get("base.items", new TypeLiteral<HashSet<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (HashSet<String>) config.get("base.items", HashSet.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testTreeSet_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + TreeSet<String> items = config.get("base.items", new TypeLiteral<TreeSet<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (TreeSet<String>) config.get("base.items", TreeSet.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } + + @Test + public void testMap_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("base.map", new TypeLiteral<Map<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items = (Map<String,String>) config.get("base.map", Map.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + } + + @Test + public void testHashMap_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("base.map", new TypeLiteral<HashMap<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items = (HashMap<String,String>) config.get("base.map", HashMap.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + } + + @Test + public void testSortedMap_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("base.map", new TypeLiteral<SortedMap<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items = (Map<String,String>) config.get("base.map", SortedMap.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + } + + @Test + public void testTreeMap_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + TreeMap<String,String> items = config.get("base.map", new TypeLiteral<TreeMap<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items = config.get("base.map", TreeMap.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + } + + @Test + public void testCollection_String(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Collection<String> items = config.get("base.items", new TypeLiteral<Collection<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items = (Collection<String>) config.get("base.items", Collection.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/4c259384/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java ---------------------------------------------------------------------- diff --git a/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java new file mode 100644 index 0000000..7882512 --- /dev/null +++ b/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.collections; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.util.*; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.*; + +/** + * Basic tests for Tamaya collection support. Relevant configs for this tests: + * <pre>base.items=1,2,3,4,5,6,7,8,9,0 + * base.map=1::a, 2::b, 3::c, [4:: ] + * </pre> + */ +public class CollectionsTypedReadOnlyTests { + + @Test(expected=UnsupportedOperationException.class) + public void testArrayListList_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = config.get("typed.arraylist", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test(expected=UnsupportedOperationException.class) + public void testArrayListList_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = (List<String>) config.get("typed.arraylist", List.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test(expected=UnsupportedOperationException.class) + public void testLinkedListList_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = config.get("typed.linkedlist", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test(expected=UnsupportedOperationException.class) + public void testLinkedListList_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + List<String> items = (List<String>) config.get("typed.linkedlist", List.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + + @Test(expected=UnsupportedOperationException.class) + public void testHashSet_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = config.get("typed.hashset", new TypeLiteral<Set<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + @Test(expected=UnsupportedOperationException.class) + public void testHashSet_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = (Set<String>) config.get("typed.hashset", Set.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test(expected=UnsupportedOperationException.class) + public void testTreeSet_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = config.get("typed.treeset", new TypeLiteral<Set<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + @Test(expected=UnsupportedOperationException.class) + public void testTreeSet_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Set<String> items = items = (Set<String>) config.get("typed.treeset", Set.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test(expected=UnsupportedOperationException.class) + public void testHashMap_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("typed.hashmap", new TypeLiteral<Map<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items.put("g","hjhhj"); + } + @Test(expected=UnsupportedOperationException.class) + public void testHashMap_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = (Map<String,String>) config.get("typed.hashmap", Map.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items.put("g","hjhhj"); + } + + + @Test(expected=UnsupportedOperationException.class) + public void testTreeMap_1(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = config.get("typed.treemap", new TypeLiteral<Map<String,String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items.put("g","hjhhj"); + } + @Test(expected=UnsupportedOperationException.class) + public void testTreeMap_2(){ + Configuration config = ConfigurationProvider.getConfiguration(); + Map<String,String> items = (Map<String,String>) config.get("typed.treemap", Map.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(4, items.size()); + assertEquals("a", items.get("1")); + assertEquals("b", items.get("2")); + assertEquals("c", items.get("3")); + assertEquals(" ", items.get("4")); + items.put("g","hjhhj"); + } + +}
