|
| 1 | +/* |
| 2 | + * See the NOTICE file distributed with this work for additional |
| 3 | + * information regarding copyright ownership. |
| 4 | + * |
| 5 | + * This is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU Lesser General Public License as |
| 7 | + * published by the Free Software Foundation; either version 2.1 of |
| 8 | + * the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This software is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this software; if not, write to the Free |
| 17 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 19 | + */ |
| 20 | +package org.xwiki.configuration.internal; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.LinkedHashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.xwiki.configuration.ConfigurationSource; |
| 28 | + |
| 29 | +/** |
| 30 | + * Base class for composing (aka chaining) several Configuration Sources. The order of sources is important. Sources |
| 31 | + * located before other sources take priority. |
| 32 | + * |
| 33 | + * @version $Id$ |
| 34 | + * @since 7.4M1 |
| 35 | + */ |
| 36 | +public abstract class AbstractCompositeConfigurationSource extends AbstractConfigurationSource |
| 37 | + implements Iterable<ConfigurationSource> |
| 38 | +{ |
| 39 | + @Override |
| 40 | + public boolean containsKey(String key) |
| 41 | + { |
| 42 | + boolean result = false; |
| 43 | + |
| 44 | + for (ConfigurationSource source : this) { |
| 45 | + if (source.containsKey(key)) { |
| 46 | + result = true; |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public <T> T getProperty(String key) |
| 56 | + { |
| 57 | + T result = null; |
| 58 | + |
| 59 | + for (ConfigurationSource source : this) { |
| 60 | + if (source.containsKey(key)) { |
| 61 | + result = source.<T>getProperty(key); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public <T> T getProperty(String key, Class<T> valueClass) |
| 71 | + { |
| 72 | + T result = null; |
| 73 | + |
| 74 | + for (ConfigurationSource source : this) { |
| 75 | + if (source.containsKey(key)) { |
| 76 | + result = source.getProperty(key, valueClass); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // List and Properties must return empty collections and not null values. |
| 82 | + if (result == null) { |
| 83 | + result = getDefault(valueClass); |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public <T> T getProperty(String key, T defaultValue) |
| 91 | + { |
| 92 | + T result = null; |
| 93 | + |
| 94 | + for (ConfigurationSource source : this) { |
| 95 | + if (source.containsKey(key)) { |
| 96 | + result = source.<T>getProperty(key, defaultValue); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (result == null) { |
| 102 | + result = defaultValue; |
| 103 | + } |
| 104 | + |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public List<String> getKeys() |
| 110 | + { |
| 111 | + // We use a linked hash set in order to keep the keys in the order in which they were defined in the sources. |
| 112 | + Set<String> keys = new LinkedHashSet<>(); |
| 113 | + |
| 114 | + for (ConfigurationSource source : this) { |
| 115 | + keys.addAll(source.getKeys()); |
| 116 | + } |
| 117 | + |
| 118 | + return new ArrayList<>(keys); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public List<String> getKeys(String prefix) |
| 123 | + { |
| 124 | + // We use a linked hash set in order to keep the keys in the order in which they were defined in the sources. |
| 125 | + Set<String> keys = new LinkedHashSet<>(); |
| 126 | + |
| 127 | + for (ConfigurationSource source : this) { |
| 128 | + keys.addAll(source.getKeys(prefix)); |
| 129 | + } |
| 130 | + |
| 131 | + return new ArrayList<>(keys); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean isEmpty() |
| 136 | + { |
| 137 | + for (ConfigurationSource source : this) { |
| 138 | + if (!source.isEmpty()) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean isEmpty(String prefix) |
| 148 | + { |
| 149 | + for (ConfigurationSource source : this) { |
| 150 | + if (!source.isEmpty(prefix)) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return true; |
| 156 | + } |
| 157 | +} |
0 commit comments