Skip to content

Commit faefe90

Browse files
committed
Convert MainType to record
1 parent 936322d commit faefe90

File tree

2 files changed

+31
-52
lines changed

2 files changed

+31
-52
lines changed

src/io/calimero/dptxlator/TranslatorTypes.java

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -194,40 +194,29 @@ public final class TranslatorTypes
194194
/**
195195
* Maps a data type main number to a corresponding translator class doing the DPT translations. Objects of this type
196196
* are immutable.
197+
*
198+
* @param mainNumber main number assigned to the data type
199+
* @param translator represents a translator class of type {@link DPTXlator}
200+
* @param description textual information describing this data type to a user, use {@code null} for no description
197201
*/
198-
public static class MainType
199-
{
200-
private final Class<? extends DPTXlator> xlator;
201-
private final String desc;
202-
private final int main;
203-
204-
/**
205-
* Creates a new main number to translator mapping.
206-
*
207-
* @param mainNumber main number assigned to the data type
208-
* @param translator represents a translator class of type {@link DPTXlator}
209-
* @param description textual information describing this data type to a user, use {@code null} for no
210-
* description
211-
*/
212-
public MainType(final int mainNumber, final Class<? extends DPTXlator> translator, final String description)
213-
{
202+
public record MainType(int mainNumber, Class<? extends DPTXlator> translator, String description) {
203+
public MainType {
214204
if (mainNumber <= 0)
215205
throw new KNXIllegalArgumentException("invalid main number");
216206
if (!DPTXlator.class.isAssignableFrom(translator) || DPTXlator.class.equals(translator))
217207
throw new KNXIllegalArgumentException(translator.getName() + " is not a valid DPT translator type");
218-
main = mainNumber;
219-
xlator = translator;
220-
desc = description == null ? "" : description;
208+
description = description == null ? "" : description;
221209
}
222210

223211
/**
224-
* Returns the data type main number.
212+
* @deprecated Returns the data type main number.
225213
*
226214
* @return main number as int
227215
*/
228-
public final int getMainNumber()
216+
@Deprecated(forRemoval = true)
217+
public int getMainNumber()
229218
{
230-
return main;
219+
return mainNumber;
231220
}
232221

233222
/**
@@ -239,24 +228,25 @@ public final int getMainNumber()
239228
* @throws KNXFormatException to forward all target exceptions thrown in the constructor of the translator
240229
* @throws KNXException thrown on translator class creation errors (e.g. security / access problems)
241230
*/
242-
public final DPTXlator createTranslator(final DPT dpt) throws KNXException
231+
public DPTXlator createTranslator(final DPT dpt) throws KNXException
243232
{
244233
return createTranslator(dpt.getID());
245234
}
246235

247236
/**
248-
* Creates a new instance of the translator for the given datapoint type ID.
237+
* @deprecated Creates a new instance of the translator for the given datapoint type ID.
249238
*
250239
* @param dptId datapoint type ID for selecting a particular kind of value translation; if the datapoint type ID
251240
* is not part of the translator of this main type, a {@link KNXFormatException} is thrown
252241
* @return the new {@link DPTXlator} instance
253242
* @throws KNXFormatException to forward all target exceptions thrown in the constructor of the translator
254243
* @throws KNXException thrown on translator class creation errors (e.g. security / access problems)
255244
*/
245+
@Deprecated(forRemoval = true)
256246
public DPTXlator createTranslator(final String dptId) throws KNXException
257247
{
258248
try {
259-
return xlator.getConstructor(String.class).newInstance(dptId);
249+
return translator.getConstructor(String.class).newInstance(dptId);
260250
}
261251
catch (final InvocationTargetException e) {
262252
// try to forward encapsulated target exception
@@ -281,12 +271,12 @@ public DPTXlator createTranslator(final String dptId) throws KNXException
281271
* is not part of the translator of this main type, a {@link KNXFormatException} is thrown
282272
* @return the new {@link DPTXlator} instance
283273
* @throws KNXFormatException to forward all target exceptions thrown in the constructor of the translator
284-
* @throws KNXException thrown on translator class creation errors (e.g. security/access problems)
274+
* @throws KNXException thrown on translator class creation errors (e.g., security/access problems)
285275
*/
286276
public DPTXlator createTranslator(final DptId dptId) throws KNXException
287277
{
288278
try {
289-
return xlator.getConstructor(String.class).newInstance(dptId.toString());
279+
return translator.getConstructor(String.class).newInstance(dptId.toString());
290280
}
291281
catch (final InvocationTargetException e) {
292282
// try to forward encapsulated target exception
@@ -305,40 +295,30 @@ public DPTXlator createTranslator(final DptId dptId) throws KNXException
305295
}
306296

307297
/**
308-
* Returns the translator class used for this main type.
309-
* <p>
310-
*
311-
* @return the translator class as {@link Class} object
312-
*/
313-
public Class<? extends DPTXlator> getTranslator()
314-
{
315-
return xlator;
316-
}
317-
318-
/**
319-
* Returns the description to this main type, if any.
298+
* @deprecated Returns the description to this main type, if any.
320299
* <p>
321300
*
322301
* @return description as String, or the empty string if no description set
323302
*/
324-
public final String getDescription()
303+
@Deprecated(forRemoval = true)
304+
public String getDescription()
325305
{
326-
return desc;
306+
return description;
327307
}
328308

329309
/**
330310
* Returns a map containing all implemented subtypes.
331311
* <p>
332312
*
333313
* @return available subtypes as {@link Map}
334-
* @throws KNXException thrown on problems accessing the translator while retrieving sub types (e.g. security /
314+
* @throws KNXException thrown on problems accessing the translator while retrieving sub types (e.g., security /
335315
* access problem) for external, user supplied translators
336316
* @see DPTXlator#getSubTypes()
337317
*/
338318
public Map<String, DPT> getSubTypes() throws KNXException
339319
{
340320
try {
341-
return subTypes(xlator);
321+
return subTypes(translator);
342322
}
343323
catch (final Exception e) {
344324
// for SecurityException and IllegalAccessException
@@ -350,7 +330,7 @@ public Map<String, DPT> getSubTypes() throws KNXException
350330
@Override
351331
public String toString()
352332
{
353-
return getDescription();
333+
return description();
354334
}
355335
}
356336

@@ -468,8 +448,8 @@ public static MainType getMainType(final int mainNumber)
468448
/**
469449
* Returns all available data types which have a DPT translator implementation assigned.
470450
* <p>
471-
* The map returned is the same used by this class for type lookup. Map entries can be added, likewise entries might
472-
* be removed to change future lookup results.
451+
* The map returned is the same used by this class for type lookup. Map entries can be added or
452+
* removed to change future lookup results.
473453
*
474454
* @return a {@link Map} containing all data types as {@link MainType} objects
475455
*/

test/io/calimero/dptxlator/TranslatorTypesTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ void getMainType() throws KNXException
7575
}
7676

7777
for (MainType type : types) {
78-
final MainType t = TranslatorTypes.getMainType(type.getMainNumber());
79-
assertEquals(t.getMainNumber(), type.getMainNumber());
80-
t.createTranslator(t.getSubTypes().values().iterator().next()
81-
.getID());
78+
final MainType t = TranslatorTypes.getMainType(type.mainNumber());
79+
assertEquals(t.mainNumber(), type.mainNumber());
80+
t.createTranslator(t.getSubTypes().values().iterator().next().getID());
8281
}
8382
}
8483

@@ -154,15 +153,15 @@ void createTranslator() throws KNXException
154153
{
155154
// with main number
156155
for (MainType mainType : types) {
157-
final int main = mainType.getMainNumber();
156+
final int main = mainType.mainNumber();
158157
final String dptID = TranslatorTypes.getMainType(main).getSubTypes()
159158
.values().iterator().next().getID();
160159
TranslatorTypes.createTranslator(main, dptID);
161160
}
162161

163162
// without main number
164163
for (MainType type : types) {
165-
final int main = type.getMainNumber();
164+
final int main = type.mainNumber();
166165
final String dptID = TranslatorTypes.getMainType(main).getSubTypes()
167166
.values().iterator().next().getID();
168167
TranslatorTypes.createTranslator(0, dptID);

0 commit comments

Comments
 (0)