Skip to content

Commit 0e77bad

Browse files
BoxStoreBuilder: extract directory state checks.
1 parent 835b9c1 commit 0e77bad

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ private BoxStoreBuilder() {
135135
/** Called internally from the generated class "MyObjectBox". Check MyObjectBox.builder() to get an instance. */
136136
@Internal
137137
public BoxStoreBuilder(byte[] model) {
138+
// Note: annotations do not guarantee parameter is non-null.
139+
//noinspection ConstantValue
138140
if (model == null) {
139141
throw new IllegalArgumentException("Model may not be null");
140142
}
@@ -150,9 +152,7 @@ public BoxStoreBuilder(byte[] model) {
150152
* Default: "objectbox", {@link #DEFAULT_NAME} (unless {@link #directory(File)} is used)
151153
*/
152154
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");
156156
if (name.contains("/") || name.contains("\\")) {
157157
throw new IllegalArgumentException("Name may not contain (back) slashes. " +
158158
"Use baseDirectory() or directory() to configure alternative directories");
@@ -179,11 +179,9 @@ public BoxStoreBuilder name(String name) {
179179
* Can not be used in combination with {@link #name(String)} or {@link #baseDirectory(File)}.
180180
*/
181181
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");
187185
}
188186
this.directory = directory;
189187
return this;
@@ -195,13 +193,21 @@ public BoxStoreBuilder directory(File directory) {
195193
* Cannot be used in combination with {@link #directory(File)}.
196194
*/
197195
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");
201197
this.baseDirectory = baseDirectory;
202198
return this;
203199
}
204200

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+
205211
/**
206212
* On Android, you can pass a Context to set the base directory using this method.
207213
* This will conveniently configure the storage location to be in the files directory of your app.

0 commit comments

Comments
 (0)