1515import org .apache .lucene .util .BytesRef ;
1616import org .elasticsearch .common .network .CIDRUtils ;
1717import org .elasticsearch .common .network .InetAddresses ;
18- import org .elasticsearch .core . SuppressForbidden ;
18+ import org .elasticsearch .common . network . NetworkAddress ;
1919import org .elasticsearch .xcontent .XContentString ;
2020
2121import java .net .InetAddress ;
@@ -38,17 +38,46 @@ class ESInetAddressPoint extends Field {
3838 private final XContentString ipString ;
3939 private final InetAddress inetAddress ;
4040
41- protected ESInetAddressPoint (String name , XContentString ipString ) {
41+ /**
42+ * Creates a new ESInetAddressPoint, indexing the provided address.
43+ * <p>
44+ * This is the difference compared to {@link #ESInetAddressPoint(String, InetAddress)}
45+ * and {@link InetAddressPoint#InetAddressPoint(String, InetAddress)}
46+ * is that this constructor uses a more efficient way to parse the IP address that avoids the need to create
47+ * a {@link String} and an {@link InetAddress} object for the IP address.
48+ *
49+ * @param name the name of the field
50+ * @param value the IP address as a string
51+ * @throws IllegalArgumentException if the field name or value is null or if the IP address is invalid
52+ */
53+ protected ESInetAddressPoint (String name , XContentString value ) {
4254 super (name , TYPE );
43- this .fieldsData = new BytesRef (InetAddresses .encodeAsIpv6 (ipString ));
44- this .ipString = ipString ;
55+ if (value == null ) {
56+ throw new IllegalArgumentException ("point must not be null" );
57+ }
58+ this .fieldsData = new BytesRef (InetAddresses .encodeAsIpv6 (value ));
59+ this .ipString = value ;
4560 this .inetAddress = null ;
4661 }
4762
48- protected ESInetAddressPoint (String name , InetAddress inetAddress ) {
63+ /**
64+ * Creates a new ESInetAddressPoint, indexing the provided address.
65+ * <p>
66+ * This constructor is similar to Lucene's InetAddressPoint.
67+ * For performance reasons, it is recommended to use the constructor that accepts
68+ * an {@link XContentString} representation of the IP address instead.
69+ *
70+ * @param name field name
71+ * @param value InetAddress value
72+ * @throws IllegalArgumentException if the field name or value is null.
73+ */
74+ protected ESInetAddressPoint (String name , InetAddress value ) {
4975 super (name , TYPE );
50- this .fieldsData = new BytesRef (CIDRUtils .encode (inetAddress .getAddress ()));
51- this .inetAddress = inetAddress ;
76+ if (value == null ) {
77+ throw new IllegalArgumentException ("point must not be null" );
78+ }
79+ this .fieldsData = new BytesRef (CIDRUtils .encode (value .getAddress ()));
80+ this .inetAddress = value ;
5281 this .ipString = null ;
5382 }
5483
@@ -63,10 +92,6 @@ public InetAddress getInetAddress() {
6392 }
6493
6594 @ Override
66- @ SuppressForbidden (
67- reason = "Calling InetAddress#getHostAddress to mimic what InetAddressPoint does. "
68- + "Some tests depend on the exact string representation."
69- )
7095 public String toString () {
7196 StringBuilder result = new StringBuilder ();
7297 result .append (getClass ().getSimpleName ());
@@ -79,10 +104,10 @@ public String toString() {
79104 InetAddress address = InetAddressPoint .decode (BytesRef .deepCopyOf (bytes ).bytes );
80105 if (address .getAddress ().length == 16 ) {
81106 result .append ('[' );
82- result .append (address . getHostAddress ( ));
107+ result .append (NetworkAddress . format ( address ));
83108 result .append (']' );
84109 } else {
85- result .append (address . getHostAddress ( ));
110+ result .append (NetworkAddress . format ( address ));
86111 }
87112
88113 result .append ('>' );
0 commit comments