Skip to content

Commit 6a80ae7

Browse files
authored
Merge pull request #64 from antonsarov/3.1
fixes #63; better toString representation of ArangoHost
2 parents ad9e507 + 61dea18 commit 6a80ae7

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/java/com/arangodb/ArangoHost.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,20 @@ public boolean equals(Object obj) {
6161
return true;
6262
}
6363

64+
/**
65+
* Returns a string representation in the form <code>host:port</code>,
66+
* adding square brackets if needed
67+
*/
68+
@Override
69+
public String toString() {
70+
// "[]:12345" requires 8 extra bytes.
71+
StringBuilder builder = new StringBuilder(host.length() + 8);
72+
if (host.indexOf(':') >= 0) {
73+
builder.append('[').append(host).append(']');
74+
} else {
75+
builder.append(host);
76+
}
77+
builder.append(':').append(port);
78+
return builder.toString();
79+
}
6480
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.arangodb;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
/**
9+
* @author Anton Sarov - initial contribution
10+
*
11+
*/
12+
public class ArangoHostTest {
13+
14+
@Test
15+
public void testToString() {
16+
ArangoHost arangoHost = new ArangoHost("127.0.0.1", 8529);
17+
assertThat(arangoHost.toString(), is("127.0.0.1:8529"));
18+
}
19+
20+
@Test
21+
public void testToStringIPv6() {
22+
ArangoHost arangoHost = new ArangoHost("2001:db8:1f70::999:de8:7648:6e8", 8529);
23+
assertThat(arangoHost.toString(), is("[2001:db8:1f70::999:de8:7648:6e8]:8529"));
24+
}
25+
}

0 commit comments

Comments
 (0)