You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigMapper.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.apache.archiva.configuration.provider.util;
  2. /*
  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
  10. *
  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
  17. * under the License.
  18. */
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.function.BiFunction;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27. /**
  28. * Helper class that can be used for mapping configuration keys (e.g. user configuration keys) to
  29. * archiva configuration objects.
  30. *
  31. * @param <T> The class used to retrieve the attribute data
  32. * @param <K> The class used to retrieve the data that is for prefix matching
  33. * @author Martin Stockhammer <martin_s@apache.org>
  34. * @since 3.0
  35. */
  36. public class ConfigMapper<T, K>
  37. {
  38. private final Map<String, Function<T, String>> stringFunctionMap = new HashMap<>( );
  39. private final Map<String, Function<T, Integer>> intFunctionMap = new HashMap<>( );
  40. private final Map<String, Function<T, Boolean>> booleanFunctionMap = new HashMap<>( );
  41. private final Map<String, BiFunction<String, K, String>> prefixStringFunctionMap = new HashMap<>( );
  42. public void addStringMapping( String attributeName, Function<T, String> mapping) {
  43. this.stringFunctionMap.put( attributeName, mapping );
  44. }
  45. public void addPrefixStringMapping(String prefix, BiFunction<String, K, String> mapping) {
  46. prefixStringFunctionMap.put( prefix, mapping );
  47. }
  48. public String getString( String attributeName, T instance) {
  49. return stringFunctionMap.get( attributeName ).apply( instance );
  50. }
  51. public String getPrefixString(String attributeName, K instance) {
  52. BiFunction<String, K, String> function = prefixStringFunctionMap.entrySet( ).stream( ).filter( entry -> attributeName.startsWith( entry.getKey( ) ) ).findFirst( )
  53. .map( entry -> entry.getValue( ) )
  54. .get( );
  55. return function.apply( attributeName, instance );
  56. }
  57. public boolean isStringMapping(String attributeName) {
  58. return stringFunctionMap.containsKey( attributeName );
  59. }
  60. public boolean isIntMapping(String attributeName) {
  61. return intFunctionMap.containsKey( attributeName );
  62. }
  63. public boolean isBooleanMapping(String attributeName) {
  64. return booleanFunctionMap.containsKey( attributeName );
  65. }
  66. public boolean isPrefixMapping(String attributeName) {
  67. return prefixStringFunctionMap.keySet( ).stream( ).anyMatch( prefix -> attributeName.startsWith( prefix ) );
  68. }
  69. public boolean isMapping(String attributeName) {
  70. return isStringMapping( attributeName ) || isIntMapping( attributeName ) || isBooleanMapping( attributeName );
  71. }
  72. public void addIntMapping( String attributeName, Function<T, Integer> mapping) {
  73. this.intFunctionMap.put( attributeName, mapping );
  74. }
  75. public int getInt( String attributeName, T instance) {
  76. return this.intFunctionMap.get( attributeName ).apply( instance );
  77. }
  78. public void addBooleanMapping( String attributeName, Function<T, Boolean> mapping) {
  79. this.booleanFunctionMap.put( attributeName, mapping );
  80. }
  81. public boolean getBoolean( String attributeName, T instance) {
  82. return this.booleanFunctionMap.get( attributeName ).apply( instance );
  83. }
  84. public List<String> getStringAttributes() {
  85. return new ArrayList<>( stringFunctionMap.keySet( ) );
  86. }
  87. public List<String> getIntAttributes() {
  88. return new ArrayList<>( intFunctionMap.keySet( ) );
  89. }
  90. public List<String> getBooleanAttributes() {
  91. return new ArrayList<>( booleanFunctionMap.keySet( ) );
  92. }
  93. public List<String> getAllAttributes() {
  94. return Arrays.asList( stringFunctionMap,intFunctionMap, booleanFunctionMap).stream()
  95. .flatMap( map->map.keySet().stream() ).collect( Collectors.toList());
  96. }
  97. }