dcapwell commented on a change in pull request #1335: URL: https://github.com/apache/cassandra/pull/1335#discussion_r802071304
########## File path: src/java/org/apache/cassandra/config/Properties.java ########## @@ -0,0 +1,215 @@ +/* + * 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.cassandra.config; + +import java.lang.annotation.Annotation; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.SortedMap; +import java.util.TreeMap; + +import com.google.common.collect.Maps; + +import org.yaml.snakeyaml.introspector.Property; + +public final class Properties +{ + public static final String DELIMITER = "."; + + private Properties() + { + } + + public static Property andThen(Property root, Property leaf, String delimiter) + { + return new AndThen(root, leaf, delimiter); + } + + public static Property andThen(Property root, Property leaf) + { + return andThen(root, leaf, DELIMITER); + } + + public static Map<String, Property> flatten(Loader loader, Map<String, Property> input) + { + return flatten(loader, input, DELIMITER); + } + + public static Map<String, Property> flatten(Loader loader, Map<String, Property> input, String delimiter) + { + Queue<Property> queue = new ArrayDeque<>(); + queue.addAll(input.values()); + + Map<String, Property> output = Maps.newHashMapWithExpectedSize(input.size()); + while (!queue.isEmpty()) + { + Property prop = queue.poll(); + if (isPrimitive(prop) || isCollection(prop)) + { + output.put(prop.getName(), prop); + continue; + } + Map<String, Property> children = loader.getProperties(prop.getType()); + if (children.isEmpty()) + { + // not a primitive, but has no properties... assume can handle + output.put(prop.getName(), prop); + } + else + { + children.values().stream().map(p -> andThen(prop, p, delimiter)).forEach(queue::add); + } + } + return output; + } + + public static boolean isCollection(Property prop) + { + return Collection.class.isAssignableFrom(prop.getType()) || Map.class.isAssignableFrom(prop.getType()); + } + + public static boolean isPrimitive(Property prop) + { + Class<?> type = prop.getType(); + return type.isPrimitive() || type.isEnum() || type.equals(String.class) || Number.class.isAssignableFrom(type) || Boolean.class.equals(type); + } + + public static Loader defaultLoader() + { + return new DefaultLoader(); + } + + public static CharSequence toStringTree(Class<?> klass) + { + Loader loader = defaultLoader(); + SortedMap<String, Property> properties = new TreeMap<>(loader.getProperties(klass)); + StringBuilder sb = new StringBuilder(); + addConfigs(loader, properties, sb, 0); + return sb; + } + + public static CharSequence toStringTree(Map<String, Property> input) + { + SortedMap<String, Property> properties = new TreeMap<>(input); + StringBuilder sb = new StringBuilder(); + addConfigs(defaultLoader(), properties, sb, 0); + return sb; + } + + private static void addConfigs(Loader loader, SortedMap<String, Property> properties, StringBuilder sb, int layer) + { + for (Map.Entry<String, Property> e : properties.entrySet()) + { + for (int i = 0; i < layer; i++) + sb.append(" "); + sb.append(e.getKey()).append(":"); + Property property = e.getValue(); + Map<String, Property> subProperties = Properties.isPrimitive(property) || Properties.isCollection(property) ? Collections.emptyMap() : loader.getProperties(property.getType()); + if (subProperties.isEmpty()) + { + sb.append(" ").append(normalize(property.getType())); + sb.append("\n"); + } + else + { + sb.append(" # ").append(normalize(property.getType())).append("\n"); + addConfigs(loader, new TreeMap<>(subProperties), sb, layer + 1); + } + } + } + + private static String normalize(Class<?> type) + { + if (type.isEnum()) + return "Enum<" + type.getSimpleName() + ">"; + if (Number.class.isAssignableFrom(type)) + { + if (Integer.class.equals(type)) + return "int"; + if (Long.class.equals(type)) + return "long"; + if (Float.class.equals(type)) + return "float"; + if (Double.class.equals(type)) + return "double"; + } + return type.getSimpleName(); + } + + private static final class AndThen extends Property + { + private final Property root; + private final Property leaf; + + public AndThen(Property root, Property leaf, String delimiter) + { + super(root.getName() + delimiter + leaf.getName(), leaf.getType()); + this.root = root; + this.leaf = leaf; + } + + @Override + public Class<?>[] getActualTypeArguments() + { + return leaf.getActualTypeArguments(); + } + + @Override + public void set(Object o, Object o1) throws Exception + { + Object parent = root.get(o); + if (parent == null) + return; Review comment: should this be a NPE? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

