org.apache.solr.util.DataConfigNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import org.apache.solr.cluster.api.SimpleMap;
import org.apache.solr.common.ConfigNode;
import org.apache.solr.common.util.PropertiesUtil;
import org.apache.solr.common.util.WrappedSimpleMap;
/** ConfigNode impl that copies and maintains data internally from DOM */
public class DataConfigNode implements ConfigNode {
public final String name;
public final SimpleMap attributes;
public final SimpleMap> kids;
public final String textData;
public DataConfigNode(ConfigNode root) {
Map> kids = new LinkedHashMap<>();
name = root.name();
attributes = wrap(root.attributes());
textData = root.txt();
root.forEachChild(
it -> {
List nodes = kids.computeIfAbsent(it.name(), k -> new ArrayList<>());
nodes.add(new DataConfigNode(it));
return Boolean.TRUE;
});
for (Map.Entry> e : kids.entrySet()) {
if (e.getValue() != null) {
e.setValue(List.copyOf(e.getValue()));
}
}
this.kids = kids.isEmpty() ? EMPTY : new WrappedSimpleMap<>(Map.copyOf(kids));
}
public String subtituteVal(String s) {
return PropertiesUtil.substitute(s, SUBSTITUTES.get());
}
private SimpleMap wrap(SimpleMap delegate) {
if (delegate.size() == 0) return delegate; // avoid unnecessary object creation
return new SimpleMap<>() {
@Override
public String get(String key) {
return subtituteVal(delegate.get(key));
}
@Override
public void forEachEntry(BiConsumer fun) {
delegate.forEachEntry((k, v) -> fun.accept(k, subtituteVal(v)));
}
@Override
public int size() {
return delegate.size();
}
};
}
@Override
public String name() {
return name;
}
@Override
public String txt() {
return subtituteVal(textData);
}
@Override
public SimpleMap attributes() {
return attributes;
}
@Override
public ConfigNode child(String name) {
List val = kids.get(name);
return val == null || val.isEmpty() ? null : val.get(0);
}
@Override
public List getAll(String name) {
return kids.get(name, Collections.emptyList());
}
@Override
public List getAll(Predicate test, Set matchNames) {
List result = new ArrayList<>();
for (String s : matchNames) {
List vals = kids.get(s);
if (vals != null) {
vals.forEach(
it -> {
if (test == null || test.test(it)) {
result.add(it);
}
});
}
}
return result;
}
@Override
public void forEachChild(Function fun) {
kids.forEachEntry(
(s, configNodes) -> {
if (configNodes != null) {
configNodes.forEach(fun::apply);
}
});
}
public static final SimpleMap> EMPTY =
new WrappedSimpleMap<>(Collections.emptyMap());
}