Use new hash-into over hashValue in Articles.framework. WIP on #402.

This commit is contained in:
Brent Simmons 2018-08-25 12:05:47 -07:00
parent cfb3bd706e
commit a3a9e9c9bd
4 changed files with 55 additions and 33 deletions

View File

@ -27,7 +27,6 @@ public struct Article: Hashable {
public let authors: Set<Author>? public let authors: Set<Author>?
public let attachments: Set<Attachment>? public let attachments: Set<Attachment>?
public let status: ArticleStatus public let status: ArticleStatus
public let hashValue: Int
public init(accountID: String, articleID: String?, feedID: String, uniqueID: String, title: String?, contentHTML: String?, contentText: String?, url: String?, externalURL: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: Set<Author>?, attachments: Set<Attachment>?, status: ArticleStatus) { public init(accountID: String, articleID: String?, feedID: String, uniqueID: String, title: String?, contentHTML: String?, contentText: String?, url: String?, externalURL: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: Set<Author>?, attachments: Set<Attachment>?, status: ArticleStatus) {
@ -54,8 +53,6 @@ public struct Article: Hashable {
else { else {
self.articleID = Article.calculatedArticleID(feedID: feedID, uniqueID: uniqueID) self.articleID = Article.calculatedArticleID(feedID: feedID, uniqueID: uniqueID)
} }
self.hashValue = (accountID + self.articleID).hashValue
} }
public static func calculatedArticleID(feedID: String, uniqueID: String) -> String { public static func calculatedArticleID(feedID: String, uniqueID: String) -> String {
@ -63,9 +60,18 @@ public struct Article: Hashable {
return databaseIDWithString("\(feedID) \(uniqueID)") return databaseIDWithString("\(feedID) \(uniqueID)")
} }
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(accountID)
hasher.combine(articleID)
}
// MARK: - Equatable
public static func ==(lhs: Article, rhs: Article) -> Bool { public static func ==(lhs: Article, rhs: Article) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.articleID == rhs.articleID && lhs.accountID == rhs.accountID && lhs.feedID == rhs.feedID && lhs.uniqueID == rhs.uniqueID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.url == rhs.url && lhs.externalURL == rhs.externalURL && lhs.summary == rhs.summary && lhs.imageURL == rhs.imageURL && lhs.bannerImageURL == rhs.bannerImageURL && lhs.datePublished == rhs.datePublished && lhs.authors == rhs.authors && lhs.attachments == rhs.attachments return lhs.articleID == rhs.articleID && lhs.accountID == rhs.accountID && lhs.feedID == rhs.feedID && lhs.uniqueID == rhs.uniqueID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.url == rhs.url && lhs.externalURL == rhs.externalURL && lhs.summary == rhs.summary && lhs.imageURL == rhs.imageURL && lhs.bannerImageURL == rhs.bannerImageURL && lhs.datePublished == rhs.datePublished && lhs.authors == rhs.authors && lhs.attachments == rhs.attachments
} }
} }

View File

@ -24,7 +24,6 @@ public final class ArticleStatus: Hashable {
public let articleID: String public let articleID: String
public let dateArrived: Date public let dateArrived: Date
public let hashValue: Int
public var read = false public var read = false
public var starred = false public var starred = false
@ -37,7 +36,6 @@ public final class ArticleStatus: Hashable {
self.starred = starred self.starred = starred
self.userDeleted = userDeleted self.userDeleted = userDeleted
self.dateArrived = dateArrived self.dateArrived = dateArrived
self.hashValue = articleID.hashValue
} }
public convenience init(articleID: String, dateArrived: Date) { public convenience init(articleID: String, dateArrived: Date) {
@ -69,9 +67,17 @@ public final class ArticleStatus: Hashable {
} }
} }
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(articleID)
}
// MARK: - Equatable
public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool { public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred && lhs.userDeleted == rhs.userDeleted return lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred && lhs.userDeleted == rhs.userDeleted
} }
} }

View File

@ -16,7 +16,6 @@ public struct Attachment: Hashable {
public let title: String? public let title: String?
public let sizeInBytes: Int? public let sizeInBytes: Int?
public let durationInSeconds: Int? public let durationInSeconds: Int?
public let hashValue: Int
public init(attachmentID: String?, url: String, mimeType: String?, title: String?, sizeInBytes: Int?, durationInSeconds: Int?) { public init(attachmentID: String?, url: String, mimeType: String?, title: String?, sizeInBytes: Int?, durationInSeconds: Int?) {
@ -36,6 +35,10 @@ public struct Attachment: Hashable {
self.durationInSeconds = nil self.durationInSeconds = nil
} }
if let attachmentID = attachmentID {
self.attachmentID = attachmentID
}
else {
var s = url var s = url
s += mimeType ?? "" s += mimeType ?? ""
s += title ?? "" s += title ?? ""
@ -45,18 +48,20 @@ public struct Attachment: Hashable {
if let durationInSeconds = durationInSeconds { if let durationInSeconds = durationInSeconds {
s += "\(durationInSeconds)" s += "\(durationInSeconds)"
} }
self.hashValue = s.hashValue
if let attachmentID = attachmentID {
self.attachmentID = attachmentID
}
else {
self.attachmentID = databaseIDWithString(s) self.attachmentID = databaseIDWithString(s)
} }
} }
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(attachmentID)
}
// MARK: - Equatable
public static func ==(lhs: Attachment, rhs: Attachment) -> Bool { public static func ==(lhs: Attachment, rhs: Attachment) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.attachmentID == rhs.attachmentID return lhs.attachmentID == rhs.attachmentID
} }
} }

View File

@ -15,7 +15,6 @@ public struct Author: Hashable {
public let url: String? public let url: String?
public let avatarURL: String? public let avatarURL: String?
public let emailAddress: String? public let emailAddress: String?
public let hashValue: Int
public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) { public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) {
@ -27,16 +26,14 @@ public struct Author: Hashable {
self.avatarURL = avatarURL self.avatarURL = avatarURL
self.emailAddress = emailAddress self.emailAddress = emailAddress
var s = name ?? ""
s += url ?? ""
s += avatarURL ?? ""
s += emailAddress ?? ""
self.hashValue = s.hashValue
if let authorID = authorID { if let authorID = authorID {
self.authorID = authorID self.authorID = authorID
} }
else { else {
var s = name ?? ""
s += url ?? ""
s += avatarURL ?? ""
s += emailAddress ?? ""
self.authorID = databaseIDWithString(s) self.authorID = databaseIDWithString(s)
} }
} }
@ -76,16 +73,24 @@ public struct Author: Hashable {
return d return d
} }
public static func ==(lhs: Author, rhs: Author) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.authorID == rhs.authorID
}
public static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set<Author>? { public static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set<Author>? {
let authors = diskArray.compactMap { Author(dictionary: $0) } let authors = diskArray.compactMap { Author(dictionary: $0) }
return authors.isEmpty ? nil : Set(authors) return authors.isEmpty ? nil : Set(authors)
} }
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(authorID)
}
// MARK: - Equatable
public static func ==(lhs: Author, rhs: Author) -> Bool {
return lhs.authorID == rhs.authorID
}
} }
extension Set where Element == Author { extension Set where Element == Author {