-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSzRecordKey.java
86 lines (81 loc) · 3.71 KB
/
SzRecordKey.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
package com.senzing.g2.engine;
import java.util.Objects;
/**
* Desribes a key for identifying a record as Java record class containing a data source code and
* record ID.
*
* @param dataSourceCode The non-null {@link String} data source code identifying the data source of
* the record.
* @param recordId The non-null {@link String} record ID identifying the record within the data
* source.
*/
public record SzRecordKey(String dataSourceCode, String recordId)
implements Comparable<SzRecordKey> {
/**
* Constructs with the specified data source code and record ID. This will throw a {@link
* NullPointerException} if either parameter is <code>null</code> and throw {@link
* IllegalArgumentException} if either parameter is empty-string or only contains whitespace.
*
* @param dataSourceCode The non-null {@link String} data source code identifying the data source
* of the record.
* @param recordId The non-null {@link String} record ID identifying the record within the data
* source.
*/
public SzRecordKey(String dataSourceCode, String recordId) {
Objects.requireNonNull(dataSourceCode, "The data source code cannot be null");
Objects.requireNonNull(recordId, "The record ID cannot be null");
if (dataSourceCode.trim().length() == 0 || recordId.trim().length() == 0) {
throw new IllegalArgumentException(
"Empty string or all whitespace not allowed for either parameter. "
+ "dataSourceCode=[ "
+ dataSourceCode
+ " ], recordId=[ "
+ recordId
+ " ]");
}
this.dataSourceCode = dataSourceCode.trim();
this.recordId = recordId.trim();
}
/**
* Shorthand static for constructing a new instance of {@link SzRecordKey} with the specified data
* source code and record ID. The specified data source code and record ID must not be <code>null
* </code> and must each contain at least one character that is not whitespace or else an
* exception is thrown.
*
* @param dataSourceCode The non-null {@link String} data source code identifying the data source
* of the record.
* @param recordId The non-null {@link String} record ID identifying the record within the data
* source.
* @return The constructed {@link SzRecordKey}.
* @throws NullPointerException If either parameter is <code>null</code>.
* @throws IllegalArgumentException If either parameter is empty-string or only contains
* whitespace.
*/
public static SzRecordKey of(String dataSourceCode, String recordId)
throws NullPointerException, IllegalArgumentException {
return new SzRecordKey(dataSourceCode, recordId);
}
/**
* Implemented to sort {@link SzRecordKey} instances first on data source code and then on record
* ID. This is a null-friendly comparison that sorts <code>null</code> values firsdt.
*
* @param recordKey The {@link SzRecordKey} to compare to.
* @return A negative integer, zero (0) or a positive integer depending on whether this instance
* sorts less-than, equal-to or greater-than the specified instance, respectively.
*/
public int compareTo(SzRecordKey recordKey) {
if (recordKey == null) return 1; // null-friendly, null firsdt
int diff = this.dataSourceCode().compareTo(recordKey.dataSourceCode());
if (diff != 0) return diff;
return this.recordId().compareTo(recordKey.recordId());
}
/**
* Returns a brief {@link String} representation of this record class. This formats the data
* source code and record ID with a colon character in between them.
*
* @return A brief {@link String} representation of this class.
*/
public String toString() {
return this.dataSourceCode + ":" + this.recordId;
}
}