ParenthesesTrees.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-7.1.1).
003  * Copyright (c) 2007-2022 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.ext.util;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.util.function.Function;
025 
026 import io.jenetics.ext.internal.util.Escaper;
027 
028 /**
029  * Helper methods for creating parentheses tree strings.
030  *
031  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
032  @version 4.3
033  @since 4.3
034  */
035 final class ParenthesesTrees {
036     private ParenthesesTrees() {}
037 
038     private static final char[] PROTECTED_CHARS = '('')'',' };
039     static final char ESCAPE_CHAR = '\\';
040 
041     private static final Escaper ESCAPER = new Escaper(ESCAPE_CHAR, PROTECTED_CHARS);
042 
043     static String escape(final CharSequence value) {
044         return ESCAPER.escape(value);
045     }
046 
047     static String unescape(final CharSequence value) {
048         return ESCAPER.unescape(value);
049     }
050 
051     /* *************************************************************************
052      * To string methods.
053      **************************************************************************/
054 
055     /**
056      * Return a compact string representation of the given tree.
057      <pre>
058      *  mul(div(cos(1.0), cos(π)), sin(mul(1.0, z)))
059      </pre>
060      *
061      @param tree the input tree
062      @param mapper the string mapper function
063      @return the string representation of the given tree
064      */
065     static <V> String toString(
066         final Tree<V, ?> tree,
067         final Function<? super V, ? extends CharSequence> mapper
068     ) {
069         requireNonNull(mapper);
070 
071         if (tree != null) {
072             final StringBuilder out = new StringBuilder();
073             toString(out, tree, mapper);
074             return out.toString();
075         else {
076             return "null";
077         }
078     }
079 
080     private static <V> void toString(
081         final StringBuilder out,
082         final Tree<V, ?> tree,
083         final Function<? super V, ? extends CharSequence> mapper
084     ) {
085         out.append(escape(mapper.apply(tree.value())));
086         if (!tree.isLeaf()) {
087             out.append("(");
088             toString(out, tree.childAt(0), mapper);
089             for (int i = 1; i < tree.childCount(); ++i) {
090                 out.append(",");
091                 toString(out, tree.childAt(i), mapper);
092             }
093             out.append(")");
094         }
095     }
096 
097 }
098 
099