1 package org.apache.archiva.configuration.util;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.function.BiFunction;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
30 * Helper class that can be used for mapping configuration keys (e.g. user configuration keys) to
31 * archiva configuration objects.
33 * @param <T> The class used to retrieve the attribute data
34 * @param <K> The class used to retrieve the data that is for prefix matching
35 * @author Martin Stockhammer <martin_s@apache.org>
38 public class ConfigMapper<T, K>
40 private final Map<String, Function<T, String>> stringFunctionMap = new HashMap<>( );
41 private final Map<String, Function<T, Integer>> intFunctionMap = new HashMap<>( );
42 private final Map<String, Function<T, Boolean>> booleanFunctionMap = new HashMap<>( );
43 private final Map<String, BiFunction<String, K, String>> prefixStringFunctionMap = new HashMap<>( );
45 public void addStringMapping( String attributeName, Function<T, String> mapping) {
46 this.stringFunctionMap.put( attributeName, mapping );
49 public void addPrefixStringMapping(String prefix, BiFunction<String, K, String> mapping) {
50 prefixStringFunctionMap.put( prefix, mapping );
53 public String getString( String attributeName, T instance) {
54 return stringFunctionMap.get( attributeName ).apply( instance );
57 public String getPrefixString(String attributeName, K instance) {
58 BiFunction<String, K, String> function = prefixStringFunctionMap.entrySet( ).stream( ).filter( entry -> attributeName.startsWith( entry.getKey( ) ) ).findFirst( )
59 .map( entry -> entry.getValue( ) )
61 return function.apply( attributeName, instance );
64 public boolean isStringMapping(String attributeName) {
65 return stringFunctionMap.containsKey( attributeName );
68 public boolean isIntMapping(String attributeName) {
69 return intFunctionMap.containsKey( attributeName );
72 public boolean isBooleanMapping(String attributeName) {
73 return booleanFunctionMap.containsKey( attributeName );
76 public boolean isPrefixMapping(String attributeName) {
77 return prefixStringFunctionMap.keySet( ).stream( ).anyMatch( prefix -> attributeName.startsWith( prefix ) );
80 public boolean isMapping(String attributeName) {
81 return isStringMapping( attributeName ) || isIntMapping( attributeName ) || isBooleanMapping( attributeName );
84 public void addIntMapping( String attributeName, Function<T, Integer> mapping) {
85 this.intFunctionMap.put( attributeName, mapping );
88 public int getInt( String attributeName, T instance) {
89 return this.intFunctionMap.get( attributeName ).apply( instance );
92 public void addBooleanMapping( String attributeName, Function<T, Boolean> mapping) {
93 this.booleanFunctionMap.put( attributeName, mapping );
96 public boolean getBoolean( String attributeName, T instance) {
97 return this.booleanFunctionMap.get( attributeName ).apply( instance );
100 public List<String> getStringAttributes() {
101 return new ArrayList<>( stringFunctionMap.keySet( ) );
104 public List<String> getIntAttributes() {
105 return new ArrayList<>( intFunctionMap.keySet( ) );
108 public List<String> getBooleanAttributes() {
109 return new ArrayList<>( booleanFunctionMap.keySet( ) );
112 public List<String> getAllAttributes() {
113 return Arrays.asList( stringFunctionMap,intFunctionMap, booleanFunctionMap).stream()
114 .flatMap( map->map.keySet().stream() ).collect( Collectors.toList());