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