/
EmbeddedAliasTree.java
84 lines (73 loc) · 2.03 KB
/
EmbeddedAliasTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.datastore.neo4j.query.parsing.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Tree structure that can be used to store alias of embedded components in Neo4j.
* <p>
* For example, a path to two properties like:
* <ol>
* <li>n.first.anotherEmbedded</li>
* <li>n.second.anotherEmbedded</li>
* </ol>
* Might be represented with the following structure:
* <pre>
* n (alias = n)|- first (alias = n_0) -- anotherEmbedded (alias = n_0_0)
* |
* -- second (alias = n_1) -- anotherEmbedded (alias = n_0_1)
* </pre>
*
* @author Davide D'Alto
*/
public class EmbeddedAliasTree {
private final String name;
private final String alias;
private final List<EmbeddedAliasTree> children;
/**
* Creates a tree node.
*
* @param alias the alias used for the embedded
* @param name the name of the property representing the emebedded
*/
public EmbeddedAliasTree(String alias, String name) {
this.name = name;
this.alias = alias;
this.children = new ArrayList<EmbeddedAliasTree>();
}
public EmbeddedAliasTree findChild(String name) {
for ( EmbeddedAliasTree child : children ) {
if ( child.getName().equals( name ) ) {
return child;
}
}
return null;
}
public String getName() {
return name;
}
public void addChild(EmbeddedAliasTree embeddedNode) {
children.add( embeddedNode );
}
public String getAlias() {
return alias;
}
public List<EmbeddedAliasTree> getChildren() {
return Collections.unmodifiableList( children );
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append( "[name=" );
builder.append( name );
builder.append( ", alias=" );
builder.append( alias );
builder.append( "]" );
return builder.toString();
}
}