Skip to content

Commit

Permalink
Merge pull request #340 from zeev-js/main
Browse files Browse the repository at this point in the history
IndexOutOfBoundsException checks in sample code
  • Loading branch information
vestrel00 authored Jan 18, 2024
2 parents f5095ff + 62e228a commit ebdd6bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
29 changes: 16 additions & 13 deletions docs/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ heading explore each API in full detail. You may also find these samples in the

public class QueryContactsAdvancedActivity extends Activity {

@Nullable
Contact getContactById(Long contactId) {
return ContactsFactory.create(this)
Query.Result result = ContactsFactory.create(this)
.query()
.where(
equalTo(Fields.Contact.Id, contactId)
)
.find()
.get(0);
.where(equalTo(Fields.Contact.Id, contactId))
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}

List<Contact> getContactByLookupKey(String lookupKey) {
Expand Down Expand Up @@ -486,17 +486,18 @@ heading explore each API in full detail. You may also find these samples in the
.where(isNotNullOrEmpty(Fields.Note.Note))
.find();
}

@Nullable
RawContact getRawContactById(Long rawContactId) {
return ContactsFactory.create(this)
RawContactsQuery.Result result = ContactsFactory.create(this)
.rawContactsQuery()
.rawContactsWhere(
new ArrayList<>(),
equalTo(RawContactsFields.Id, rawContactId)
)
// alternatively, .where(equalTo(Fields.RawContact.Id, rawContactId))
.find()
.get(0);
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}
}
```
Expand Down Expand Up @@ -1111,8 +1112,9 @@ heading explore each API in full detail. You may also find these samples in the
.find();
}

@Nullable
Event getContactBirthday(Long contactId) {
return ContactsFactory.create(this)
DataQuery.Result<Event> result = ContactsFactory.create(this)
.data()
.query()
.events()
Expand All @@ -1122,8 +1124,9 @@ heading explore each API in full detail. You may also find these samples in the
equalTo(Fields.Event.Type, EventEntity.Type.BIRTHDAY)
)
)
.find()
.get(0);
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

public class QueryContactsAdvancedActivity extends Activity {

@Nullable
Contact getContactById(Long contactId) {
return ContactsFactory.create(this)
Query.Result result = ContactsFactory.create(this)
.query()
.where(
equalTo(Fields.Contact.Id, contactId)
)
.find()
.get(0);
.where(equalTo(Fields.Contact.Id, contactId))
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}

List<Contact> getContactByLookupKey(String lookupKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ List<RawContact> getRawContactsThatHasANote() {
.where(isNotNullOrEmpty(Fields.Note.Note))
.find();
}


@Nullable
RawContact getRawContactById(Long rawContactId) {
return ContactsFactory.create(this)
RawContactsQuery.Result result = ContactsFactory.create(this)
.rawContactsQuery()
.rawContactsWhere(
new ArrayList<>(),
equalTo(RawContactsFields.Id, rawContactId)
)
// alternatively, .where(equalTo(Fields.RawContact.Id, rawContactId))
.find()
.get(0);
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ List<Relation> getUpTo10Mothers() {
.find();
}

@Nullable
Event getContactBirthday(Long contactId) {
return ContactsFactory.create(this)
DataQuery.Result<Event> result = ContactsFactory.create(this)
.data()
.query()
.events()
Expand All @@ -62,7 +63,8 @@ Event getContactBirthday(Long contactId) {
equalTo(Fields.Event.Type, EventEntity.Type.BIRTHDAY)
)
)
.find()
.get(0);
.limit(1)
.find();
return !result.isEmpty() ? result.get(0) : null;
}
}

0 comments on commit ebdd6bc

Please sign in to comment.