Skip to content

Commit

Permalink
Merge pull request #2870 from wordpress-mobile/analysis/add-missing-n…
Browse files Browse the repository at this point in the history
…ullability-annotations-to-all-comment-sqlutils-model-classes

[Nullability Annotations to Java Classes] Add Missing Nullability Annotations to All `Comment` SqlUtils & Model Classes (`breaking`)
  • Loading branch information
ParaskP7 authored Oct 19, 2023
2 parents 0867b48 + 5317567 commit 263e700
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.wordpress.android.fluxc.model;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yarolegovich.wellsql.core.Identifiable;
import com.yarolegovich.wellsql.core.annotation.Column;
import com.yarolegovich.wellsql.core.annotation.PrimaryKey;
Expand All @@ -11,6 +14,7 @@
import java.io.Serializable;

@Table
@SuppressWarnings("NotNullFieldNotInitialized")
public class CommentModel extends Payload<BaseNetworkError> implements Identifiable, Serializable {
private static final long serialVersionUID = 3454722213760369852L;

Expand All @@ -23,19 +27,19 @@ public class CommentModel extends Payload<BaseNetworkError> implements Identifia
@Column private long mRemoteSiteId;

// Comment author
@Column private String mAuthorUrl;
@Column private String mAuthorName;
@Column private String mAuthorEmail;
@Nullable @Column private String mAuthorUrl;
@Nullable @Column private String mAuthorName;
@Nullable @Column private String mAuthorEmail;
@Column private long mAuthorId;
@Column private String mAuthorProfileImageUrl;
@Nullable @Column private String mAuthorProfileImageUrl;

// Comment data
@Column private String mPostTitle;
@Column private String mStatus;
@Column private String mDatePublished;
@Nullable @Column private String mPostTitle;
@NonNull @Column private String mStatus;
@NonNull @Column private String mDatePublished;
@Column private long mPublishedTimestamp;
@Column private String mContent;
@Column private String mUrl;
@NonNull @Column private String mContent;
@NonNull @Column private String mUrl;

// Parent Comment Data
@Column private boolean mHasParent;
Expand All @@ -54,9 +58,6 @@ public void setId(int id) {
mId = id;
}

// not stored in db - denotes the hierarchical level of this comment
public transient int level = 0;

public long getRemoteCommentId() {
return mRemoteCommentId;
}
Expand All @@ -73,67 +74,75 @@ public void setRemotePostId(long remotePostId) {
mRemotePostId = remotePostId;
}

@Nullable
public String getAuthorUrl() {
return mAuthorUrl;
}

public void setAuthorUrl(String authorUrl) {
public void setAuthorUrl(@Nullable String authorUrl) {
this.mAuthorUrl = authorUrl;
}

@Nullable
public String getAuthorName() {
return mAuthorName;
}

public void setAuthorName(String authorName) {
public void setAuthorName(@Nullable String authorName) {
this.mAuthorName = authorName;
}

@Nullable
public String getAuthorEmail() {
return mAuthorEmail;
}

public void setAuthorEmail(String authorEmail) {
public void setAuthorEmail(@Nullable String authorEmail) {
this.mAuthorEmail = authorEmail;
}

@Nullable
public String getAuthorProfileImageUrl() {
return mAuthorProfileImageUrl;
}

public void setAuthorProfileImageUrl(String authorProfileImageUrl) {
public void setAuthorProfileImageUrl(@Nullable String authorProfileImageUrl) {
this.mAuthorProfileImageUrl = authorProfileImageUrl;
}

@Nullable
public String getPostTitle() {
return mPostTitle;
}

public void setPostTitle(String postTitle) {
public void setPostTitle(@Nullable String postTitle) {
this.mPostTitle = postTitle;
}

@NonNull
public String getStatus() {
return mStatus;
}

public void setStatus(String status) {
public void setStatus(@NonNull String status) {
this.mStatus = status;
}

@NonNull
public String getDatePublished() {
return mDatePublished;
}

public void setDatePublished(String datePublished) {
public void setDatePublished(@NonNull String datePublished) {
this.mDatePublished = datePublished;
}

@NonNull
public String getContent() {
return mContent;
}

public void setContent(String content) {
public void setContent(@NonNull String content) {
this.mContent = content;
}

Expand Down Expand Up @@ -169,11 +178,12 @@ public void setILike(boolean iLike) {
mILike = iLike;
}

@NonNull
public String getUrl() {
return mUrl;
}

public void setUrl(String url) {
public void setUrl(@NonNull String url) {
mUrl = url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class CommentsMapper @Inject constructor(
this.authorEmail = entity.authorEmail
this.authorProfileImageUrl = entity.authorProfileImageUrl
this.postTitle = entity.postTitle
this.status = entity.status
this.datePublished = entity.datePublished
this.status = entity.status ?: ""
this.datePublished = entity.datePublished ?: ""
this.publishedTimestamp = entity.publishedTimestamp
this.content = entity.content
this.url = entity.url
this.content = entity.content ?: ""
this.url = entity.url ?: ""
this.hasParent = entity.hasParent
this.parentId = entity.parentId
this.iLike = entity.iLike
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import static org.wordpress.android.fluxc.model.LikeModel.TIMESTAMP_THRESHOLD;

public class CommentSqlUtils {
public static int insertOrUpdateComment(CommentModel comment) {
public static int insertOrUpdateComment(@Nullable CommentModel comment) {
if (comment == null) {
return 0;
}
Expand Down Expand Up @@ -62,7 +61,7 @@ public static int insertOrUpdateComment(CommentModel comment) {
}
}

public static int removeComment(CommentModel comment) {
public static int removeComment(@Nullable CommentModel comment) {
if (comment == null) {
return 0;
}
Expand All @@ -72,29 +71,26 @@ public static int removeComment(CommentModel comment) {
.execute();
}

public static int removeComments(SiteModel site) {
if (site == null) {
return 0;
}

public static int removeComments(@NonNull SiteModel site) {
return WellSql.delete(CommentModel.class)
.where().equals(CommentModelTable.LOCAL_SITE_ID, site.getId()).endWhere()
.execute();
}

public static int removeCommentGaps(SiteModel site, List<CommentModel> comments, int maxEntriesInResponse,
int requestOffset, @Nullable CommentStatus... statuses) {
if (site == null || comments == null || comments.isEmpty()) {
public static int removeCommentGaps(
@NonNull SiteModel site,
@NonNull List<CommentModel> comments,
int maxEntriesInResponse,
int requestOffset,
@Nullable CommentStatus... statuses) {
if (comments.isEmpty()) {
return 0;
}

Collections.sort(comments, new Comparator<CommentModel>() {
@Override
public int compare(CommentModel o1, CommentModel o2) {
long x = o2.getPublishedTimestamp();
long y = o1.getPublishedTimestamp();
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
comments.sort((o1, o2) -> {
long x = o2.getPublishedTimestamp();
long y = o1.getPublishedTimestamp();
return Long.compare(x, y);
});

ArrayList<Long> remoteIds = new ArrayList<>();
Expand All @@ -105,12 +101,18 @@ public int compare(CommentModel o1, CommentModel o2) {
long startOfRange = comments.get(0).getPublishedTimestamp();
long endOfRange = comments.get(comments.size() - 1).getPublishedTimestamp();

List<CommentStatus> sourceStatuses;
if (statuses != null) {
sourceStatuses = Arrays.asList(statuses);
} else {
sourceStatuses = Collections.emptyList();
}
ArrayList<CommentStatus> targetStatuses = new ArrayList<>();
if (Arrays.asList(statuses).contains(CommentStatus.ALL)) {
if (sourceStatuses.contains(CommentStatus.ALL)) {
targetStatuses.add(CommentStatus.APPROVED);
targetStatuses.add(CommentStatus.UNAPPROVED);
} else {
targetStatuses.addAll(Arrays.asList(statuses));
targetStatuses.addAll(sourceStatuses);
}

int numOfDeletedComments = 0;
Expand Down Expand Up @@ -155,6 +157,7 @@ public static int deleteAllComments() {
return WellSql.delete(CommentModel.class).execute();
}

@Nullable
public static CommentModel getCommentByLocalCommentId(int localId) {
List<CommentModel> results = WellSql.select(CommentModel.class)
.where().equals(CommentModelTable.ID, localId).endWhere().getAsModel();
Expand All @@ -177,16 +180,18 @@ public static CommentModel getCommentBySiteAndRemoteId(@NonNull SiteModel site,
return results.get(0);
}

private static SelectQuery<CommentModel> getCommentsQueryForSite(SiteModel site, CommentStatus... statuses) {
@NonNull
private static SelectQuery<CommentModel> getCommentsQueryForSite(
@NonNull SiteModel site,
@NonNull CommentStatus... statuses) {
return getCommentsQueryForSite(site, 0, statuses);
}

private static SelectQuery<CommentModel> getCommentsQueryForSite(SiteModel site, int limit,
CommentStatus... statuses) {
if (site == null) {
return null;
}

@NonNull
private static SelectQuery<CommentModel> getCommentsQueryForSite(
@NonNull SiteModel site,
int limit,
@NonNull CommentStatus... statuses) {
SelectQuery<CommentModel> query = WellSql.select(CommentModel.class);

if (limit > 0) {
Expand All @@ -204,29 +209,32 @@ private static SelectQuery<CommentModel> getCommentsQueryForSite(SiteModel site,
return selectQueryBuilder.endGroup().endWhere();
}

public static List<CommentModel> getCommentsForSite(SiteModel site, @Order int order, CommentStatus... statuses) {
@NonNull
public static List<CommentModel> getCommentsForSite(
@NonNull SiteModel site,
@Order int order,
@NonNull CommentStatus... statuses) {
return getCommentsForSite(site, order, 0, statuses);
}

public static List<CommentModel> getCommentsForSite(SiteModel site, @Order int order, int limit,
CommentStatus... statuses) {
if (site == null) {
return Collections.emptyList();
}

@NonNull
public static List<CommentModel> getCommentsForSite(
@NonNull SiteModel site,
@Order int order,
int limit,
@NonNull CommentStatus... statuses) {
return getCommentsQueryForSite(site, limit, statuses)
.orderBy(CommentModelTable.DATE_PUBLISHED, order)
.getAsModel();
}

public static int getCommentsCountForSite(SiteModel site, CommentStatus... statuses) {
if (site == null) {
return 0;
}

public static int getCommentsCountForSite(
@NonNull SiteModel site,
@NonNull CommentStatus... statuses) {
return (int) getCommentsQueryForSite(site, statuses).count();
}

@SuppressWarnings("resource")
public static int deleteCommentLikesAndPurgeExpired(long siteId, long remoteCommentId) {
int numDeleted = WellSql.delete(LikeModel.class)
.where()
Expand Down Expand Up @@ -272,11 +280,10 @@ public static int deleteCommentLikesAndPurgeExpired(long siteId, long remoteComm
return numDeleted;
}

public static int insertOrUpdateCommentLikes(long siteId, long remoteCommentId, LikeModel like) {
if (null == like) {
return 0;
}

public static int insertOrUpdateCommentLikes(
long siteId,
long remoteCommentId,
@NonNull LikeModel like) {
List<LikeModel> likeResult;

// If the like already exists and has an id, we want to update it.
Expand All @@ -300,6 +307,7 @@ public static int insertOrUpdateCommentLikes(long siteId, long remoteCommentId,
}
}

@NonNull
public static List<LikeModel> getCommentLikesByCommentId(long siteId, long remoteCommentId) {
return WellSql.select(LikeModel.class)
.where()
Expand Down
Loading

0 comments on commit 263e700

Please sign in to comment.