@@ -135,6 +135,8 @@ private BoxStoreBuilder() {
135
135
/** Called internally from the generated class "MyObjectBox". Check MyObjectBox.builder() to get an instance. */
136
136
@ Internal
137
137
public BoxStoreBuilder (byte [] model ) {
138
+ // Note: annotations do not guarantee parameter is non-null.
139
+ //noinspection ConstantValue
138
140
if (model == null ) {
139
141
throw new IllegalArgumentException ("Model may not be null" );
140
142
}
@@ -150,9 +152,7 @@ public BoxStoreBuilder(byte[] model) {
150
152
* Default: "objectbox", {@link #DEFAULT_NAME} (unless {@link #directory(File)} is used)
151
153
*/
152
154
public BoxStoreBuilder name (String name ) {
153
- if (directory != null ) {
154
- throw new IllegalArgumentException ("Already has directory, cannot assign name" );
155
- }
155
+ checkIsNull (directory , "Already has directory, cannot assign name" );
156
156
if (name .contains ("/" ) || name .contains ("\\ " )) {
157
157
throw new IllegalArgumentException ("Name may not contain (back) slashes. " +
158
158
"Use baseDirectory() or directory() to configure alternative directories" );
@@ -179,11 +179,9 @@ public BoxStoreBuilder name(String name) {
179
179
* Can not be used in combination with {@link #name(String)} or {@link #baseDirectory(File)}.
180
180
*/
181
181
public BoxStoreBuilder directory (File directory ) {
182
- if (name != null ) {
183
- throw new IllegalArgumentException ("Already has name, cannot assign directory" );
184
- }
185
- if (!android && baseDirectory != null ) {
186
- throw new IllegalArgumentException ("Already has base directory, cannot assign directory" );
182
+ checkIsNull (name , "Already has name, cannot assign directory" );
183
+ if (!android ) {
184
+ checkIsNull (baseDirectory , "Already has base directory, cannot assign directory" );
187
185
}
188
186
this .directory = directory ;
189
187
return this ;
@@ -195,13 +193,21 @@ public BoxStoreBuilder directory(File directory) {
195
193
* Cannot be used in combination with {@link #directory(File)}.
196
194
*/
197
195
public BoxStoreBuilder baseDirectory (File baseDirectory ) {
198
- if (directory != null ) {
199
- throw new IllegalArgumentException ("Already has directory, cannot assign base directory" );
200
- }
196
+ checkIsNull (directory , "Already has directory, cannot assign base directory" );
201
197
this .baseDirectory = baseDirectory ;
202
198
return this ;
203
199
}
204
200
201
+ /**
202
+ * Use to check conflicting properties are not set.
203
+ * If not null, throws {@link IllegalStateException} with the given message.
204
+ */
205
+ private static void checkIsNull (@ Nullable Object value , String errorMessage ) {
206
+ if (value != null ) {
207
+ throw new IllegalStateException (errorMessage );
208
+ }
209
+ }
210
+
205
211
/**
206
212
* On Android, you can pass a Context to set the base directory using this method.
207
213
* This will conveniently configure the storage location to be in the files directory of your app.
0 commit comments