|
20 | 20 | * @author Brian Wellington |
21 | 21 | */ |
22 | 22 | @EqualsAndHashCode(of = {"rrs", "sigs"}) |
23 | | -public class RRset implements Serializable { |
| 23 | +public class RRset implements Serializable, Iterable<Record> { |
24 | 24 | private final ArrayList<Record> rrs; |
25 | 25 | private final ArrayList<RRSIGRecord> sigs; |
26 | 26 | private short position; |
@@ -51,6 +51,20 @@ public RRset(Record... records) { |
51 | 51 | } |
52 | 52 | } |
53 | 53 |
|
| 54 | + /** |
| 55 | + * Creates an RRset and sets its contents to the specified record(s) |
| 56 | + * |
| 57 | + * @param records The records to add to the set. See {@link #addRR(Record)} for restrictions. |
| 58 | + * @since 3.6 |
| 59 | + */ |
| 60 | + public RRset(Iterable<Record> records) { |
| 61 | + this(); |
| 62 | + Objects.requireNonNull(records); |
| 63 | + for (Record r : records) { |
| 64 | + addRR(r); |
| 65 | + } |
| 66 | + } |
| 67 | + |
54 | 68 | /** Creates an RRset with the contents of an existing RRset */ |
55 | 69 | public RRset(RRset rrset) { |
56 | 70 | rrs = new ArrayList<>(rrset.rrs); |
@@ -191,11 +205,30 @@ public List<RRSIGRecord> sigs() { |
191 | 205 | return Collections.unmodifiableList(sigs); |
192 | 206 | } |
193 | 207 |
|
194 | | - /** Returns the number of (data) records */ |
| 208 | + /** Returns the number of data records. */ |
195 | 209 | public int size() { |
196 | 210 | return rrs.size(); |
197 | 211 | } |
198 | 212 |
|
| 213 | + /** |
| 214 | + * Returns the number of signature records. |
| 215 | + * |
| 216 | + * @since 3.6 |
| 217 | + */ |
| 218 | + public int sigSize() { |
| 219 | + return sigs.size(); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Returns {@code true} if this RRset is empty, i.e. if there are neither data nor signature |
| 224 | + * records. |
| 225 | + * |
| 226 | + * @since 3.6 |
| 227 | + */ |
| 228 | + public boolean isEmpty() { |
| 229 | + return rrs.isEmpty() && sigs.isEmpty(); |
| 230 | + } |
| 231 | + |
199 | 232 | /** |
200 | 233 | * Returns the name of the records |
201 | 234 | * |
@@ -289,4 +322,15 @@ public String toString() { |
289 | 322 | sb.append(" }"); |
290 | 323 | return sb.toString(); |
291 | 324 | } |
| 325 | + |
| 326 | + /** |
| 327 | + * Returns an {@link Iterator} over the resource records. This is a convenience method / interface |
| 328 | + * implementation and equivalent to calling {@code rrs().iterator()}. |
| 329 | + * |
| 330 | + * @since 3.6 |
| 331 | + */ |
| 332 | + @Override |
| 333 | + public Iterator<Record> iterator() { |
| 334 | + return rrs().iterator(); |
| 335 | + } |
292 | 336 | } |
0 commit comments