EgorBaranovEnjoysTyping commented on code in PR #13262: URL: https://github.com/apache/ignite/pull/13262#discussion_r3481746372
########## modules/commons/src/main/java/org/apache/ignite/internal/util/tostring/GridToStringNode.java: ########## @@ -0,0 +1,195 @@ +/* + * 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.ignite.internal.util.tostring; + +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.internal.util.GridStringBuilder; + +import static org.apache.ignite.internal.util.tostring.NodeRecursionMonitor.OBJECT_REGISTRY; + +/** + * The abstract base class for all nodes in the string representation tree. + * Defines the common interface for appending a node's value to a string builder + * and provides static factory and utility methods for all subclasses. + */ +public abstract class GridToStringNode { + /** + * A thread-local cache for nodes, used to handle references of + * inner toString() calls by mapping temporary markers to actual nodes. + */ + static final ConcurrentHashMap<Thread, IdentityHashMap<String, GridToStringNode>> CATCHED_NODES + = new ConcurrentHashMap<>(); + + /** Last constructed node. */ + static final ThreadLocal<GridToStringNode> LAST_CONSTRUCTED_GRID_TO_STRING_NODE = new ThreadLocal<>(); + + /** The name of the property this node represents. */ + String propName; + + /** Inner buffer. For inner calls. */ + StringBuilder innerBuf = new StringBuilder(0); + + /** + * Base constructor. + * @param propName The name of the property. + */ + GridToStringNode(String propName) { + this.propName = propName; + } + + /** + * Creates a root node for a string value. + * @param str The string value. + * @param addNodes Additional child nodes to include. + * @return A new root node. + */ + static GridToStringNode getRootNode(String str, List<GridToStringNode> addNodes) { + return new GridToStringObjectNode(str, addNodes); + } + + /** + * Creates a root node for an object. + * Checks for recursion and creates either a termination node or a full object node. + * @param obj The object to represent. + * @param cls The class of the object. + * @param addNodes Additional child nodes to include. + * @return A new root node. + */ + static GridToStringNode getRootNode(Object obj, Class<?> cls, List<GridToStringNode> addNodes) { + return recursionTermination(obj) + .orElseGet(() -> new GridToStringObjectNode(null, obj, cls, addNodes)); + } + + /** + * Marks a node in the thread-local cache with a unique, empty string key. + * Used to handle references during object graph traversal. + * @param node The node to mark. + * @return The unique marker string. + */ + static String markNode(GridToStringNode node) { + String result = new String(GridToStringNode.class.getSimpleName()); Review Comment: 1) new String will recover new Object, toString may return value from String pool 2) It's assertion, that init run. -- 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]
