11package io .intercom .api ;
22
3+ import com .google .common .base .Objects ;
4+ import com .google .common .base .Predicate ;
5+ import com .google .common .collect .Iterables ;
36import com .google .common .collect .Lists ;
47
5- import java .util .Iterator ;
68import java .util .List ;
79
810class CompanyUpdateBuilder {
@@ -16,40 +18,34 @@ static List<CompanyWithStringPlan> buildUserUpdateCompanies(CompanyCollection ad
1618 if (add != null ) {
1719 final List <Company > companies = add .getPage ();
1820 for (Company company : companies ) {
19- if ( remove != null && ! remove . getPage (). contains ( company )) {
21+ if (! isCompanyInList ( company , remove )) {
2022 updatableCompanies .add (prepareUpdatableCompany (company ));
2123 }
2224 }
2325 }
2426
2527 if (remove != null ) {
2628 final List <Company > companies = remove .getPage ();
27-
2829 for (Company company : companies ) {
29- removeCompanyFromAddedList (updatableCompanies , company );
3030 updatableCompanies .add (prepareUpdatableCompany (company ).setRemove (Boolean .TRUE ));
3131 }
3232 }
3333
3434 return updatableCompanies ;
3535 }
3636
37- private static void removeCompanyFromAddedList (List <CompanyWithStringPlan > companies , Company company ) {
38- Iterator <CompanyWithStringPlan > companyIterator = companies .iterator ();
39-
40- while (companyIterator .hasNext ()) {
41- CompanyWithStringPlan companyWithStringPlan = companyIterator .next ();
42- // TODO add equals and toHash methods to CompanyWithStringPlan and use it here
43- if (companyWithStringPlan .getId () != null && company .getId () != null && companyWithStringPlan .getId ().equals (company .getId ())) {
44- companyIterator .remove ();
45- break ;
46- }
37+ private static boolean isCompanyInList (final Company company , CompanyCollection companyCollection ) {
38+ if (companyCollection == null ) {
39+ return false ;
40+ }
4741
48- if (companyWithStringPlan .getCompanyID () != null && company .getCompanyID () != null && companyWithStringPlan .getCompanyID ().equals (company .getCompanyID ())) {
49- companyIterator .remove ();
50- break ;
42+ return Iterables .any (companyCollection .getPage (), new Predicate <Company >() {
43+ @ Override
44+ public boolean apply (Company e ) {
45+ return Objects .equal (company .getCompanyID (), e .getCompanyID ())
46+ || Objects .equal (company .getId (), e .getId ());
5147 }
52- }
48+ });
5349 }
5450
5551 private static CompanyWithStringPlan prepareUpdatableCompany (Company company ) {
@@ -60,7 +56,7 @@ private static CompanyWithStringPlan prepareUpdatableCompany(Company company) {
6056 updatableCompany .setSessionCount (company .getSessionCount ());
6157 updatableCompany .setMonthlySpend (company .getMonthlySpend ());
6258 updatableCompany .setRemoteCreatedAt (company .getRemoteCreatedAt ());
63- if (company .getCustomAttributes () != null ) {
59+ if (company .getCustomAttributes () != null ) {
6460 updatableCompany .getCustomAttributes ().putAll (company .getCustomAttributes ());
6561 }
6662 if (company .getPlan () != null ) {
0 commit comments