Skip to content

Commit 54e4f58

Browse files
committed
Refactor classes to be either final or have final equals/hashCode
1 parent 4da4f7b commit 54e4f58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+320
-166
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryError.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* {@link BigQueryException} is thrown the BigQuery Error that caused it, if any, can be accessed
1818
* with {@link BigQueryException#error()}.
1919
*/
20-
public class BigQueryError implements Serializable {
20+
public final class BigQueryError implements Serializable {
2121

2222
static final Function<ErrorProto, BigQueryError> FROM_PB_FUNCTION =
2323
new Function<ErrorProto, BigQueryError>() {
@@ -98,7 +98,9 @@ public String toString() {
9898

9999
@Override
100100
public boolean equals(Object obj) {
101-
return obj instanceof BigQueryError && Objects.equals(toPb(), ((BigQueryError) obj).toPb());
101+
return obj == this
102+
|| obj instanceof BigQueryError
103+
&& Objects.equals(toPb(), ((BigQueryError) obj).toPb());
102104
}
103105

104106
ErrorProto toPb() {

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CopyJobConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ ToStringHelper toStringHelper() {
181181

182182
@Override
183183
public final boolean equals(Object obj) {
184-
return this == obj
185-
|| obj instanceof CopyJobConfiguration
184+
return obj == this
185+
|| obj != null
186+
&& obj.getClass().equals(CopyJobConfiguration.class)
186187
&& baseEquals((CopyJobConfiguration) obj);
187188
}
188189

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Google BigQuery options for CSV format. This class wraps some properties of CSV files used by
2626
* BigQuery to parse external data.
2727
*/
28-
public class CsvOptions extends FormatOptions {
28+
public final class CsvOptions extends FormatOptions {
2929

3030
private static final long serialVersionUID = 2193570529308612708L;
3131

@@ -224,7 +224,9 @@ public int hashCode() {
224224

225225
@Override
226226
public boolean equals(Object obj) {
227-
return obj instanceof CsvOptions && Objects.equals(toPb(), ((CsvOptions) obj).toPb());
227+
return obj == this
228+
|| obj instanceof CsvOptions
229+
&& Objects.equals(toPb(), ((CsvOptions) obj).toPb());
228230
}
229231

230232
com.google.api.services.bigquery.model.CsvOptions toPb() {

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,15 @@ public Builder toBuilder() {
231231

232232
@Override
233233
public final boolean equals(Object obj) {
234-
return this == obj
235-
|| obj instanceof Dataset
236-
&& Objects.equals(toPb(), ((Dataset) obj).toPb())
237-
&& Objects.equals(options, ((Dataset) obj).options);
234+
if (obj == this) {
235+
return true;
236+
}
237+
if (obj == null || !obj.getClass().equals(Dataset.class)) {
238+
return false;
239+
}
240+
Dataset other = (Dataset) obj;
241+
return Objects.equals(toPb(), other.toPb())
242+
&& Objects.equals(options, other.options);
238243
}
239244

240245
@Override

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetId.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Google BigQuery Dataset identity.
2828
*/
29-
public class DatasetId implements Serializable {
29+
public final class DatasetId implements Serializable {
3030

3131
private static final long serialVersionUID = -6186254820908152300L;
3232

@@ -68,7 +68,9 @@ public static DatasetId of(String dataset) {
6868

6969
@Override
7070
public boolean equals(Object obj) {
71-
return obj instanceof DatasetId && Objects.equals(toPb(), ((DatasetId) obj).toPb());
71+
return obj == this
72+
|| obj instanceof DatasetId
73+
&& Objects.equals(toPb(), ((DatasetId) obj).toPb());
7274
}
7375

7476
@Override

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ public int hashCode() {
395395

396396
@Override
397397
public boolean equals(Object obj) {
398-
return obj != null
398+
return obj == this
399+
|| obj != null
399400
&& obj.getClass().equals(DatasetInfo.class)
400401
&& Objects.equals(toPb(), ((DatasetInfo) obj).toPb());
401402
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ ToStringHelper toStringHelper() {
256256

257257
@Override
258258
public final boolean equals(Object obj) {
259-
return this == obj
260-
|| obj instanceof ExternalTableDefinition
259+
return obj == this
260+
|| obj != null
261+
&& obj.getClass().equals(ExternalTableDefinition.class)
261262
&& baseEquals((ExternalTableDefinition) obj);
262263
}
263264

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExtractJobConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ ToStringHelper toStringHelper() {
211211

212212
@Override
213213
public final boolean equals(Object obj) {
214-
return this == obj
215-
|| obj instanceof ExtractJobConfiguration
214+
return obj == this
215+
|| obj != null
216+
&& obj.getClass().equals(ExtractJobConfiguration.class)
216217
&& baseEquals((ExtractJobConfiguration) obj);
217218
}
218219

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Field.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
import java.util.Objects;
3333

3434
/**
35-
* Google BigQuery Table field. A table field has a name, a value, a mode and possibly a
36-
* description. Supported types are: {@link Type#integer()}, {@link Type#bool()},
37-
* {@link Type#string()}, {@link Type#floatingPoint()}, {@link Type#timestamp()} and
38-
* {@link Type#record(Field...)}. One or more fields form a table's schema.
35+
* Google BigQuery Table field. A table field has a name, a type, a mode and possibly a description.
36+
* Supported types are: {@link Type#integer()}, {@link Type#bool()}, {@link Type#string()},
37+
* {@link Type#floatingPoint()}, {@link Type#timestamp()} and {@link Type#record(Field...)}. One or
38+
* more fields form a table's schema.
3939
*/
40-
public class Field implements Serializable {
40+
public final class Field implements Serializable {
4141

4242
static final Function<TableFieldSchema, Field> FROM_PB_FUNCTION =
4343
new Function<TableFieldSchema, Field>() {
@@ -56,6 +56,11 @@ public TableFieldSchema apply(Field field) {
5656

5757
private static final long serialVersionUID = -8154262932305199256L;
5858

59+
private final String name;
60+
private final Type type;
61+
private final String mode;
62+
private final String description;
63+
5964
/**
6065
* Data Types for a BigQuery Table field. This class provides factory methods for all BigQuery
6166
* field types. To instantiate a RECORD value the list of sub-fields must be provided.
@@ -185,11 +190,6 @@ public enum Mode {
185190
NULLABLE, REQUIRED, REPEATED
186191
}
187192

188-
private final String name;
189-
private final Type type;
190-
private final String mode;
191-
private final String description;
192-
193193
public static final class Builder {
194194

195195
private String name;

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/FieldValue.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,16 @@ public String toString() {
216216
}
217217

218218
@Override
219-
public int hashCode() {
219+
public final int hashCode() {
220220
return Objects.hash(attribute, value);
221221
}
222222

223223
@Override
224-
public boolean equals(Object obj) {
225-
if (!(obj instanceof FieldValue)) {
224+
public final boolean equals(Object obj) {
225+
if (obj == this) {
226+
return true;
227+
}
228+
if (obj == null || !obj.getClass().equals(FieldValue.class)) {
226229
return false;
227230
}
228231
FieldValue other = (FieldValue) obj;

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/FormatOptions.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public int hashCode() {
5959

6060
@Override
6161
public boolean equals(Object obj) {
62-
return obj instanceof FormatOptions && Objects.equals(type, ((FormatOptions) obj).type());
62+
return obj == this
63+
|| obj != null
64+
&& obj.getClass().equals(FormatOptions.class)
65+
&& Objects.equals(type, ((FormatOptions) obj).type());
6366
}
6467

6568
/**

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @see <a href="https://cloud.google.com/bigquery/streaming-data-into-bigquery">Streaming Data into
3838
* BigQuery</a>
3939
*/
40-
public class InsertAllRequest implements Serializable {
40+
public final class InsertAllRequest implements Serializable {
4141

4242
private static final long serialVersionUID = 211200307773853078L;
4343

@@ -443,6 +443,9 @@ public int hashCode() {
443443

444444
@Override
445445
public boolean equals(Object obj) {
446+
if (obj == this) {
447+
return true;
448+
}
446449
if (!(obj instanceof InsertAllRequest)) {
447450
return false;
448451
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllResponse.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ public boolean hasErrors() {
7474
}
7575

7676
@Override
77-
public int hashCode() {
77+
public final int hashCode() {
7878
return Objects.hash(insertErrors);
7979
}
8080

8181
@Override
82-
public boolean equals(Object obj) {
83-
return obj instanceof InsertAllResponse
82+
public final boolean equals(Object obj) {
83+
return obj == this
84+
|| obj != null
85+
&& obj.getClass().equals(InsertAllResponse.class)
8486
&& Objects.equals(insertErrors, ((InsertAllResponse) obj).insertErrors);
8587
}
8688

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,15 @@ public Builder toBuilder() {
179179

180180
@Override
181181
public final boolean equals(Object obj) {
182-
return this == obj
183-
|| obj instanceof Job
184-
&& Objects.equals(toPb(), ((Job) obj).toPb())
185-
&& Objects.equals(options, ((Job) obj).options);
182+
if (obj == this) {
183+
return true;
184+
}
185+
if (obj == null || !obj.getClass().equals(Job.class)) {
186+
return false;
187+
}
188+
Job other = (Job) obj;
189+
return Objects.equals(toPb(), other.toPb())
190+
&& Objects.equals(options, other.options);
186191
}
187192

188193
@Override

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobId.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Google BigQuery Job identity.
2828
*/
29-
public class JobId implements Serializable {
29+
public final class JobId implements Serializable {
3030

3131
private static final long serialVersionUID = 1225914835379688976L;
3232

@@ -68,7 +68,9 @@ public static JobId of(String job) {
6868

6969
@Override
7070
public boolean equals(Object obj) {
71-
return obj instanceof JobId && Objects.equals(toPb(), ((JobId) obj).toPb());
71+
return obj == this
72+
|| obj instanceof JobId
73+
&& Objects.equals(toPb(), ((JobId) obj).toPb());
7274
}
7375

7476
@Override

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ public int hashCode() {
319319

320320
@Override
321321
public boolean equals(Object obj) {
322-
return obj != null
322+
return obj == this
323+
|| obj != null
323324
&& obj.getClass().equals(JobInfo.class)
324325
&& Objects.equals(toPb(), ((JobInfo) obj).toPb());
325326
}

0 commit comments

Comments
 (0)