Skip to content

Commit 1c8add6

Browse files
committed
Merge pull request OfficeDev#335 from vbauer/remove-instanceof
improve ComplexPropertyCollection
2 parents 7a27f9e + 64dc0ed commit 1c8add6

5 files changed

Lines changed: 96 additions & 28 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/property/complex/ComplexProperty.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,18 @@ protected void clearChangeEvents() {
372372
/**
373373
* Implements ISelfValidate.validate. Validates this instance.
374374
*
375-
* @throws ServiceValidationException the service validation exception
376-
* @throws Exception the exception
375+
* @throws Exception the exception
377376
*/
378-
public void validate() throws ServiceValidationException, Exception {
377+
public void validate() throws Exception {
379378
this.internalValidate();
380379
}
381380

382381
/**
383382
* Validates this instance.
384383
*
385-
* @throws ServiceValidationException the service validation exception
386-
* @throws Exception
384+
* @throws Exception the exception
387385
*/
388-
protected void internalValidate() throws ServiceValidationException, Exception {
386+
protected void internalValidate() throws Exception {
389387
}
390388

391389
public Boolean func(EwsServiceXmlReader reader) throws Exception {

src/main/java/microsoft/exchange/webservices/data/property/complex/ComplexPropertyCollection.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@
4848
public abstract class ComplexPropertyCollection
4949
<TComplexProperty extends ComplexProperty>
5050
extends ComplexProperty implements ICustomXmlUpdateSerializer,
51-
Iterable<TComplexProperty>, IComplexPropertyChangedDelegate {
51+
Iterable<TComplexProperty>, IComplexPropertyChangedDelegate<TComplexProperty> {
5252

5353
/**
5454
* The item.
5555
*/
56-
private List<TComplexProperty> items = new ArrayList<TComplexProperty>();
56+
private final List<TComplexProperty> items = new ArrayList<TComplexProperty>();
5757

5858
/**
5959
* The added item.
6060
*/
61-
private List<TComplexProperty> addedItems =
61+
private final List<TComplexProperty> addedItems =
6262
new ArrayList<TComplexProperty>();
6363

6464
/**
6565
* The modified item.
6666
*/
67-
private List<TComplexProperty> modifiedItems =
67+
private final List<TComplexProperty> modifiedItems =
6868
new ArrayList<TComplexProperty>();
6969

7070
/**
7171
* The removed item.
7272
*/
73-
private List<TComplexProperty> removedItems =
73+
private final List<TComplexProperty> removedItems =
7474
new ArrayList<TComplexProperty>();
7575

7676
/**
@@ -101,17 +101,14 @@ protected ComplexPropertyCollection() {
101101
/**
102102
* Item changed.
103103
*
104-
* @param complexProperty The complex property.
104+
* @param property The complex property.
105105
*/
106-
protected void itemChanged(ComplexProperty complexProperty) {
107-
EwsUtilities
108-
.ewsAssert(complexProperty instanceof ComplexProperty, "ComplexPropertyCollection.ItemChanged",
109-
String.format("ComplexPropertyCollection." +
110-
"ItemChanged: the type of " +
111-
"the complexProperty argument " +
112-
"(%s) is not supported.", complexProperty.getClass().getName()));
106+
protected void itemChanged(final TComplexProperty property) {
107+
EwsUtilities.ewsAssert(
108+
property != null, "ComplexPropertyCollection.ItemChanged",
109+
"The complexProperty argument must be not null"
110+
);
113111

114-
TComplexProperty property = (TComplexProperty) complexProperty;
115112
if (!this.addedItems.contains(property)) {
116113
if (!this.modifiedItems.contains(property)) {
117114
this.modifiedItems.add(property);
@@ -334,7 +331,7 @@ private void internalAdd(TComplexProperty complexProperty,
334331
* @param complexProperty accepts ComplexProperty
335332
*/
336333
@Override
337-
public void complexPropertyChanged(ComplexProperty complexProperty) {
334+
public void complexPropertyChanged(final TComplexProperty complexProperty) {
338335
this.itemChanged(complexProperty);
339336
}
340337

src/main/java/microsoft/exchange/webservices/data/property/complex/DictionaryProperty.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
@EditorBrowsable(state = EditorBrowsableState.Never)
5151
public abstract class DictionaryProperty
5252
<TKey, TEntry extends DictionaryEntryProperty<TKey>>
53-
extends ComplexProperty implements ICustomXmlUpdateSerializer, IComplexPropertyChangedDelegate {
53+
extends ComplexProperty implements ICustomXmlUpdateSerializer, IComplexPropertyChangedDelegate<TEntry> {
5454

5555
/**
5656
* The entries.
@@ -77,8 +77,8 @@ public abstract class DictionaryProperty
7777
*
7878
* @param complexProperty the complex property
7979
*/
80-
private void entryChanged(ComplexProperty complexProperty) {
81-
TKey key = ((TEntry) complexProperty).getKey();
80+
private void entryChanged(final TEntry complexProperty) {
81+
TKey key = complexProperty.getKey();
8282

8383
if (!this.addedEntries.contains(key) && !this.modifiedEntries.contains(key)) {
8484
this.modifiedEntries.add(key);
@@ -188,8 +188,8 @@ protected void internalAdd(TEntry entry) {
188188
* @param complexProperty accepts ComplexProperty
189189
*/
190190
@Override
191-
public void complexPropertyChanged(ComplexProperty complexProperty) {
192-
this.entryChanged(complexProperty);
191+
public void complexPropertyChanged(final TEntry complexProperty) {
192+
entryChanged(complexProperty);
193193
}
194194

195195
/**

src/main/java/microsoft/exchange/webservices/data/property/complex/IComplexPropertyChangedDelegate.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
/**
2727
* The Interface ComplexPropertyChangedDelegateInterface.
2828
*/
29-
public interface IComplexPropertyChangedDelegate {
29+
public interface IComplexPropertyChangedDelegate<TComplexProperty extends ComplexProperty> {
3030

3131
/**
3232
* Complex property changed.
3333
*
3434
* @param complexProperty the complex property
3535
*/
36-
void complexPropertyChanged(ComplexProperty complexProperty);
36+
void complexPropertyChanged(TComplexProperty complexProperty);
37+
3738
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* The MIT License
3+
* Copyright (c) 2012 Microsoft Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package microsoft.exchange.webservices.data.property.complex;
25+
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
import java.util.List;
32+
33+
34+
@RunWith(JUnit4.class)
35+
public class ComplexPropertyCollectionTest {
36+
37+
@Test
38+
public void testComplexPropertyChangedPositive() {
39+
final ComplexPropertyCollection<ComplexProperty> collection = createFakeComplexPropertyCollection();
40+
41+
final ComplexProperty property = createFakeComplexProperty();
42+
collection.complexPropertyChanged(property);
43+
44+
final List<ComplexProperty> modifiedItems = collection.getModifiedItems();
45+
Assert.assertTrue(collection.getAddedItems().isEmpty());
46+
Assert.assertTrue(modifiedItems.contains(property));
47+
Assert.assertEquals(1, modifiedItems.size());
48+
}
49+
50+
@Test(expected = RuntimeException.class)
51+
public void testComplexPropertyChangedNegative() {
52+
final ComplexPropertyCollection<ComplexProperty> collection = createFakeComplexPropertyCollection();
53+
collection.complexPropertyChanged(null);
54+
}
55+
56+
57+
private ComplexProperty createFakeComplexProperty() {
58+
return new ComplexProperty() {};
59+
}
60+
61+
private ComplexPropertyCollection<ComplexProperty> createFakeComplexPropertyCollection() {
62+
return new ComplexPropertyCollection<ComplexProperty>() {
63+
@Override protected ComplexProperty createComplexProperty(final String xmlElementName) {
64+
return null;
65+
}
66+
@Override protected String getCollectionItemXmlElementName(final ComplexProperty complexProperty) {
67+
return null;
68+
}
69+
};
70+
}
71+
72+
}

0 commit comments

Comments
 (0)