2929import java .util .logging .Level ;
3030import java .util .logging .Logger ;
3131
32+ import static org .bson .assertions .Assertions .isTrueArgument ;
33+ import static org .bson .assertions .Assertions .notNull ;
34+
3235/**
3336 * <p>A globally unique identifier for objects.</p>
3437 *
@@ -244,10 +247,7 @@ public ObjectId(final String hexString) {
244247 * @throws IllegalArgumentException if array is null or not of length 12
245248 */
246249 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 )));
251251 }
252252
253253 /**
@@ -266,14 +266,11 @@ public ObjectId(final byte[] bytes) {
266266 *
267267 * @param buffer the ByteBuffer
268268 * @throws IllegalArgumentException if the buffer is null or does not have at least 12 bytes remaining
269+ * @since 3.4
269270 */
270271 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 );
277274
278275 // Note: Cannot use ByteBuffer.getInt because it depends on tbe buffer's byte order
279276 // and ObjectId's are always in big-endian order.
@@ -317,14 +314,11 @@ public byte[] toByteArray() {
317314 *
318315 * @param buffer the ByteBuffer
319316 * @throws IllegalArgumentException if the buffer is null or does not have at least 12 bytes remaining
317+ * @since 3.4
320318 */
321319 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 );
328322
329323 buffer .put (int3 (timestamp ));
330324 buffer .put (int2 (timestamp ));
0 commit comments