29
29
import java .util .logging .Level ;
30
30
import java .util .logging .Logger ;
31
31
32
+ import static org .bson .assertions .Assertions .isTrueArgument ;
33
+ import static org .bson .assertions .Assertions .notNull ;
34
+
32
35
/**
33
36
* <p>A globally unique identifier for objects.</p>
34
37
*
@@ -244,10 +247,7 @@ public ObjectId(final String hexString) {
244
247
* @throws IllegalArgumentException if array is null or not of length 12
245
248
*/
246
249
public ObjectId (final byte [] bytes ) {
247
- // This null check allows the delegate constructor to throw the expected IllegalArgumentException.
248
- // (ByteBuffer.wrap throws NullPointerException if bytes is null, which violates ObjectId's contract
249
- // to callers.)
250
- this (bytes != null ? ByteBuffer .wrap (bytes ) : null );
250
+ this (ByteBuffer .wrap (notNull ("bytes" , bytes )));
251
251
}
252
252
253
253
/**
@@ -266,14 +266,11 @@ public ObjectId(final byte[] bytes) {
266
266
*
267
267
* @param buffer the ByteBuffer
268
268
* @throws IllegalArgumentException if the buffer is null or does not have at least 12 bytes remaining
269
+ * @since 3.4
269
270
*/
270
271
public ObjectId (final ByteBuffer buffer ) {
271
- if (buffer == null ) {
272
- throw new IllegalArgumentException ();
273
- }
274
- if (buffer .remaining () < 12 ) {
275
- throw new IllegalArgumentException ("need 12 bytes" );
276
- }
272
+ notNull ("buffer" , buffer );
273
+ isTrueArgument ("buffer.remaining() >=12" , buffer .remaining () >= 12 );
277
274
278
275
// Note: Cannot use ByteBuffer.getInt because it depends on tbe buffer's byte order
279
276
// and ObjectId's are always in big-endian order.
@@ -317,14 +314,11 @@ public byte[] toByteArray() {
317
314
*
318
315
* @param buffer the ByteBuffer
319
316
* @throws IllegalArgumentException if the buffer is null or does not have at least 12 bytes remaining
317
+ * @since 3.4
320
318
*/
321
319
public void putToByteBuffer (final ByteBuffer buffer ) {
322
- if (buffer == null ) {
323
- throw new IllegalArgumentException ();
324
- }
325
- if (buffer .remaining () < 12 ) {
326
- throw new IllegalArgumentException ("need 12 bytes" );
327
- }
320
+ notNull ("buffer" , buffer );
321
+ isTrueArgument ("buffer.remaining() >=12" , buffer .remaining () >= 12 );
328
322
329
323
buffer .put (int3 (timestamp ));
330
324
buffer .put (int2 (timestamp ));
0 commit comments