1 | /* This file is part of Aard Dictionary for Android <http://aarddict.org>. |
2 | * |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License version 3 |
5 | * as published by the Free Software Foundation. |
6 | * |
7 | * This program is distributed in the hope that it will be useful, |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * GNU General Public License <http://www.gnu.org/licenses/gpl-3.0.txt> |
11 | * for more details. |
12 | * |
13 | * Copyright (C) 2010 Igor Tkach |
14 | */ |
15 | |
16 | package aarddict; |
17 | |
18 | import java.io.IOException; |
19 | import java.io.Serializable; |
20 | import java.util.Map; |
21 | import java.util.UUID; |
22 | |
23 | public final class Article implements Serializable { |
24 | |
25 | public UUID dictionaryUUID; |
26 | public String volumeId; |
27 | public String title; |
28 | public String section; |
29 | public long pointer; |
30 | private String redirect; |
31 | public String text; |
32 | public String redirectedFromTitle; |
33 | |
34 | public Article() { |
35 | } |
36 | |
37 | public Article(Article that) { |
38 | this.dictionaryUUID = that.dictionaryUUID; |
39 | this.volumeId = that.volumeId; |
40 | this.title = that.title; |
41 | this.section = that.section; |
42 | this.pointer = that.pointer; |
43 | this.redirect = that.redirect; |
44 | this.text = that.text; |
45 | this.redirectedFromTitle = that.redirectedFromTitle; |
46 | } |
47 | |
48 | @SuppressWarnings("unchecked") |
49 | static Article fromJsonStr(String serializedArticle) throws IOException { |
50 | Object[] articleTuple = Volume.mapper.readValue(serializedArticle, Object[].class); |
51 | Article article = new Article(); |
52 | article.text = String.valueOf(articleTuple[0]); |
53 | if (articleTuple.length == 3) { |
54 | Map metadata = (Map)articleTuple[2]; |
55 | if (metadata.containsKey("r")) { |
56 | article.redirect = String.valueOf(metadata.get("r")); |
57 | } |
58 | else if (metadata.containsKey("redirect")) { |
59 | article.redirect = String.valueOf(metadata.get("redirect")); |
60 | } |
61 | } |
62 | return article; |
63 | } |
64 | |
65 | public String getRedirect() { |
66 | if (this.redirect != null && this.section != null) { |
67 | return this.redirect + "#" + this.section; |
68 | } |
69 | return this.redirect; |
70 | } |
71 | |
72 | public boolean isRedirect() { |
73 | return this.redirect != null; |
74 | } |
75 | |
76 | public boolean equalsIgnoreSection(Article other) { |
77 | return volumeId.equals(other.volumeId) && pointer == other.pointer; |
78 | } |
79 | |
80 | public boolean sectionEquals(Article other) { |
81 | return (section == null && other.section == null) || |
82 | (section !=null && other.section != null && section.equals(other.section)); |
83 | } |
84 | |
85 | @Override |
86 | public int hashCode() { |
87 | final int prime = 31; |
88 | int result = 1; |
89 | result = prime * result + (int) (pointer ^ (pointer >>> 32)); |
90 | result = prime * result + ((section == null) ? 0 : section.hashCode()); |
91 | result = prime * result |
92 | + ((volumeId == null) ? 0 : volumeId.hashCode()); |
93 | return result; |
94 | } |
95 | |
96 | @Override |
97 | public boolean equals(Object obj) { |
98 | if (this == obj) |
99 | return true; |
100 | if (obj == null) |
101 | return false; |
102 | if (getClass() != obj.getClass()) |
103 | return false; |
104 | Article other = (Article) obj; |
105 | if (pointer != other.pointer) |
106 | return false; |
107 | if (section == null) { |
108 | if (other.section != null) |
109 | return false; |
110 | } else if (!section.equals(other.section)) |
111 | return false; |
112 | if (volumeId == null) { |
113 | if (other.volumeId != null) |
114 | return false; |
115 | } else if (!volumeId.equals(other.volumeId)) |
116 | return false; |
117 | return true; |
118 | } |
119 | |
120 | |
121 | } |