-
Notifications
You must be signed in to change notification settings - Fork 17
/
PersonImpl.java
144 lines (126 loc) · 3.73 KB
/
PersonImpl.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.neo4j.examples.social.impl;
import static org.neo4j.examples.social.SocialNetworkRelationshipTypes.FRIENDS;
import static org.neo4j.examples.social.SocialNetworkRelationshipTypes.INTERESTED_IN;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.neo4j.examples.social.domain.FriendPath;
import org.neo4j.examples.social.domain.Person;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
class PersonImpl implements Person
{
static final String PERSON_NAME = "name";
private final Node underlyingNode;
private final Neo4jSocialNetwork socnet;
PersonImpl( Neo4jSocialNetwork socnet, Node underlyingNode )
{
this.socnet = socnet;
this.underlyingNode = underlyingNode;
}
@Override
public int hashCode()
{
return underlyingNode.hashCode();
}
@Override
public boolean equals( Object obj )
{
return obj instanceof PersonImpl
&& underlyingNode.equals( ( (PersonImpl) obj ).underlyingNode );
}
@Override
public String toString()
{
return "Person[" + getName() + "]";
}
public String getName()
{
return getName( underlyingNode );
}
static String getName( Node person )
{
return (String) person.getProperty( PERSON_NAME );
}
public void addFriend( Person friend )
{
Node friendNode = underlyingNodeOf( friend );
Transaction tx = socnet.beginTx();
try
{
if ( createSingleRelationship( friendNode, FRIENDS ) )
{
tx.success();
}
}
finally
{
tx.finish();
}
}
public void addInterest( String interest )
{
Transaction tx = socnet.beginTx();
try
{
Node interestNode = socnet.interestNode( interest );
if ( createSingleRelationship( interestNode, INTERESTED_IN ) )
{
tx.success();
}
}
finally
{
tx.finish();
}
}
public Collection<String> getInterests()
{
List<String> interests = new LinkedList<String>();
for ( Relationship interest : underlyingNode.getRelationships( INTERESTED_IN ) )
{
interests.add( (String) interest.getEndNode().getProperty( Neo4jSocialNetwork.INTEREST ) );
}
return Collections.unmodifiableCollection( interests );
}
public Collection<Person> getFriends()
{
// TODO: implement this in Step four
return Collections.emptySet();
}
public FriendPath getPath( Person other )
{
// TODO: implement this in Step five
return null;
}
public Collection<Person> suggestFriends()
{
// TODO: implement this in Step six
return Collections.emptyList();
}
private Node underlyingNodeOf( Person friend )
{
if ( friend instanceof PersonImpl )
{
return ( (PersonImpl) friend ).underlyingNode;
}
else
{
throw new IllegalArgumentException(
friend + " does not belong to the same social network as " + this );
}
}
private boolean createSingleRelationship( Node other, RelationshipType type )
{
Relationship newRel = underlyingNode.createRelationshipTo( other, type );
for ( Relationship rel : underlyingNode.getRelationships( type ) )
{
if ( rel == newRel ) continue;
if ( rel.getOtherNode( underlyingNode ).equals( other ) ) return false;
}
return true;
}
}