Moved collections, consul, etcd and hazelcast modules into extensions. Updated docs to match code.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/c8ba9c4c Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/c8ba9c4c Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/c8ba9c4c Branch: refs/heads/master Commit: c8ba9c4c7b3c5d29d4b39bfef81f0f26e7ecf08a Parents: 4932c0c Author: Anatole Tresch <[email protected]> Authored: Thu Nov 8 00:26:26 2018 +0100 Committer: Anatole Tresch <[email protected]> Committed: Thu Nov 8 00:26:26 2018 +0100 ---------------------------------------------------------------------- modules/collections/bnd.bnd | 27 + modules/collections/pom.xml | 62 +++ .../tamaya/collections/ArrayListConverter.java | 50 ++ .../tamaya/collections/CollectionConverter.java | 317 +++++++++++ .../collections/ConcurrentHashMapConverter.java | 53 ++ .../tamaya/collections/HashMapConverter.java | 50 ++ .../tamaya/collections/HashSetConverter.java | 52 ++ .../tamaya/collections/ItemTokenizer.java | 166 ++++++ .../tamaya/collections/LinkedListConverter.java | 52 ++ .../tamaya/collections/ListConverter.java | 60 +++ .../apache/tamaya/collections/MapConverter.java | 63 +++ .../apache/tamaya/collections/SetConverter.java | 59 +++ .../tamaya/collections/SortedMapConverter.java | 36 ++ .../tamaya/collections/SortedSetConverter.java | 37 ++ .../tamaya/collections/TreeMapConverter.java | 52 ++ .../tamaya/collections/TreeSetConverter.java | 52 ++ .../org.apache.tamaya.spi.PropertyConverter | 31 ++ .../collections/CollectionAdvancedTests.java | 116 +++++ .../collections/CollectionsBaseTests.java | 226 ++++++++ .../CollectionsTypedReadOnlyTests.java | 174 +++++++ .../collections/CollectionsTypedTests.java | 207 ++++++++ .../collections/MyUpperCaseConverter.java | 33 ++ .../META-INF/javaconfiguration.properties | 69 +++ modules/consul/bnd.bnd | 32 ++ modules/consul/pom.xml | 83 +++ .../consul/AbstractConsulPropertySource.java | 246 +++++++++ .../tamaya/consul/ConsulBackendConfig.java | 85 +++ .../tamaya/consul/ConsulPropertySource.java | 66 +++ .../tamaya/consul/ConsulPropertySourceTest.java | 72 +++ .../apache/tamaya/consul/ConsulWriteTest.java | 86 +++ modules/etcd/bnd.bnd | 32 ++ modules/etcd/pom.xml | 80 +++ .../tamaya/etcd/AbstractEtcdPropertySource.java | 267 ++++++++++ .../org/apache/tamaya/etcd/EtcdAccessor.java | 519 +++++++++++++++++++ .../apache/tamaya/etcd/EtcdBackendConfig.java | 95 ++++ .../apache/tamaya/etcd/EtcdPropertySource.java | 52 ++ .../apache/tamaya/etcd/EtcdAccessorTest.java | 116 +++++ .../tamaya/etcd/EtcdPropertySourceTest.java | 75 +++ modules/hazelcast/bnd.bnd | 29 ++ modules/hazelcast/pom.xml | 77 +++ .../AbstractHazelcastPropertySource.java | 238 +++++++++ .../hazelcast/HazelcastPropertySource.java | 119 +++++ .../hazelcast/AbstractHzPropertySourceTest.java | 207 ++++++++ .../apache/tamaya/hazelcast/HazelcastUtil.java | 59 +++ .../hazelcast/HazelcaszPropertySourceTest.java | 204 ++++++++ modules/pom.xml | 4 + 46 files changed, 4887 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/bnd.bnd ---------------------------------------------------------------------- diff --git a/modules/collections/bnd.bnd b/modules/collections/bnd.bnd new file mode 100644 index 0000000..61b2571 --- /dev/null +++ b/modules/collections/bnd.bnd @@ -0,0 +1,27 @@ +-buildpath: \ + osgi.annotation; version=6.0.0,\ + osgi.core; version=6.0,\ + osgi.cmpn; version=6.0 + +-testpath: \ + ${junit} + +javac.source: 1.8 +javac.target: 1.8 + +Automatic-Module-Name: org.apache.tamaya.collections +Bundle-Version: ${version}.${tstamp} +Bundle-Name: Apache Tamaya - Collections Support +Bundle-SymbolicName: org.apache.tamaya.collections +Bundle-Description: Apacha Tamaya Config - Collections Support +Bundle-Category: Implementation +Bundle-Copyright: (C) Apache Foundation +Bundle-License: Apache Licence version 2 +Bundle-Vendor: Apache Software Foundation +Bundle-ContactAddress: [email protected] +Bundle-DocURL: http://tamaya.apache.org +Export-Package: \ + org.apache.tamaya.collections +Import-Package: \ + org.apache.tamaya,\ + org.apache.tamaya.spi http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/pom.xml ---------------------------------------------------------------------- diff --git a/modules/collections/pom.xml b/modules/collections/pom.xml new file mode 100644 index 0000000..ecc922f --- /dev/null +++ b/modules/collections/pom.xml @@ -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 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-sandbox</artifactId> + <version>0.4-incubating-SNAPSHOT</version> + </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-spisupport</artifactId> + <version>${project.version}</version> + </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> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java new file mode 100644 index 0000000..2715cfc --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/ArrayListConverter.java @@ -0,0 +1,50 @@ +/* + * 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.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) { + return CollectionConverter.convertList(context, ArrayList::new); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java new file mode 100644 index 0000000..2cadd6e --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/CollectionConverter.java @@ -0,0 +1,317 @@ +/* + * 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.TypeLiteral; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyValue; + +import java.lang.reflect.Type; +import java.util.*; +import java.util.function.Supplier; +import java.util.logging.Logger; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public final class CollectionConverter implements PropertyConverter<Collection> { + + private static final Logger LOG = Logger.getLogger(CollectionConverter.class.getName()); + public static final String VALUE_MAPPING = "collection-mapping"; + + private enum MappingType{ + /** The list values are identiified by parsing the node value(s) into items. + * Hereby only the items of the most significant config entry are considered. */ + value, + /** The list values are identiified by parsing the node value(s) into items, hereby + * the items of all values are combined. */ + value_all, + /** The list values are identiified by using the node's child value(s) as items. + * Hereby only the items of the most significant config entry are considered.*/ + node, + /** The list values are identiified by using the node's child value(s) as items. Hereby + * the items of all values are combined. */ + node_all, + /** This will guess the matching evaluation policy (value,bode) for each {@code PropertyValue} and + * combine the items of all values. */ + combine, + /** This will guess the matching evaluation policy (value,bode) on the most significant {@code PropertyValue} + * only. */ + override, + } + + private MappingType mappingType = MappingType.value_all; + + public static <T extends Collection> T convertList(ConversionContext context, + Supplier<T> collectionSupplier) { + MappingType mappingType = MappingType.valueOf((String)context.getMeta() + .getOrDefault(VALUE_MAPPING, MappingType.combine.toString())); + TypeLiteral<?> targetType = context.getTargetType(); + Type[] types = TypeLiteral.getTypeParameters(targetType.getType()); + TypeLiteral<?> collectionTargetType; + if(types.length>0) { + collectionTargetType = TypeLiteral.of(types[0]); + }else { + LOG.warning(String.format("No type information for Collection item type in '{0}', using String.", + context.getKey())); + collectionTargetType = TypeLiteral.of(String.class); + } + T result = collectionSupplier.get(); + switch (mappingType) { + case node_all: + return convertListByNodes(context.getValues(), context, + collectionTargetType, result, true); + case node: + return convertListByNodes(context.getValues(), context, + collectionTargetType, result, false); + case value: + return convertListByValues(context.getValues(), context, + collectionTargetType, result, false); + case value_all: + return convertListByValues(context.getValues(), context, + collectionTargetType, result, true); + case override: + return convertListWithBestGuess(context.getValues(), context, + collectionTargetType, result,false); + case combine: + default: + return convertListWithBestGuess(context.getValues(), context, + collectionTargetType, result,true); + } + } + + private static <T extends Collection> T convertListWithBestGuess(List<PropertyValue> values, + ConversionContext context, + TypeLiteral<?> targetType, + T result, + boolean combine) { + if(!combine){ + values = Collections.singletonList(values.get(0)); + LOG.finest(String.format("Combine deactivated, only checking for collection values in {0}.", values.get(0))); + }else{ + LOG.finest(String.format("Combine activated, checking for collection values in {0}.", values)); + } + // First: try value based approach + for (PropertyValue val : values) { + int valuesFound = 0; + List<String> tokenList = ItemTokenizer.split(val.getValue(), context); + for (String token : tokenList) { + Object o = ItemTokenizer.convertValue(token, targetType, context); + if (o != null) { + valuesFound++; + result.add(o); + } + } + if(valuesFound==0) { + LOG.finest(() -> String.format("No values found in {0} using value evaluation, checking for child nodes...", val)); + for(PropertyValue itemNode:val) { + String textValue = itemNode.getValue(); + if(textValue!=null) { + if (targetType.equals(TypeLiteral.of(String.class))) { + valuesFound++; + result.add(textValue); + } else { + Object o = ItemTokenizer.convertValue(itemNode.getValue(), targetType, context); + if (o != null) { + valuesFound++; + result.add(o); + } + } + } + } + } + if(valuesFound==0){ + LOG.warning(String.format("Failed to convert key '{0}' to type: {1}: no values found.", + val.getKey(), targetType)); + }else{ + LOG.finest(String.format("Found {2} collection values for key '{0}' with type: {1}: no values found.", + val.getKey(), targetType, valuesFound)); + } + } + + return result; + } + + private static <T extends Collection> T convertListByValues(List<PropertyValue> values, + ConversionContext context, + TypeLiteral<?> targetType, + T result, + boolean combine) { + if(!combine){ + values = Collections.singletonList(values.get(0)); + LOG.finest(String.format("Combine deactivated, only checking for collection values in {0}.", values.get(0))); + }else{ + LOG.finest(String.format("Combine activated, checking for collection values in {0}.", values)); + } + for (PropertyValue val : values) { + List<String> tokenList = ItemTokenizer.split(val.getValue(), context); + for (String token : tokenList) { + Object o = ItemTokenizer.convertValue(token, targetType, context); + if (o != null) { + result.add(o); + }else{ + LOG.warning(String.format("Failed to convert '{0}' to type: {1}, key: {2}", + token, targetType, val.getQualifiedKey())); + } + } + } + return result; + } + + private static <T extends Collection> T convertListByNodes(List<PropertyValue> values, + ConversionContext context, + TypeLiteral<?> targetType, + T result, + boolean combine) { + if(!combine){ + values = Collections.singletonList(values.get(0)); + } + for (PropertyValue val : values) { + for(PropertyValue itemNode:val) { + if(targetType.equals(TypeLiteral.of(String.class))){ + result.add(itemNode.getValue()); + }else { + Object o = ItemTokenizer.convertValue(itemNode.getValue(), targetType, context); + if (o != null) { + result.add(o); + }else{ + LOG.warning(String.format("Failed to convert '{0}' to type: {1}, key: {2}", + itemNode.getValue(), targetType, itemNode.getQualifiedKey())); + } + } + } + } + return result; + } + + public static <T extends Map> T convertMap(ConversionContext context, + Supplier<T> collectionSupplier) { + TypeLiteral<?> targetType = context.getTargetType(); + Type[] types = TypeLiteral.getTypeParameters(targetType.getType()); + TypeLiteral<?> collectionTargetType; + if (types.length > 1) { + collectionTargetType = TypeLiteral.of(types[1]); + } else { + LOG.warning(String.format("No type information for Map parameter types in '{0}', using String.", + context.getKey())); + collectionTargetType = TypeLiteral.of(String.class); + } + MappingType mappingType = MappingType.valueOf((String) context.getMeta() + .getOrDefault("mapping", "value_all")); + T result = collectionSupplier.get(); + switch (mappingType) { + case node_all: + return convertMapByNodes(context.getValues(), context, + collectionTargetType, result, true); + case node: + return convertMapByNodes(context.getValues(), context, + collectionTargetType, result, false); + case value: + return convertMapByValues(context.getValues(), context, + collectionTargetType, result, false); + default: + case value_all: + return convertMapByValues(context.getValues(), context, + collectionTargetType, result, true); + } + } + + + private static <T extends Map> T convertMapByValues(List<PropertyValue> values, + ConversionContext context, + TypeLiteral<?> targetType, + T result, + boolean combine) { + if(!combine){ + values = Collections.singletonList(values.get(0)); + LOG.finest(String.format("Combine deactivated, only checking for collection values in {0}.", values.get(0))); + }else{ + LOG.finest(String.format("Combine activated, checking for collection values in {0}.", values)); + } + for (PropertyValue val : values) { + List<String> tokenList = ItemTokenizer.split(val.getValue(), context); + for(String token:tokenList) { + String[] keyValue = ItemTokenizer.splitMapEntry(token, context); + Object o = ItemTokenizer.convertValue(keyValue[1], targetType, context); + if (o != null) { + result.put(keyValue[0], o); + }else{ + LOG.warning(String.format("Failed to convert '{0}' to type: {1}, key: {2}", + keyValue[1], targetType, val.getQualifiedKey())); + } + } + } + return result; + } + + private static <T extends Map> T convertMapByNodes(List<PropertyValue> values, + ConversionContext context, + TypeLiteral<?> targetType, + T result, + boolean combine) { + if(!combine){ + values = Collections.singletonList(values.get(0)); + LOG.finest(String.format("Combine deactivated, only checking for collection values in {0}.", values.get(0))); + }else{ + LOG.finest(String.format("Combine activated, checking for collection values in {0}.", values)); + } + for (PropertyValue val : values) { + for(PropertyValue itemNode:val) { + Object o = ItemTokenizer.convertValue(itemNode.getValue(), targetType, context); + if (o != null) { + result.put(itemNode.getKey(), o); + }else{ + LOG.warning(String.format("Failed to convert '{0}' to type: {1}, key: {2}", + itemNode.getValue(), targetType, itemNode.getQualifiedKey())); + } + } + } + return result; + } + + + @Override + public Collection convert(String value, ConversionContext context) { + String collectionType = (String)context.getMeta().getOrDefault("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; + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java new file mode 100644 index 0000000..506ac00 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/ConcurrentHashMapConverter.java @@ -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 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.TypeLiteral; +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) { + return CollectionConverter.convertMap(context, ConcurrentHashMap::new); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java new file mode 100644 index 0000000..19ecb46 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/HashMapConverter.java @@ -0,0 +1,50 @@ +/* + * 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.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) { + return CollectionConverter.convertMap(context, HashMap::new); + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java new file mode 100644 index 0000000..47a5044 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/HashSetConverter.java @@ -0,0 +1,52 @@ +/* + * 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.TypeLiteral; +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) { + return CollectionConverter.convertList(context, HashSet::new); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java b/modules/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java new file mode 100644 index 0000000..98f5473 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/ItemTokenizer.java @@ -0,0 +1,166 @@ +/* + * 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.TypeLiteral; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.ConversionContext; + +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 createValue. + */ +final class ItemTokenizer { + + private static final Logger LOG = Logger.getLogger(ItemTokenizer.class.getName()); + + public static final String ITEM_SEPARATOR = "item-separator"; + public static final String MAP_ENTRY_SEPARATOR = "map-entry-separator"; + public static final String ITEM_CONVERTER = "item-converter"; + + /** + * Private singleton. + */ + private ItemTokenizer(){} + + /** + * Splits the given createValue using the given separator. Matcjhing is done by traversing the String createValue using + * {@code indexOf} calls, one by one. The last unresolvable item (without any next separator token) + * is added at the end of the createList. + * @param value the createValue, not null. + * @return the tokenized createValue as createList, in order of occurrence. + */ + public static List<String> split(String value, ConversionContext ctx){ + String itemSeparator = (String)ctx.getMeta().getOrDefault(ITEM_SEPARATOR, ","); + return split(value, itemSeparator); + } + + /** + * Splits the given createValue using the given separator. Matching 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); + } + if(start < value.length()){ + result.add(value.substring(start)); + } + return result; + } + + /** + * Splits the given String createValue as a map entry, splitting it into key and createValue part with the given separator. + * If the createValue cannot be split then {@code key = createValue = mapEntry} is used for further processing. key or createValue + * parts are normally trimmed, unless they are enclosed with brackets {@code []}. + * @param mapEntry the entry, not null. + * @return an array of length 2, with the trimmed and parsed key/createValue pair. + */ + public static String[] splitMapEntry(String mapEntry, ConversionContext ctx){ + String entrySeparator = (String)ctx.getMeta().getOrDefault(MAP_ENTRY_SEPARATOR, "="); + return splitMapEntry(mapEntry, entrySeparator); + } + + /** + * Splits the given String createValue as a map entry, splitting it into key and createValue part with the given separator. + * If the createValue cannot be split then {@code key = createValue = mapEntry} is used for further processing. key or createValue + * 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/createValue 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 createValue into the required collection target type, defined by the context. + * @param value the raw String createValue. + * @return the parsed createValue, or null. + */ + public static <T> T convertValue(String value, TypeLiteral<T> targetType, ConversionContext context) { + String converterClass = context.getMeta().get(ITEM_CONVERTER); + List<PropertyConverter<T>> valueConverters = new ArrayList<>(1); + if (converterClass != null) { + try { + valueConverters.add((PropertyConverter<T>) Class.forName(converterClass).getConstructor() + .newInstance()); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Error convertion config to ArrayList type.", e); + } + } + valueConverters.addAll(context.getConfiguration().getContext().getPropertyConverters(targetType)); + if (valueConverters.isEmpty()) { + if(targetType.getRawType().equals(String.class)) { + return (T)value; + } + } else { + ConversionContext newContext = new ConversionContext.Builder(context.getConfiguration(), context.getKey(), + targetType).build(); + T result = null; + for (PropertyConverter<T> conv : valueConverters) { + try { + result = conv.convert(value, newContext); + 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 createValue type for '" + value + "'."); + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java new file mode 100644 index 0000000..b769ab6 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/LinkedListConverter.java @@ -0,0 +1,52 @@ +/* + * 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.TypeLiteral; +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) { + return CollectionConverter.convertList(context, LinkedList::new); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java new file mode 100644 index 0000000..769be12 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/ListConverter.java @@ -0,0 +1,60 @@ +/* + * 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.PropertyConverter; +import org.apache.tamaya.spi.ConversionContext; + +import java.util.Collections; +import java.util.List; +import java.util.function.Supplier; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class ListConverter implements PropertyConverter<List> { + + @Override + public List convert(String value, ConversionContext context) { + String collectionType = "ArrayList"; + boolean readOnly = false; + if(context!=null) { + collectionType = (String)context.getMeta().getOrDefault("collection-type", "ArrayList"); + if (collectionType.startsWith("java.util.")) { + collectionType = collectionType.substring("java.util.".length()); + } + readOnly = Boolean.parseBoolean((String)context.getMeta().getOrDefault("read-only", "false")); + } + 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(readOnly){ + return Collections.unmodifiableList(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/MapConverter.java new file mode 100644 index 0000000..08fefff --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/MapConverter.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.PropertyConverter; +import org.apache.tamaya.spi.ConversionContext; + +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 = "HashMap"; + boolean readOnly = false; + if(context!=null) { + collectionType = (String)context.getMeta().getOrDefault("collection-type", "HashMap"); + if (collectionType.startsWith("java.util.")) { + collectionType = collectionType.substring("java.util.".length()); + } + readOnly = Boolean.parseBoolean((String)context.getMeta().getOrDefault("read-only", "false")); + } + Map result = null; + switch(collectionType){ + case "TreeMap": + case "SortedMap": + 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(readOnly){ + return Collections.unmodifiableMap(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java new file mode 100644 index 0000000..b66ef5b --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/SetConverter.java @@ -0,0 +1,59 @@ +/* + * 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.PropertyConverter; +import org.apache.tamaya.spi.ConversionContext; + +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 = "Set"; + boolean readOnly = false; + if(context!=null) { + collectionType = (String)context.getMeta().getOrDefault("collection-type", "HashSet"); + if (collectionType.startsWith("java.util.")) { + collectionType = collectionType.substring("java.util.".length()); + } + readOnly = Boolean.parseBoolean((String)context.getMeta().getOrDefault("read-only", "false")); + } + Set result; + switch(collectionType){ + case "TreeSet": + result = TreeSetConverter.getInstance().convert(value, context); + break; + case "Set": + case "HashSet": + default: + result = HashSetConverter.getInstance().convert(value, context); + break; + } + if(readOnly){ + return Collections.unmodifiableSet(result); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/SortedMapConverter.java new file mode 100644 index 0000000..5416e28 --- /dev/null +++ b/modules/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-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java new file mode 100644 index 0000000..acac96c --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/SortedSetConverter.java @@ -0,0 +1,37 @@ +/* + * 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; +import java.util.TreeSet; + +/** + * PropertyConverter for gnerating a LIST representation of values. + */ +public class SortedSetConverter implements PropertyConverter<SortedSet> { + + @Override + public SortedSet convert(String value, ConversionContext context) { + return TreeSetConverter.getInstance().convert(value, context); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java new file mode 100644 index 0000000..b287dd9 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/TreeMapConverter.java @@ -0,0 +1,52 @@ +/* + * 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.TypeLiteral; +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) { + return CollectionConverter.convertMap(context, TreeMap::new); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java b/modules/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java new file mode 100644 index 0000000..c5651f5 --- /dev/null +++ b/modules/collections/src/main/java/org/apache/tamaya/collections/TreeSetConverter.java @@ -0,0 +1,52 @@ +/* + * 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; +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) { + return CollectionConverter.convertList(context, TreeSet::new); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/c8ba9c4c/modules/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/modules/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/modules/collections/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter new file mode 100644 index 0000000..167bc27 --- /dev/null +++ b/modules/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-extensions/blob/c8ba9c4c/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java new file mode 100644 index 0000000..f9ac048 --- /dev/null +++ b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionAdvancedTests.java @@ -0,0 +1,116 @@ +/* + * 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.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-createList=a,b,c|d,e,f|g,h,i + * _sep-createList.collection-type=List + * _sep-createList.collection-separator=| + * </pre> + */ + @Test + public void testCustomSeparator(){ + Configuration config = Configuration.current(); + 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-createList=CHF,USD,YEN + * _currency-createList.collection-type=List + * </pre> + */ + @Test + public void testTypedContent(){ + Configuration config = Configuration.current(); + 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-createList=a,b,c + * _parser-createList.collection-type=List + * _parser-createList.item-converter=org.apache.tamaya.collections.MyUpperCaseConverter + * </pre> + */ + @Test + public void testCustomParser(){ + Configuration config = Configuration.current(); + 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 = Configuration.current(); + 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-extensions/blob/c8ba9c4c/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java new file mode 100644 index 0000000..8a531e3 --- /dev/null +++ b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsBaseTests.java @@ -0,0 +1,226 @@ +/* + * 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.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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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-extensions/blob/c8ba9c4c/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java ---------------------------------------------------------------------- diff --git a/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java new file mode 100644 index 0000000..848d1b3 --- /dev/null +++ b/modules/collections/src/test/java/org/apache/tamaya/collections/CollectionsTypedReadOnlyTests.java @@ -0,0 +1,174 @@ +/* + * 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.TypeLiteral; +import org.junit.Ignore; +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> + */ +//@Ignore("Readonly support has been deactivated as of now.") +public class CollectionsTypedReadOnlyTests { + + @Test(expected=UnsupportedOperationException.class) + public void testArrayListList_1(){ + Configuration config = Configuration.current(); + 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 = Configuration.current(); + List<String> items = (List<String>) config.get("typed.arraylist", List.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test + public void testLinkedListList_1(){ + Configuration config = Configuration.current(); + List<String> items = config.get("typed.linkedlist", new TypeLiteral<List<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test + public void testLinkedListList_2(){ + Configuration config = Configuration.current(); + List<String> items = (List<String>) config.get("typed.linkedlist", List.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + + @Test + public void testHashSet_1(){ + Configuration config = Configuration.current(); + Set<String> items = config.get("typed.hashset", new TypeLiteral<Set<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + @Test + public void testHashSet_2(){ + Configuration config = Configuration.current(); + Set<String> items = (Set<String>) config.get("typed.hashset", Set.class); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + + @Test + public void testTreeSet_1(){ + Configuration config = Configuration.current(); + Set<String> items = config.get("typed.treeset", new TypeLiteral<Set<String>>(){}); + assertNotNull(items); + assertFalse(items.isEmpty()); + assertEquals(10, items.size()); + items.add("test"); + } + @Test + public void testTreeSet_2(){ + Configuration config = Configuration.current(); + 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 = Configuration.current(); + 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 = Configuration.current(); + 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 + public void testTreeMap_1(){ + Configuration config = Configuration.current(); + 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 + public void testTreeMap_2(){ + Configuration config = Configuration.current(); + 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"); + } + +}
