1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.data;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.keyset.GordianKeySet;
20 import io.github.tonywasher.joceanus.metis.data.MetisDataDifference;
21 import io.github.tonywasher.joceanus.metis.data.MetisDataDifference.MetisDataDiffers;
22 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataObjectFormat;
23 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
24 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
25
26 import java.util.Arrays;
27 import java.util.Objects;
28
29
30
31
32 public class PrometheusEncryptedPair
33 implements MetisDataObjectFormat, MetisDataDiffers {
34
35
36
37 private final Object theValue;
38
39
40
41
42 private byte[] theBytes;
43
44
45
46
47 private GordianKeySet theKeySet;
48
49
50
51
52
53
54
55
56 PrometheusEncryptedPair(final GordianKeySet pKeySet,
57 final Object pValue,
58 final byte[] pBytes) {
59 theKeySet = pKeySet;
60 theValue = pValue;
61 theBytes = pBytes;
62 }
63
64
65
66
67
68
69 public GordianKeySet getKeySet() {
70 return theKeySet;
71 }
72
73
74
75
76
77
78 public Object getValue() {
79 return theValue;
80 }
81
82
83
84
85
86
87 public byte[] getBytes() {
88 return theBytes;
89 }
90
91
92
93
94
95
96
97
98 protected void adoptEncryption(final PrometheusEncryptor pEncryptor,
99 final PrometheusEncryptedPair pSource) throws OceanusException {
100
101 theKeySet = pEncryptor.getKeySet();
102
103
104 if (pSource == null
105 || MetisDataDifference.difference(theKeySet, pSource.getKeySet()).isDifferent()
106 || MetisDataDifference.difference(getValue(), pSource.getValue()).isDifferent()) {
107
108 theBytes = pEncryptor.encryptValue(getValue());
109
110
111 } else {
112
113 theBytes = pSource.getBytes();
114 }
115 }
116
117 @Override
118 public String formatObject(final OceanusDataFormatter pFormatter) {
119
120 return pFormatter.formatObject(theValue);
121 }
122
123 @Override
124 public String toString() {
125 return theValue.toString();
126 }
127
128 @Override
129 public boolean equals(final Object pThat) {
130
131 if (this == pThat) {
132 return true;
133 }
134 if (pThat == null) {
135 return false;
136 }
137
138
139 if (pThat.getClass() != this.getClass()) {
140 return false;
141 }
142
143
144 final PrometheusEncryptedPair myThat = (PrometheusEncryptedPair) pThat;
145
146
147 if (!theKeySet.equals(myThat.getKeySet())) {
148 return false;
149 }
150
151
152 if (MetisDataDifference.difference(getValue(), myThat.getValue()).isDifferent()) {
153 return false;
154 }
155
156
157 return Arrays.equals(getBytes(), myThat.getBytes());
158 }
159
160 @Override
161 public int hashCode() {
162 return Objects.hash(theKeySet, theValue, Arrays.hashCode(theBytes));
163 }
164
165 @Override
166 public MetisDataDifference differs(final Object pThat) {
167
168 if (pThat == null) {
169 return MetisDataDifference.DIFFERENT;
170 }
171
172
173 if (!getClass().equals(pThat.getClass())) {
174 return MetisDataDifference.DIFFERENT;
175 }
176
177
178 final PrometheusEncryptedPair myField = (PrometheusEncryptedPair) pThat;
179
180
181 if (MetisDataDifference.difference(getValue(), myField.getValue()).isDifferent()) {
182 return MetisDataDifference.DIFFERENT;
183 }
184
185
186 if (!Arrays.equals(getBytes(), myField.getBytes())) {
187 return MetisDataDifference.SECURITY;
188 }
189
190
191 return MetisDataDifference.IDENTICAL;
192 }
193 }