-
Notifications
You must be signed in to change notification settings - Fork 0
/
InetAddressType.java
101 lines (82 loc) · 2.85 KB
/
InetAddressType.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
package com.example.inettest;
import com.google.common.net.InetAddresses;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGobject;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class InetAddressType implements UserType {
@Override
public int[] sqlTypes() {
return new int[]{java.sql.Types.OTHER};
}
@Override
public Class<InetAddress> returnedClass() {
return InetAddress.class;
}
@Override
public boolean equals(Object o, Object o1) throws HibernateException {
if (o == null && o1 == null)
return true;
else if (o == null || o1 == null)
return false;
return o.equals(o1);
}
@Override
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sessionImplementor, Object owner) throws HibernateException, SQLException {
if (names.length != 1)
throw new IllegalArgumentException("names.length != 1, names = " + names);
String value = resultSet.getString(names[0]);
if (value == null) {
return null;
} else {
return InetAddresses.forString(value);
}
}
@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException {
if (value == null) {
preparedStatement.setNull(i, java.sql.Types.OTHER);
} else {
PGobject object = new PGobject();
object.setValue(((InetAddress) value).getHostAddress());
object.setType("inet");
preparedStatement.setObject(i, object);
}
}
@Override
public Object deepCopy(Object o) throws HibernateException {
if (o == null)
return null;
try {
return InetAddress.getByAddress(((InetAddress) o).getAddress());
} catch (UnknownHostException e) {
throw new AssertionError("this can't happen!");
}
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object o) throws HibernateException {
return (Serializable) o;
}
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
@Override
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
}