Skip to content

Commit f3e84b5

Browse files
committed
Refactor to use DptId for storing id in DPT
1 parent a1fb669 commit f3e84b5

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/io/calimero/dptxlator/DPT.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Calimero 3 - A library for KNX network access
3-
Copyright (c) 2006, 2022 B. Malinowsky
3+
Copyright (c) 2006, 2025 B. Malinowsky
44
55
This program is free software; you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -45,15 +45,14 @@
4545
* representation of a low value, if possible the lower bound of the supported value
4646
* range, and the second contains a corresponding upper bound string value representation.
4747
* <br>
48-
* Even though not enforced, the preferred way for identifying a DPT is to use a datapoint
49-
* type ID (dptID) value in the format "[main number].[sub number]".<br>
48+
* A DPT is identified using a datapoint type ID of the format "[main number].[sub number]".<br>
5049
* Instances of this type are immutable.
5150
*
5251
* @author B. Malinowsky
5352
*/
5453
public class DPT
5554
{
56-
private final String id;
55+
private final DptId dptId;
5756
private final String desc;
5857
private final String v1;
5958
private final String v2;
@@ -62,22 +61,33 @@ public class DPT
6261
/**
6362
* Creates a new datapoint type information structure.
6463
*
65-
* @param typeID datapoint type identifier
64+
* @param dptId datapoint type identifier
6665
* @param description short textual description
6766
* @param lower lower value information
6867
* @param upper upper value information
6968
* @param unit unit of measure, use "" or {@code null} for no unit
7069
*/
71-
public DPT(final String typeID, final String description, final String lower,
72-
final String upper, final String unit)
73-
{
74-
id = typeID;
70+
public DPT(final DptId dptId, final String description, final String lower, final String upper, final String unit) {
71+
this.dptId = dptId;
7572
desc = description;
7673
v1 = lower;
7774
v2 = upper;
7875
this.unit = unit == null ? "" : unit;
7976
}
8077

78+
/**
79+
* Creates a new datapoint type information structure.
80+
*
81+
* @param typeID datapoint type identifier
82+
* @param description short textual description
83+
* @param lower lower value information
84+
* @param upper upper value information
85+
* @param unit unit of measure, use "" or {@code null} for no unit
86+
*/
87+
public DPT(final String typeID, final String description, final String lower, final String upper, final String unit) {
88+
this(DptId.from(typeID), description, lower, upper, unit);
89+
}
90+
8191
/**
8292
* Creates a new datapoint type information structure for a DPT without a unit.
8393
*
@@ -98,9 +108,12 @@ public DPT(final String typeID, final String description, final String lower, fi
98108
*/
99109
public final String getID()
100110
{
101-
return id;
111+
return dptId().toString();
102112
}
103113

114+
/** {@return the DPT identifier} */
115+
public final DptId dptId() { return dptId; }
116+
104117
/**
105118
* Returns the DPT description.
106119
*
@@ -160,7 +173,7 @@ public final String getUpperValue()
160173
public String toString()
161174
{
162175
final StringBuilder sb = new StringBuilder(50);
163-
sb.append(id).append(": ").append(desc);
176+
sb.append(dptId).append(": ").append(desc);
164177
if (!v1.isEmpty())
165178
sb.append(" [").append(v1);
166179
if (v2.length() > 0)
@@ -175,12 +188,12 @@ public String toString()
175188
@Override
176189
public boolean equals(final Object obj)
177190
{
178-
return obj == this || obj instanceof DPT dpt && dpt.id.equals(id);
191+
return obj == this || obj instanceof DPT dpt && dpt.dptId.equals(dptId);
179192
}
180193

181194
@Override
182195
public int hashCode()
183196
{
184-
return id.hashCode();
197+
return dptId.hashCode();
185198
}
186199
}

test/io/calimero/dptxlator/DPTXlator2ByteFloatTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Calimero 3 - A library for KNX network access
3-
Copyright (c) 2006, 2024 B. Malinowsky
3+
Copyright (c) 2006, 2025 B. Malinowsky
44
55
This program is free software; you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -274,7 +274,7 @@ void lowerUpperLimit(final DPTXlator2ByteFloat t) throws KNXFormatException
274274

275275
@Test
276276
void invalid() {
277-
final DPT invalid = new DPT("0.00", "invalid", "invalid", "invalid", "invalid");
277+
final DPT invalid = new DPT("1.1000", "invalid", "invalid", "invalid", "invalid");
278278
assertThrows(KNXFormatException.class, () -> new DPTXlator2ByteFloat(invalid));
279279
}
280280

0 commit comments

Comments
 (0)