001 /*
002 * Java Genetic Algorithm Library (jenetics-5.1.0).
003 * Copyright (c) 2007-2019 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.rewriting;
021
022 import static java.util.Collections.unmodifiableMap;
023 import static java.util.Objects.requireNonNull;
024 import static io.jenetics.internal.util.Hashes.hash;
025
026 import java.util.Map;
027
028 import io.jenetics.ext.rewriting.TreePattern.Var;
029 import io.jenetics.ext.util.Tree;
030
031 /**
032 * The result of a tree match operation. It contains the matching tree and the
033 * tree variables which matches the matching tree.
034 *
035 * <pre>{@code
036 * final Tree<String, ?> tree = ...;
037 * final TreePattern<String> pattern = ...;
038 * final Optional<TreeMatchResult<String>> result = pattern.match(tree);
039 * result.ifPresent(r -> {assert r.tree() == tree;});
040 * }</pre>
041 *
042 * @see TreePattern#match(Tree)
043 *
044 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
045 * @version 5.0
046 * @since 5.0
047 */
048 public final class TreeMatchResult<V> {
049
050 private final Tree<V, ?> _tree;
051 private final Map<Var<V>, Tree<V, ?>> _vars;
052
053 private TreeMatchResult(
054 final Tree<V, ?> tree,
055 final Map<Var<V>, Tree<V, ?>> vars
056 ) {
057 _tree = requireNonNull(tree);
058 _vars = unmodifiableMap(requireNonNull(vars));
059 }
060
061 /**
062 * The node (tree), which has been matched by some pattern. This tree is the
063 * argument of the {@link TreePattern#match(Tree)} call, in the case of a
064 * match.
065 *
066 * <pre>{@code
067 * final Tree<String, ?> tree = ...;
068 * final TreePattern<String> pattern = ...;
069 * final Optional<TreeMatchResult<String>> result = pattern.match(tree);
070 * result.ifPresent(r -> {assert r.tree() == tree;});
071 * }</pre>
072 *
073 * @return node (tree), which has been matched by some pattern
074 */
075 public Tree<V, ?> tree() {
076 return _tree;
077 }
078
079 /**
080 * The variables involved while matching the tree {@link #tree()}.
081 *
082 * @return variables involved while matching the tree {@link #tree()}.
083 */
084 public Map<Var<V>, Tree<V, ?>> vars() {
085 return _vars;
086 }
087
088 @Override
089 public int hashCode() {
090 return hash(_tree, hash(_vars));
091 }
092
093 @Override
094 public boolean equals(final Object obj) {
095 return obj == this ||
096 obj instanceof TreeMatchResult &&
097 _tree.equals(((TreeMatchResult)obj)._tree) &&
098 _vars.equals(((TreeMatchResult)obj)._vars);
099 }
100
101 @Override
102 public String toString() {
103 return _tree.toParenthesesString();
104 }
105
106 static <V> TreeMatchResult<V> of(
107 final Tree<V, ?> tree,
108 final Map<Var<V>, Tree<V, ?>> vars
109 ) {
110 return new TreeMatchResult<>(tree, vars);
111 }
112
113 }
|