Skip to content

Commit

Permalink
In lib/src/ref_entities overrode == and hashCode for all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
orthros committed Aug 7, 2018
1 parent a4d1855 commit 0148b23
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 31 deletions.
42 changes: 32 additions & 10 deletions lib/src/ref_entities/epub_book_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,57 @@ import 'dart:async';

import 'package:archive/archive.dart';
import 'package:image/image.dart';
import 'package:quiver/collection.dart' as collections;
import 'package:quiver/core.dart';

import 'epub_chapter_ref.dart';
import 'epub_content_ref.dart';
import '../entities/epub_schema.dart';
import '../readers/book_cover_reader.dart';
import '../readers/chapter_reader.dart';
import 'epub_chapter_ref.dart';
import 'epub_content_ref.dart';

class EpubBookRef {
Archive _epubArchive;

EpubBookRef(Archive epubArchive) {
this._epubArchive = epubArchive;
}

String Title;
String Author;
List<String> AuthorList;
EpubSchema Schema;
EpubContentRef Content;
EpubBookRef(Archive epubArchive) {
this._epubArchive = epubArchive;
}

Archive EpubArchive() {
return _epubArchive;
@override
int get hashCode => hashObjects([
Title.hashCode,
Author.hashCode,
AuthorList.hashCode,
Schema.hashCode,
Content.hashCode
]);

bool operator ==(other) {
var otherAs = other as EpubBookRef;
if (otherAs == null) {
return false;
}
return Title == otherAs.Title &&
Author == otherAs.Author &&
Schema == otherAs.Schema &&
Content == otherAs.Content &&
collections.listsEqual(AuthorList, otherAs.AuthorList);
}

Future<Image> readCover() async {
return await BookCoverReader.readBookCover(this);
Archive EpubArchive() {
return _epubArchive;
}

Future<List<EpubChapterRef>> getChapters() async {
return await ChapterReader.getChapters(this);
}

Future<Image> readCover() async {
return await BookCoverReader.readBookCover(this);
}
}
1 change: 1 addition & 0 deletions lib/src/ref_entities/epub_byte_content_file_ref.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'epub_book_ref.dart';
import 'epub_content_file_ref.dart';

Expand Down
32 changes: 28 additions & 4 deletions lib/src/ref_entities/epub_chapter_ref.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import 'dart:async';

import 'package:quiver/collection.dart' as collections;
import 'package:quiver/core.dart';

import 'epub_text_content_file_ref.dart';

class EpubChapterRef {
EpubTextContentFileRef epubTextContentFileRef;

EpubChapterRef(EpubTextContentFileRef epubTextContentFileRef) {
this.epubTextContentFileRef = epubTextContentFileRef;
}

String Title;
String ContentFileName;
String Anchor;
List<EpubChapterRef> SubChapters;

EpubChapterRef(EpubTextContentFileRef epubTextContentFileRef) {
this.epubTextContentFileRef = epubTextContentFileRef;
}

@override
int get hashCode => hashObjects([
Title.hashCode,
ContentFileName.hashCode,
Anchor.hashCode,
epubTextContentFileRef.hashCode,
SubChapters.hashCode
]);

bool operator ==(other) {
var otherAs = other as EpubChapterRef;
if (otherAs == null) {
return false;
}
return Title == otherAs.Title &&
ContentFileName == otherAs.ContentFileName &&
Anchor == otherAs.Anchor &&
epubTextContentFileRef == otherAs.epubTextContentFileRef &&
collections.listsEqual(SubChapters, otherAs.SubChapters);
}

Future<String> readHtmlContent() async {
return epubTextContentFileRef.readContentAsText();
}
Expand Down
45 changes: 28 additions & 17 deletions lib/src/ref_entities/epub_content_file_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@ import 'dart:async';

import 'package:archive/archive.dart';
import 'package:dart2_constant/convert.dart' as convert;
import 'package:quiver/core.dart';

import 'epub_book_ref.dart';
import '../entities/epub_content_type.dart';
import '../utils/zip_path_utils.dart';
import 'epub_book_ref.dart';

abstract class EpubContentFileRef {
EpubBookRef epubBookRef;

EpubContentFileRef(EpubBookRef epubBookRef) {
this.epubBookRef = epubBookRef;
}

String FileName;

EpubContentType ContentType;
String ContentMimeType;

Future<List<int>> readContentAsBytes() async {
ArchiveFile contentFileEntry = getContentFileEntry();
var content = openContentStream(contentFileEntry);
return content;
EpubContentFileRef(EpubBookRef epubBookRef) {
this.epubBookRef = epubBookRef;
}

Future<String> readContentAsText() async {
List<int> contentStream = getContentStream();
String result = convert.utf8.decode(contentStream);
return result;
}
@override
int get hashCode => hash3(FileName.hashCode, ContentMimeType.hashCode,ContentType.hashCode);

List<int> getContentStream() {
return openContentStream(getContentFileEntry());
bool operator ==(other) {
return (other is EpubContentFileRef &&
other.FileName == FileName &&
other.ContentMimeType == ContentMimeType &&
other.ContentType == ContentType);
}

ArchiveFile getContentFileEntry() {
Expand All @@ -46,6 +41,10 @@ abstract class EpubContentFileRef {
return contentFileEntry;
}

List<int> getContentStream() {
return openContentStream(getContentFileEntry());
}

List<int> openContentStream(ArchiveFile contentFileEntry) {
List<int> contentStream = new List<int>();
if (contentFileEntry.content == null)
Expand All @@ -54,4 +53,16 @@ abstract class EpubContentFileRef {
contentStream.addAll(contentFileEntry.content);
return contentStream;
}

Future<List<int>> readContentAsBytes() async {
ArchiveFile contentFileEntry = getContentFileEntry();
var content = openContentStream(contentFileEntry);
return content;
}

Future<String> readContentAsText() async {
List<int> contentStream = getContentStream();
String result = convert.utf8.decode(contentStream);
return result;
}
}
25 changes: 25 additions & 0 deletions lib/src/ref_entities/epub_content_ref.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:quiver/collection.dart' as collections;
import 'package:quiver/core.dart';

import 'epub_byte_content_file_ref.dart';
import 'epub_content_file_ref.dart';
import 'epub_text_content_file_ref.dart';
Expand All @@ -8,4 +11,26 @@ class EpubContentRef {
Map<String, EpubByteContentFileRef> Images;
Map<String, EpubByteContentFileRef> Fonts;
Map<String, EpubContentFileRef> AllFiles;

@override
int get hashCode => hashObjects([
Html.hashCode,
Css.hashCode,
Images.hashCode,
Fonts.hashCode,
AllFiles.hashCode
]);

bool operator ==(other) {
var otherAs = other as EpubContentRef;
if (otherAs == null) {
return false;
}

return collections.mapsEqual(Html, otherAs.Html) &&
collections.mapsEqual(Css, otherAs.Css) &&
collections.mapsEqual(Images, otherAs.Images) &&
collections.mapsEqual(Fonts, otherAs.Fonts) &&
collections.mapsEqual(AllFiles, otherAs.AllFiles);
}
}

0 comments on commit 0148b23

Please sign in to comment.