88
99package com .tinkerpop .blueprints .impls .arangodb ;
1010
11- import com .tinkerpop .blueprints .impls .arangodb .client .*;
12-
1311import com .tinkerpop .blueprints .Direction ;
1412import com .tinkerpop .blueprints .Edge ;
1513import com .tinkerpop .blueprints .Vertex ;
14+ import com .tinkerpop .blueprints .impls .arangodb .client .ArangoDBException ;
15+ import com .tinkerpop .blueprints .impls .arangodb .client .ArangoDBSimpleEdge ;
1616import com .tinkerpop .blueprints .util .ExceptionFactory ;
1717import com .tinkerpop .blueprints .util .StringFactory ;
1818
1919/**
20+ * The arangodb edge class
21+ *
2022 * @author Achim Brandt (http://www.triagens.de)
2123 * @author Johannes Gocke (http://www.triagens.de)
24+ * @author Guido Schwab (http://www.triagens.de)
2225 */
2326
24- public class ArangoDBEdge extends ArangoDBElement implements Edge
25- {
27+ public class ArangoDBEdge extends ArangoDBElement implements Edge {
2628 /**
27- * the _from vertex
29+ * the _from vertex
2830 */
2931 private Vertex outVertex = null ;
30-
32+
3133 /**
32- * the _to vertex
34+ * the _to vertex
3335 */
3436 private Vertex inVertex = null ;
35-
36- static ArangoDBEdge create (ArangoDBGraph graph , Object id , Vertex outVertex , Vertex inVertex , String label ) {
37- String key = (id != null ) ? id .toString () : null ;
38-
37+
38+ static ArangoDBEdge create (ArangoDBGraph graph , Object id , Vertex outVertex , Vertex inVertex , String label ) {
39+ String key = (id != null ) ? id .toString () : null ;
40+
3941 if (outVertex instanceof ArangoDBVertex && inVertex instanceof ArangoDBVertex ) {
4042 ArangoDBVertex from = (ArangoDBVertex ) outVertex ;
4143 ArangoDBVertex to = (ArangoDBVertex ) inVertex ;
42-
44+
4345 try {
44- ArangoDBSimpleEdge v = graph .client .createEdge (graph .getRawGraph (), key , label , from .getRawVertex (), to .getRawVertex (), null );
46+ ArangoDBSimpleEdge v = graph .client .createEdge (graph .getRawGraph (), key , label , from .getRawVertex (),
47+ to .getRawVertex (), null );
4548 return build (graph , v , outVertex , inVertex );
4649 } catch (ArangoDBException e ) {
4750 if (e .errorNumber () == 1210 ) {
4851 throw ExceptionFactory .vertexWithIdAlreadyExists (id );
4952 }
5053 throw new IllegalArgumentException (e .getMessage ());
51- }
54+ }
5255 }
5356 throw new IllegalArgumentException ("Wrong vertex class." );
54-
57+
5558 }
56-
57- static ArangoDBEdge load (ArangoDBGraph graph , Object id ) {
59+
60+ static ArangoDBEdge load (ArangoDBGraph graph , Object id ) {
5861 if (id == null ) {
5962 throw ExceptionFactory .edgeIdCanNotBeNull ();
6063 }
61-
62- String key = id .toString ();
64+
65+ String key = id .toString ();
6366
6467 ArangoDBEdge edge = graph .edgeCache .get (key );
6568 if (edge != null ) {
6669 return edge ;
6770 }
68-
71+
6972 try {
7073 ArangoDBSimpleEdge v = graph .client .getEdge (graph .getRawGraph (), key );
71- return build (graph , v , null , null );
74+ return build (graph , v , null , null );
7275 } catch (ArangoDBException e ) {
7376 // do nothing
7477 return null ;
75- }
78+ }
7679 }
77-
78- static ArangoDBEdge build (ArangoDBGraph graph , ArangoDBSimpleEdge simpleEdge , Vertex outVertex , Vertex inVertex ) {
80+
81+ static ArangoDBEdge build (ArangoDBGraph graph , ArangoDBSimpleEdge simpleEdge , Vertex outVertex , Vertex inVertex ) {
7982 String id = simpleEdge .getDocumentKey ();
80-
83+
8184 ArangoDBEdge vert = graph .edgeCache .get (id );
8285 if (vert != null ) {
8386 vert .setDocument (simpleEdge );
8487 return vert ;
8588 }
86-
89+
8790 ArangoDBEdge newEdge = new ArangoDBEdge (graph , simpleEdge , outVertex , inVertex );
8891 graph .edgeCache .put (newEdge .getRaw ().getDocumentKey (), newEdge );
89- return newEdge ;
92+ return newEdge ;
9093 }
9194
9295 private ArangoDBEdge (ArangoDBGraph graph , ArangoDBSimpleEdge edge , Vertex outVertex , Vertex inVertex ) {
9396 this .graph = graph ;
9497 this .document = edge ;
9598 this .outVertex = outVertex ;
96- this .inVertex = inVertex ;
99+ this .inVertex = inVertex ;
97100 }
98-
99- public Vertex getVertex (Direction direction ) throws IllegalArgumentException
100- {
101- if (direction .equals (Direction .IN ))
102- {
103- if (inVertex == null ) {
104- Object id = document .getProperty (ArangoDBSimpleEdge ._TO );
105- inVertex = graph .getVertex (getKey (id ));
106- }
107- return inVertex ;
108- }
109- else if (direction .equals (Direction .OUT ))
110- {
111- if (outVertex == null ) {
112- Object id = document .getProperty (ArangoDBSimpleEdge ._FROM );
113- outVertex = graph .getVertex (getKey (id ));
114- }
115- return outVertex ;
116- }
117- else
118- {
119- throw ExceptionFactory .bothIsNotSupported ();
120- }
101+
102+ public Vertex getVertex (Direction direction ) throws IllegalArgumentException {
103+ if (direction .equals (Direction .IN )) {
104+ if (inVertex == null ) {
105+ Object id = document .getProperty (ArangoDBSimpleEdge ._TO );
106+ inVertex = graph .getVertex (getKey (id ));
107+ }
108+ return inVertex ;
109+ } else if (direction .equals (Direction .OUT )) {
110+ if (outVertex == null ) {
111+ Object id = document .getProperty (ArangoDBSimpleEdge ._FROM );
112+ outVertex = graph .getVertex (getKey (id ));
113+ }
114+ return outVertex ;
115+ } else {
116+ throw ExceptionFactory .bothIsNotSupported ();
117+ }
121118 }
122-
123- private String getKey (Object id ) {
124- if (id == null ) {
119+
120+ private String getKey (Object id ) {
121+ if (id == null ) {
125122 return "" ;
126123 }
127-
124+
128125 String [] parts = id .toString ().split ("/" );
129-
126+
130127 if (parts .length > 1 ) {
131128 return parts [1 ];
132129 }
133-
130+
134131 return parts [0 ];
135132 }
136133
137- public String getLabel ()
138- {
134+ public String getLabel () {
139135 Object l = document .getProperty (ArangoDBSimpleEdge ._LABEL );
140136 if (l != null ) {
141137 return l .toString ();
142138 }
143-
139+
144140 return null ;
145141 }
146-
147- public ArangoDBSimpleEdge getRawEdge () {
142+
143+ /**
144+ * Returns the arangodb raw edge
145+ *
146+ * @return a ArangoDBSimpleEdge
147+ */
148+ public ArangoDBSimpleEdge getRawEdge () {
148149 return (ArangoDBSimpleEdge ) document ;
149150 }
150-
151+
151152 @ Override
152- public String toString () {
153- return StringFactory .edgeString (this );
154- }
155-
156- public void remove () {
153+ public String toString () {
154+ return StringFactory .edgeString (this );
155+ }
156+
157+ public void remove () {
157158 if (document .isDeleted ()) {
158159 return ;
159160 }
@@ -165,12 +166,12 @@ public void remove () {
165166 }
166167 graph .edgeCache .remove (key );
167168 }
168-
169- public void save () throws ArangoDBException {
169+
170+ public void save () throws ArangoDBException {
170171 if (document .isDeleted ()) {
171172 return ;
172173 }
173174 graph .client .saveEdge (graph .getRawGraph (), (ArangoDBSimpleEdge ) document );
174175 }
175-
176+
176177}
0 commit comments