All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jenetics.ext.rewriting.TreeMatchResult Maven / Gradle / Ivy

The newest version!
/*
 * Java Genetic Algorithm Library (jenetics-8.1.0).
 * Copyright (c) 2007-2024 Franz Wilhelmstötter
 *
 * Licensed 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.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package io.jenetics.ext.rewriting;

import static java.util.Objects.requireNonNull;
import static io.jenetics.internal.util.Hashes.hash;

import java.util.Map;

import io.jenetics.ext.rewriting.TreePattern.Var;
import io.jenetics.ext.util.Tree;

/**
 * The result of a tree match operation. It contains the matching tree and the
 * tree variables which matches the matching tree.
 *
 * {@snippet lang="java":
 * final Tree tree = null; // @replace substring='null' replacement="..."
 * final TreePattern pattern = null; // @replace substring='null' replacement="..."
 * final Optional> result = pattern.match(tree);
 * result.ifPresent(r -> {assert r.tree() == tree;});
 * }
 *
 * @see TreePattern#match(Tree)
 *
 * @author Franz Wilhelmstötter
 * @version 5.0
 * @since 5.0
 */
public final class TreeMatchResult {

	private final Tree _tree;
	private final Map, Tree> _vars;

	private TreeMatchResult(
		final Tree tree,
		final Map, Tree> vars
	) {
		_tree = requireNonNull(tree);
		_vars = Map.copyOf(vars);
	}

	/**
	 * The node (tree), which has been matched by some pattern. This tree is the
	 * argument of the {@link TreePattern#match(Tree)} call, in the case of a
	 * match.
	 *
	 * {@snippet lang="java":
	 * final Tree tree = null; // @replace substring='null' replacement="..."
	 * final TreePattern pattern = null; // @replace substring='null' replacement="..."
	 * final Optional> result = pattern.match(tree);
	 * result.ifPresent(r -> {assert r.tree() == tree;});
	 * }
	 *
	 * @return node (tree), which has been matched by some pattern
	 */
	public Tree tree() {
		return _tree;
	}

	/**
	 * The variables involved while matching the tree {@link #tree()}.
	 *
	 * @return variables involved while matching the tree {@link #tree()}.
	 */
	public Map, Tree> vars() {
		return _vars;
	}

	@Override
	public int hashCode() {
		return hash(_tree, hash(_vars));
	}

	@Override
	public boolean equals(final Object obj) {
		return obj == this ||
			obj instanceof TreeMatchResult other &&
			_tree.equals(other._tree) &&
			_vars.equals(other._vars);
	}

	@Override
	public String toString() {
		return _tree.toParenthesesString();
	}

	static  TreeMatchResult of(
		final Tree tree,
		final Map, Tree> vars
	) {
		return new TreeMatchResult<>(tree, vars);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy