-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathPGobject.java
More file actions
131 lines (114 loc) · 3.32 KB
/
PGobject.java
File metadata and controls
131 lines (114 loc) · 3.32 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2003, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.util;
import static org.postgresql.util.internal.Nullness.castNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.Serializable;
import java.sql.SQLException;
/**
* PGobject is a class used to describe unknown types An unknown type is any type that is unknown by
* JDBC Standards.
*/
public class PGobject implements Serializable, Cloneable {
protected @Nullable String type;
protected @Nullable String value;
/**
* This is called by org.postgresql.Connection.getObject() to create the object.
*/
public PGobject() {
}
/**
* This method sets the type of this object.
*
* <p>It should not be extended by subclasses, hence it is final</p>
*
* @param type a string describing the type of the object
*/
public final void setType(String type) {
this.type = type;
}
/**
* This method sets the value of this object. It must be overridden.
*
* @param value a string representation of the value of the object
* @throws SQLException thrown if value is invalid for this type
*/
public void setValue(@Nullable String value) throws SQLException {
this.value = value;
}
/**
* As this cannot change during the life of the object, it's final.
*
* @return the type name of this object
*/
public final String getType() {
return castNonNull(type, "PGobject#type is uninitialized. Please call setType(String)");
}
/**
* This must be overridden, to return the value of the object, in the form required by
* org.postgresql.
*
* @return the value of this object
*/
public @Nullable String getValue() {
return value;
}
/**
* Returns true if the current object wraps `null` value.
* This might be helpful
*
* @return true if the current object wraps `null` value.
*/
public boolean isNull() {
return getValue() == null;
}
/**
* This must be overridden to allow comparisons of objects.
*
* @param obj Object to compare with
* @return true if the two boxes are identical
*/
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof PGobject) {
final Object otherValue = ((PGobject) obj).getValue();
if (otherValue == null) {
return getValue() == null;
}
return otherValue.equals(getValue());
}
return false;
}
/**
* This must be overridden to allow the object to be cloned.
*/
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* This is defined here, so user code need not override it.
*
* @return the value of this object, in the syntax expected by org.postgresql
*/
@Override
@SuppressWarnings("nullness")
public String toString() {
return getValue();
}
/**
* Compute hash. As equals() use only value. Return the same hash for the same value.
*
* @return Value hashcode, 0 if value is null {@link java.util.Objects#hashCode(Object)}
*/
@Override
public int hashCode() {
String value = getValue();
return value != null ? value.hashCode() : 0;
}
protected static boolean equals(@Nullable Object a, @Nullable Object b) {
return a == b || a != null && a.equals(b);
}
}