1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2014 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.je.utilint;
9 
10 import com.sleepycat.je.DatabaseEntry;
11 
12 /**
13  * Utils for use in the db package.
14  */
15 public class DatabaseUtil {
16 
17     /*
18      * The global JE test mode flag.  When true, certain instrumentation is
19      * turned on.  This flag is always true during unit testing.
20      */
21     public static final boolean TEST = Boolean.getBoolean("JE_TEST");
22 
23     /**
24      * Throw an exception if the parameter is null.
25      *
26      * @throws IllegalArgumentException via any API method
27      */
checkForNullParam(final Object param, final String name)28     public static void checkForNullParam(final Object param,
29                                          final String name) {
30         if (param == null) {
31             throw new IllegalArgumentException(name + " cannot be null");
32         }
33     }
34 
35     /**
36      * Throw an exception if the parameter is a null or 0-length array.
37      *
38      * @throws IllegalArgumentException via any API method
39      */
checkForZeroLengthArrayParam(final Object[] param, final String name)40     public static void checkForZeroLengthArrayParam(final Object[] param,
41                                                     final String name) {
42         checkForNullParam(param, name);
43         if (param.length == 0) {
44             throw new IllegalArgumentException(name + " cannot be zero length");
45         }
46     }
47 
48     /**
49      * Throw an exception if the entry is null or the data field is not set.
50      *
51      * @throws IllegalArgumentException via any API method that takes a
52      * required DatabaseEntry param
53      */
checkForNullDbt(final DatabaseEntry entry, final String name, final boolean checkData)54     public static void checkForNullDbt(final DatabaseEntry entry,
55                                        final String name,
56                                        final boolean checkData) {
57         if (entry == null) {
58             throw new IllegalArgumentException
59                 ("DatabaseEntry " + name + " cannot be null");
60         }
61 
62         if (checkData) {
63             if (entry.getData() == null) {
64                 throw new IllegalArgumentException
65                     ("Data field for DatabaseEntry " +
66                      name + " cannot be null");
67             }
68         }
69     }
70 
71     /**
72      * Throw an exception if the key entry has the partial flag set.  This
73      * method should be called for all put() operations.
74      *
75      * @throws IllegalArgumentException via put methodx
76      */
checkForPartialKey(final DatabaseEntry entry)77     public static void checkForPartialKey(final DatabaseEntry entry) {
78         if (entry.getPartial()) {
79             throw new IllegalArgumentException
80                 ("A partial key DatabaseEntry is not allowed");
81         }
82     }
83 }
84