Skip to content

Commit 8f922fb

Browse files
authored
Add class type to sync statistics (#1105)
1 parent d0c2fac commit 8f922fb

24 files changed

Lines changed: 168 additions & 14 deletions

File tree

src/main/java/com/commercetools/sync/cartdiscounts/helpers/CartDiscountSyncStatistics.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
44

5-
public final class CartDiscountSyncStatistics extends BaseSyncStatistics {
5+
public final class CartDiscountSyncStatistics
6+
extends BaseSyncStatistics<CartDiscountSyncStatistics> {
67

78
/**
89
* Builds a summary of the cart discount sync statistics instance that looks like the following
@@ -17,4 +18,9 @@ public final class CartDiscountSyncStatistics extends BaseSyncStatistics {
1718
public String getReportMessage() {
1819
return getDefaultReportMessageForResource("cart discounts");
1920
}
21+
22+
@Override
23+
protected CartDiscountSyncStatistics getThis() {
24+
return this;
25+
}
2026
}

src/main/java/com/commercetools/sync/categories/helpers/CategorySyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import javax.annotation.Nonnull;
1111
import javax.annotation.Nullable;
1212

13-
public class CategorySyncStatistics extends BaseSyncStatistics {
13+
public class CategorySyncStatistics extends BaseSyncStatistics<CategorySyncStatistics> {
1414

1515
/**
1616
* The following {@link java.util.Map} ({@code categoryKeysWithMissingParents}) represents
@@ -52,6 +52,11 @@ public String getReportMessage() {
5252
getNumberOfCategoriesWithMissingParents());
5353
}
5454

55+
@Override
56+
protected CategorySyncStatistics getThis() {
57+
return this;
58+
}
59+
5560
/**
5661
* Returns the total number of categories with missing parents.
5762
*

src/main/java/com/commercetools/sync/commons/BaseSync.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public abstract class BaseSync<
1515
ResourceT extends BaseResource,
1616
ResourceDraftT,
1717
ResourceUpdateActionT extends ResourceUpdateAction<ResourceUpdateActionT>,
18-
SyncStatisticsT extends BaseSyncStatistics,
19-
SyncOptionsT extends BaseSyncOptions> {
18+
SyncStatisticsT extends BaseSyncStatistics<SyncStatisticsT>,
19+
SyncOptionsT extends BaseSyncOptions<ResourceT, ResourceDraftT, ResourceUpdateActionT>> {
2020
protected final SyncStatisticsT statistics;
2121
protected final SyncOptionsT syncOptions;
2222

src/main/java/com/commercetools/sync/commons/helpers/BaseSyncStatistics.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.concurrent.atomic.AtomicInteger;
77
import javax.annotation.Nonnull;
88

9-
public abstract class BaseSyncStatistics {
9+
public abstract class BaseSyncStatistics<SyncStatisticsT extends BaseSyncStatistics> {
1010
private AtomicInteger updated;
1111
private AtomicInteger created;
1212
private AtomicInteger failed;
@@ -280,6 +280,29 @@ public long getLatestBatchProcessingTimeInMillis() {
280280
*/
281281
public abstract String getReportMessage();
282282

283+
/**
284+
* Gets the name of the implementation class extending this abstract class. This name is then
285+
* serialized into JSON and stored in the custom object of the last sync statistics. Later we
286+
* could use this name to deserialize the custom object into the correct implementation class.
287+
*
288+
* @return name of the implementation class extending this abstract class.
289+
*/
290+
public String getSyncStatisticsClassName() {
291+
return getThis().getClass().getName();
292+
}
293+
294+
/**
295+
* Returns {@code this} instance of {@code T}, which extends {@link
296+
* com.commercetools.sync.commons.helpers.BaseSyncStatistics}. The purpose of this method is to
297+
* make sure that {@code this} is an instance of a class which extends {@link
298+
* com.commercetools.sync.commons.helpers.BaseSyncStatistics} in order to be used in the generic
299+
* methods of the class. Otherwise, without this method, the methods above would need to cast
300+
* {@code this to T} which could lead to a runtime error of the class was extended in a wrong way.
301+
*
302+
* @return an instance of the class that overrides this method.
303+
*/
304+
protected abstract SyncStatisticsT getThis();
305+
283306
/**
284307
* Builds a proper summary message of the statistics report of a given {@code resourceString} in
285308
* following format:

src/main/java/com/commercetools/sync/customers/helpers/CustomerSyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
44

5-
public class CustomerSyncStatistics extends BaseSyncStatistics {
5+
public class CustomerSyncStatistics extends BaseSyncStatistics<CustomerSyncStatistics> {
66

77
/**
88
* Builds a summary of the customer sync statistics instance that looks like the following
@@ -17,4 +17,9 @@ public class CustomerSyncStatistics extends BaseSyncStatistics {
1717
public String getReportMessage() {
1818
return getDefaultReportMessageForResource("customers");
1919
}
20+
21+
@Override
22+
protected CustomerSyncStatistics getThis() {
23+
return this;
24+
}
2025
}

src/main/java/com/commercetools/sync/customobjects/helpers/CustomObjectSyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
44

5-
public class CustomObjectSyncStatistics extends BaseSyncStatistics {
5+
public class CustomObjectSyncStatistics extends BaseSyncStatistics<CustomObjectSyncStatistics> {
66
/**
77
* Builds a summary of the custom object sync statistics instance that looks like the following
88
* example:
@@ -16,4 +16,9 @@ public class CustomObjectSyncStatistics extends BaseSyncStatistics {
1616
public String getReportMessage() {
1717
return getDefaultReportMessageForResource("custom objects");
1818
}
19+
20+
@Override
21+
protected CustomObjectSyncStatistics getThis() {
22+
return this;
23+
}
1924
}

src/main/java/com/commercetools/sync/inventories/helpers/InventorySyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
44

5-
public class InventorySyncStatistics extends BaseSyncStatistics {
5+
public class InventorySyncStatistics extends BaseSyncStatistics<InventorySyncStatistics> {
66

77
public InventorySyncStatistics() {
88
super();
@@ -22,4 +22,9 @@ public InventorySyncStatistics() {
2222
public String getReportMessage() {
2323
return getDefaultReportMessageForResource("inventory entries");
2424
}
25+
26+
@Override
27+
protected InventorySyncStatistics getThis() {
28+
return this;
29+
}
2530
}

src/main/java/com/commercetools/sync/products/helpers/ProductSyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import javax.annotation.Nonnull;
1212
import javax.annotation.Nullable;
1313

14-
public class ProductSyncStatistics extends BaseSyncStatistics {
14+
public class ProductSyncStatistics extends BaseSyncStatistics<ProductSyncStatistics> {
1515

1616
/**
1717
* The following {@link Map} ({@code productKeysWithMissingParents}) represents products with
@@ -47,6 +47,11 @@ public String getReportMessage() {
4747
getNumberOfProductsWithMissingParents());
4848
}
4949

50+
@Override
51+
protected ProductSyncStatistics getThis() {
52+
return this;
53+
}
54+
5055
/**
5156
* Returns the total number of products with missing parents.
5257
*

src/main/java/com/commercetools/sync/producttypes/helpers/ProductTypeSyncStatistics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.concurrent.ConcurrentHashMap;
1414
import javax.annotation.Nonnull;
1515

16-
public class ProductTypeSyncStatistics extends BaseSyncStatistics {
16+
public class ProductTypeSyncStatistics extends BaseSyncStatistics<ProductTypeSyncStatistics> {
1717

1818
/**
1919
* The following {@link java.util.concurrent.ConcurrentHashMap} ({@code
@@ -82,6 +82,11 @@ public String getReportMessage() {
8282
getNumberOfProductTypesWithMissingNestedProductTypes());
8383
}
8484

85+
@Override
86+
protected ProductTypeSyncStatistics getThis() {
87+
return this;
88+
}
89+
8590
/**
8691
* Returns the total number of product types with at least one NestedType or a Set of NestedType
8792
* attribute definition(s) referencing a missing productType.

src/main/java/com/commercetools/sync/shoppinglists/helpers/ShoppingListSyncStatistics.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
44

5-
public final class ShoppingListSyncStatistics extends BaseSyncStatistics {
5+
public final class ShoppingListSyncStatistics
6+
extends BaseSyncStatistics<ShoppingListSyncStatistics> {
67

78
/**
89
* Builds a summary of the shopping list sync statistics instance that looks like the following
@@ -17,4 +18,9 @@ public final class ShoppingListSyncStatistics extends BaseSyncStatistics {
1718
public String getReportMessage() {
1819
return getDefaultReportMessageForResource("shopping lists");
1920
}
21+
22+
@Override
23+
protected ShoppingListSyncStatistics getThis() {
24+
return this;
25+
}
2026
}

0 commit comments

Comments
 (0)