Skip to content

Commit 675d69c

Browse files
author
Denver
authored
Merge pull request #303 from RachelTucker/bulk_object_equals
BulkObject equals now handles null InCache and Name variables
2 parents 693427a + 94274af commit 675d69c

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

ds3-sdk/src/main/java/com/spectralogic/ds3client/models/BulkObject.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,23 @@ public boolean equals(final Object obj) {
143143

144144
final BulkObject bulkObject = (BulkObject) obj;
145145

146-
return (((this.getId() == null) && (bulkObject.getId() == null))
147-
|| ((this.getId() != null) && (bulkObject.getId() != null) && this.getId().equals(bulkObject.getId())))
148-
&& this.getInCache().equals(bulkObject.getInCache())
146+
return nullableEquals(this.getId(), bulkObject.getId())
147+
&& nullableEquals(this.getInCache(), bulkObject.getInCache())
149148
&& this.getLatest() == bulkObject.getLatest()
150149
&& this.getLength() == bulkObject.getLength()
151-
&& this.getName().equals(bulkObject.getName())
150+
&& nullableEquals(this.getName(), bulkObject.getName())
152151
&& this.getOffset() == bulkObject.getOffset()
153152
&& this.getPhysicalPlacement() == bulkObject.getPhysicalPlacement()
154153
&& this.getVersion() == bulkObject.getVersion();
155154
}
156155

156+
/**
157+
* Tests if two objects are equal, and handles the case if either or both objects are null
158+
*/
159+
protected static boolean nullableEquals(final Object obj1, final Object obj2) {
160+
return obj1 == null && obj2 == null || obj1 != null && obj2 != null && obj1.equals(obj2);
161+
}
162+
157163
@Override
158164
public String toString() {
159165
return String.format("name = %s, offset = %d, length %d", name, offset, length);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.models;
17+
18+
import org.junit.Test;
19+
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
23+
public class ModelFunctionality_Test {
24+
25+
@Test
26+
public void bulkObjectEqualsEmptyObjects_Test() {
27+
final BulkObject obj1 = new BulkObject();
28+
final BulkObject obj2 = new BulkObject();
29+
30+
assertThat(obj1.equals(obj2), is(true));
31+
}
32+
33+
@Test
34+
public void bulkObjectNullableEquals_Test() {
35+
assertThat(BulkObject.nullableEquals(null, null), is(true));
36+
assertThat(BulkObject.nullableEquals("1", null), is(false));
37+
assertThat(BulkObject.nullableEquals("1", "1"), is(true));
38+
assertThat(BulkObject.nullableEquals("1", 1), is(false));
39+
}
40+
}

ds3-sdk/src/test/java/com/spectralogic/ds3client/models/ModelParsing_Test.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
116
package com.spectralogic.ds3client.models;
217

318
import com.spectralogic.ds3client.serializer.XmlOutput;

0 commit comments

Comments
 (0)