From f2649044851898fe5a4a2d38ca27c5b16731c0ea Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 28 Feb 2022 13:17:23 +0100 Subject: [PATCH] [jOOQ/jOOQ#2092] Add support for the Postgres INET data type (java.net.InetAddress) --- .../bindings/AbstractInetBinding.java | 81 ++++++++++++ .../bindings/AbstractPostgresBinding.java | 6 - .../extensions/bindings/CidrBinding.java | 62 ++++++++++ .../extensions/bindings/HstoreBinding.java | 6 +- .../extensions/bindings/InetBinding.java | 62 ++++++++++ .../converters/AbstractInetConverter.java | 88 +++++++++++++ .../extensions/converters/CidrConverter.java | 61 +++++++++ .../converters/HstoreConverter.java | 7 +- .../extensions/converters/InetConverter.java | 61 +++++++++ .../extensions/types/AbstractInet.java | 90 ++++++++++++++ .../jooq/postgres/extensions/types/Cidr.java | 89 +++++++++++++ .../postgres/extensions/types/Hstore.java | 1 - .../jooq/postgres/extensions/types/Inet.java | 117 ++++++++++++++++++ 13 files changed, 713 insertions(+), 18 deletions(-) create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractInetBinding.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/CidrBinding.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/InetBinding.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/AbstractInetConverter.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/CidrConverter.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/InetConverter.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/AbstractInet.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Cidr.java create mode 100644 jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Inet.java diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractInetBinding.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractInetBinding.java new file mode 100644 index 0000000000..572632a6f6 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractInetBinding.java @@ -0,0 +1,81 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.bindings; + +import java.sql.SQLException; +import java.sql.Types; + +import org.jooq.BindingGetResultSetContext; +import org.jooq.BindingGetStatementContext; +import org.jooq.BindingRegisterContext; +import org.jooq.BindingSetStatementContext; +import org.jooq.Converter; +import org.jooq.postgres.extensions.converters.InetConverter; +import org.jooq.postgres.extensions.types.AbstractInet; +import org.jooq.postgres.extensions.types.Inet; + +/** + * A binding for the PostgreSQL inet or cidr data + * type. + * + * @author Lukas Eder + */ +abstract class AbstractInetBinding extends AbstractPostgresBinding { + + @Override + public void register(final BindingRegisterContext ctx) throws SQLException { + ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR); + } + + @Override + public void set(final BindingSetStatementContext ctx) throws SQLException { + Object value = ctx.convert(converter()).value(); + + ctx.statement().setString(ctx.index(), value == null ? null : "" + value); + } + + @Override + public void get(final BindingGetResultSetContext ctx) throws SQLException { + ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index())); + } + + @Override + public void get(final BindingGetStatementContext ctx) throws SQLException { + ctx.convert(converter()).value(ctx.statement().getString(ctx.index())); + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractPostgresBinding.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractPostgresBinding.java index c0a3acd992..faa1c1ddb5 100644 --- a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractPostgresBinding.java +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/AbstractPostgresBinding.java @@ -39,7 +39,6 @@ package org.jooq.postgres.extensions.bindings; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; -import java.util.Map; import org.jooq.BindingGetSQLInputContext; import org.jooq.BindingSQLContext; @@ -53,11 +52,6 @@ import org.jooq.impl.AbstractBinding; */ public abstract class AbstractPostgresBinding extends AbstractBinding { - /** - * Generated UID - */ - private static final long serialVersionUID = 689952685043626697L; - /** * Provide the data type name for casts. *

diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/CidrBinding.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/CidrBinding.java new file mode 100644 index 0000000000..c935f528ee --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/CidrBinding.java @@ -0,0 +1,62 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.bindings; + +import org.jooq.Converter; +import org.jooq.postgres.extensions.converters.CidrConverter; +import org.jooq.postgres.extensions.types.Cidr; + +/** + * A binding for the PostgreSQL cidr data type. + * + * @author Lukas Eder + */ +public class CidrBinding extends AbstractInetBinding { + + private static final Converter CONVERTER = new CidrConverter(); + + @Override + public Converter converter() { + return CONVERTER; + } + + @Override + protected String castType() { + return "cidr"; + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/HstoreBinding.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/HstoreBinding.java index 7ee6a950b7..8df3afb173 100644 --- a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/HstoreBinding.java +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/HstoreBinding.java @@ -56,11 +56,7 @@ import org.jooq.postgres.extensions.types.Hstore; */ public class HstoreBinding extends AbstractPostgresBinding { - /** - * Generated UID - */ - private static final long serialVersionUID = 5809336497608771915L; - private static final Converter CONVERTER = new HstoreConverter(); + private static final Converter CONVERTER = new HstoreConverter(); @Override public Converter converter() { diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/InetBinding.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/InetBinding.java new file mode 100644 index 0000000000..40ef8445e2 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/bindings/InetBinding.java @@ -0,0 +1,62 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.bindings; + +import org.jooq.Converter; +import org.jooq.postgres.extensions.converters.InetConverter; +import org.jooq.postgres.extensions.types.Inet; + +/** + * A binding for the PostgreSQL inet data type. + * + * @author Lukas Eder + */ +public class InetBinding extends AbstractInetBinding { + + private static final Converter CONVERTER = new InetConverter(); + + @Override + public Converter converter() { + return CONVERTER; + } + + @Override + protected String castType() { + return "inet"; + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/AbstractInetConverter.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/AbstractInetConverter.java new file mode 100644 index 0000000000..cac4bcb7fb --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/AbstractInetConverter.java @@ -0,0 +1,88 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.converters; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.jooq.exception.DataTypeException; +import org.jooq.impl.AbstractConverter; +import org.jooq.postgres.extensions.types.AbstractInet; + +/** + * A converter for the PostgreSQL inet or cidr data + * type. + * + * @author Lukas Eder + */ +abstract class AbstractInetConverter extends AbstractConverter { + + public AbstractInetConverter(Class uType) { + super(Object.class, uType); + } + + abstract U construct(InetAddress address, Integer prefix); + + @Override + public U from(Object t) { + if (t == null) + return null; + + String[] s = t.toString().split("/"); + try { + InetAddress a = InetAddress.getByName(s[0]); + + if (s.length == 1) + return construct(a, null); + else + return construct(a, Integer.valueOf(s[1])); + } + catch (UnknownHostException e) { + throw new DataTypeException("Cannot parse InetAddress", e); + } + } + + @Override + public Object to(AbstractInet u) { + return u == null + ? null + : u.prefix() == null + ? u.address().getHostAddress() + : u.address().getHostAddress() + "/" + u.prefix(); + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/CidrConverter.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/CidrConverter.java new file mode 100644 index 0000000000..1f9a7d18d6 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/CidrConverter.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.converters; + +import static org.jooq.postgres.extensions.types.Cidr.cidr; + +import java.net.InetAddress; + +import org.jooq.postgres.extensions.types.Cidr; + +/** + * A converter for the PostgreSQL cidr data type. + * + * @author Lukas Eder + */ +public class CidrConverter extends AbstractInetConverter { + + public CidrConverter() { + super(Cidr.class); + } + + @Override + final Cidr construct(InetAddress address, Integer prefix) { + return cidr(address, prefix); + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/HstoreConverter.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/HstoreConverter.java index dfffe9a7fb..1c6b0db6c0 100644 --- a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/HstoreConverter.java +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/HstoreConverter.java @@ -43,18 +43,13 @@ import org.jooq.impl.AbstractConverter; import org.jooq.postgres.extensions.types.Hstore; /** - * A binding for the PostgreSQL hstore data type. + * A converter for the PostgreSQL hstore data type. * * @author Dmitry Baev * @author Lukas Eder */ public class HstoreConverter extends AbstractConverter { - /** - * Generated UID - */ - private static final long serialVersionUID = -2852275331366617696L; - public HstoreConverter() { super(Object.class, Hstore.class); } diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/InetConverter.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/InetConverter.java new file mode 100644 index 0000000000..85fabba1b8 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/converters/InetConverter.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.converters; + +import static org.jooq.postgres.extensions.types.Inet.inet; + +import java.net.InetAddress; + +import org.jooq.postgres.extensions.types.Inet; + +/** + * A converter for the PostgreSQL inet data type. + * + * @author Lukas Eder + */ +public class InetConverter extends AbstractInetConverter { + + public InetConverter() { + super(Inet.class); + } + + @Override + final Inet construct(InetAddress address, Integer prefix) { + return inet(address, prefix); + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/AbstractInet.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/AbstractInet.java new file mode 100644 index 0000000000..27d22f79a5 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/AbstractInet.java @@ -0,0 +1,90 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.types; + +import java.io.Serializable; +import java.net.InetAddress; +import java.util.Objects; + +import org.jetbrains.annotations.NotNull; + + +/** + * A data type representing the PostgreSQL inet or + * cidr type. + * + * @author Lukas Eder + */ +public abstract class AbstractInet implements Serializable { + + private final InetAddress address; + private final Integer prefix; + + AbstractInet(InetAddress address, Integer prefix) { + this.address = address; + this.prefix = prefix; + } + + @NotNull + public final InetAddress address() { + return address; + } + + public /* non-final */ Integer prefix() { + return prefix; + } + + @Override + public int hashCode() { + return address.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj instanceof AbstractInet) + return address.equals(((AbstractInet) obj).address) && Objects.equals(prefix, ((AbstractInet) obj).prefix); + return false; + } + + @Override + public String toString() { + return prefix == null ? address.getHostAddress() : address.getHostAddress() + "/" + prefix; + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Cidr.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Cidr.java new file mode 100644 index 0000000000..b244c22cba --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Cidr.java @@ -0,0 +1,89 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.types; + +import java.net.InetAddress; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A data type representing the PostgreSQL cidr type. + * + * @author Lukas Eder + */ +public final class Cidr extends AbstractInet { + + private Cidr(InetAddress address, Integer prefix) { + super(address, prefix); + } + + @Override + @NotNull + public final Integer prefix() { + return super.prefix(); + } + + /** + * Create a new {@link Cidr} instance. + */ + @NotNull + public static final Cidr valueOf(InetAddress address, Integer prefix) { + return new Cidr(address, prefix); + } + + /** + * Create a new {@link Cidr} instance. + *

+ * This is the same as {@link #valueOf(InetAddress, Integer)}, but it can be + * static imported. + */ + @NotNull + public static final Cidr cidr(InetAddress address, Integer prefix) { + return new Cidr(address, prefix); + } + + /** + * Create a new {@link Cidr} instance, or null if the input + * address is null. + */ + @Nullable + public static final Cidr cidrOrNull(InetAddress address, Integer prefix) { + return address == null ? null : new Cidr(address, prefix); + } +} diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Hstore.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Hstore.java index daa6561304..67d9ea2f2b 100644 --- a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Hstore.java +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Hstore.java @@ -51,7 +51,6 @@ import org.jetbrains.annotations.Nullable; */ public final class Hstore implements Serializable { - private static final long serialVersionUID = 860591239448066408L; private final Map data; private Hstore(Map data) { diff --git a/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Inet.java b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Inet.java new file mode 100644 index 0000000000..0d39afb7f0 --- /dev/null +++ b/jOOQ-postgres-extensions/src/main/java/org/jooq/postgres/extensions/types/Inet.java @@ -0,0 +1,117 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.postgres.extensions.types; + +import java.net.InetAddress; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A data type representing the PostgreSQL inet type. + * + * @author Lukas Eder + */ +public final class Inet extends AbstractInet { + + private Inet(InetAddress address, Integer prefix) { + super(address, prefix); + } + + @Override + @Nullable + public final Integer prefix() { + return super.prefix(); + } + + /** + * Create a new {@link Inet} instance. + */ + @NotNull + public static final Inet valueOf(InetAddress address) { + return new Inet(address, null); + } + + /** + * Create a new {@link Inet} instance. + */ + @NotNull + public static final Inet valueOf(InetAddress address, Integer prefix) { + return new Inet(address, prefix); + } + + /** + * Create a new {@link Inet} instance. + *

+ * This is the same as {@link #valueOf(InetAddress)}, but it can be static + * imported. + */ + @NotNull + public static final Inet inet(InetAddress address) { + return new Inet(address, null); + } + + /** + * Create a new {@link Inet} instance. + *

+ * This is the same as {@link #valueOf(InetAddress, Integer)}, but it can be + * static imported. + */ + @NotNull + public static final Inet inet(InetAddress address, Integer prefix) { + return new Inet(address, prefix); + } + + /** + * Create a new {@link Inet} instance, or null if the input + * address is null. + */ + @Nullable + public static final Inet inetOrNull(InetAddress address) { + return address == null ? null : new Inet(address, null); + } + + /** + * Create a new {@link Inet} instance, or null if the input + * address is null. + */ + @Nullable + public static final Inet inetOrNull(InetAddress address, Integer prefix) { + return address == null ? null : new Inet(address, prefix); + } +}