1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.spi.config.domain.key; 17 18 import java.io.Serializable; 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import de.smartics.properties.api.config.domain.key.ConfigurationKey; 23 import de.smartics.util.lang.Arg; 24 25 /** 26 * A collection of matchers to determine a configuration key for a properties 27 * resource. 28 */ 29 public final class PropertyResourceMatchers implements Serializable 30 { 31 // ********************************* Fields ********************************* 32 33 // --- constants ------------------------------------------------------------ 34 35 /** 36 * The class version identifier. 37 * <p> 38 * The value of this constant is {@value}. 39 * </p> 40 */ 41 private static final long serialVersionUID = 1L; 42 43 // --- members -------------------------------------------------------------- 44 45 /** 46 * The default key to match if none of the matchers can be applied. May be 47 * <code>null</code>. 48 * 49 * @serial 50 */ 51 private final ConfigurationKey<?> anyKey; 52 53 /** 54 * The matchers for mapping properties resources (file path names) to a 55 * configuration key. 56 * 57 * @serial 58 */ 59 private final List<PropertiesResourceMatcher> matchers; 60 61 // ****************************** Initializer ******************************* 62 63 // ****************************** Constructors ****************************** 64 65 /** 66 * Convenience constructor for a matcher that matches nothing. 67 */ 68 public PropertyResourceMatchers() 69 { 70 this(null, new ArrayList<PropertiesResourceMatcher>()); 71 } 72 73 /** 74 * Default constructor. 75 * 76 * @param anyKey the default key to match if none of the matchers can be 77 * applied. May be <code>null</code>. 78 * @param matchers the matchers for mapping properties resources (file path 79 * names) to a configuration key. 80 * @throws NullPointerException if {@code matchers} is <code>null</code>. 81 */ 82 public PropertyResourceMatchers(final ConfigurationKey<?> anyKey, 83 final List<PropertiesResourceMatcher> matchers) 84 throws NullPointerException 85 { 86 this.anyKey = anyKey; 87 this.matchers = Arg.checkNotNull("matchers", matchers); 88 } 89 90 // ****************************** Inner Classes ***************************** 91 92 // ********************************* Methods ******************************** 93 94 // --- init ----------------------------------------------------------------- 95 96 // --- get&set -------------------------------------------------------------- 97 98 // --- business ------------------------------------------------------------- 99 100 /** 101 * Returns the key for the given resource identifier. 102 * 103 * @param resourceId the identifier of a resource. Usually the path to a 104 * properties file within an archive. 105 * @return the configuration key the resource is associated with or the 106 * default {@code anyKey} if none is registered for the given 107 * {@code resourceId}. The default key may be <code>null</code>. 108 */ 109 public ConfigurationKey<?> getKey(final String resourceId) 110 { 111 for (final PropertiesResourceMatcher matcher : matchers) 112 { 113 if (matcher.match(resourceId)) 114 { 115 return matcher.getConfigurationKey(); 116 } 117 } 118 119 return anyKey; 120 } 121 122 // --- object basics -------------------------------------------------------- 123 124 /** 125 * Returns the string representation of the object. 126 * 127 * @return the string representation of the object. 128 */ 129 @Override 130 public String toString() 131 { 132 final StringBuilder buffer = new StringBuilder(); 133 134 if (anyKey != null) 135 { 136 buffer.append(anyKey); 137 } 138 else 139 { 140 buffer.append("-/-"); 141 } 142 buffer.append(':'); 143 144 for (final PropertiesResourceMatcher matcher : matchers) 145 { 146 buffer.append(' ').append(matcher); 147 } 148 149 return buffer.toString(); 150 } 151 }