| 1 | package be.nikiroo.fanfix.searchable; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | /** |
| 7 | * This class represents a tag that can be searched on a supported website. |
| 8 | * |
| 9 | * @author niki |
| 10 | */ |
| 11 | public class SearchableTag { |
| 12 | private String id; |
| 13 | private String name; |
| 14 | private boolean complete; |
| 15 | private long count; |
| 16 | private List<SearchableTag> children; |
| 17 | |
| 18 | /** |
| 19 | * Create a new {@link SearchableTag}. |
| 20 | * |
| 21 | * @param id |
| 22 | * the ID (usually a way to find the linked stories later on) |
| 23 | * @param name |
| 24 | * the tag name, which can be displayed to the user |
| 25 | * @param complete |
| 26 | * TRUE for a {@link SearchableTag} that cannot be "filled" by |
| 27 | * the {@link BasicSearchable} in order to get (more?) subtag |
| 28 | * children |
| 29 | */ |
| 30 | public SearchableTag(String id, String name, boolean complete) { |
| 31 | this.id = id; |
| 32 | this.name = name; |
| 33 | this.complete = complete; |
| 34 | |
| 35 | children = new ArrayList<SearchableTag>(); |
| 36 | } |
| 37 | |
| 38 | public String getId() { |
| 39 | return id; |
| 40 | } |
| 41 | |
| 42 | public String getName() { |
| 43 | return name; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * This tag can still be completed via a "fill" tag operation from a |
| 48 | * {@link BasicSearchable}, in order to gain (more?) subtag children. |
| 49 | * |
| 50 | * @return TRUE if it can |
| 51 | */ |
| 52 | public boolean isComplete() { |
| 53 | return complete; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * This tag can still be completed via a "fill" tag operation from a |
| 58 | * {@link BasicSearchable}, in order to gain (more?) subtag children. |
| 59 | * |
| 60 | * @param complete |
| 61 | * TRUE if it can |
| 62 | */ |
| 63 | public void setComplete(boolean complete) { |
| 64 | this.complete = complete; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * The number of items that can be found with this tag if it is searched. |
| 69 | * <p> |
| 70 | * Will report the number of subtags by default. |
| 71 | * |
| 72 | * @return the number of items |
| 73 | */ |
| 74 | public long getCount() { |
| 75 | long count = this.count; |
| 76 | if (count <= 0) { |
| 77 | count = children.size(); |
| 78 | } |
| 79 | |
| 80 | return count; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * The number of items that can be found with this tag if it is searched, |
| 85 | * displayable format. |
| 86 | * <p> |
| 87 | * Will report the number of subtags by default. |
| 88 | * |
| 89 | * @return the number of items |
| 90 | */ |
| 91 | public String getCountDisplay() { |
| 92 | long count = this.count; |
| 93 | if (count <= 0) { |
| 94 | count = children.size(); |
| 95 | } |
| 96 | |
| 97 | if (count > 999999) { |
| 98 | return count / 1000000 + "M"; |
| 99 | } |
| 100 | |
| 101 | if (count > 2000) { |
| 102 | return count / 1000 + "k"; |
| 103 | } |
| 104 | |
| 105 | return Long.toString(count); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * The number of items that can be found with this tag if it is searched. |
| 110 | * |
| 111 | * @param count |
| 112 | * the new count |
| 113 | */ |
| 114 | public void setCount(long count) { |
| 115 | this.count = count; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * The subtag children of this {@link SearchableTag}. |
| 120 | * <p> |
| 121 | * Never NULL. |
| 122 | * <p> |
| 123 | * Note that if {@link SearchableTag#isComplete()} returns false, you can |
| 124 | * still fill (more?) subtag children with a {@link BasicSearchable}. |
| 125 | * |
| 126 | * @return the subtag children, never NULL |
| 127 | */ |
| 128 | public List<SearchableTag> getChildren() { |
| 129 | return children; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Add the given {@link SearchableTag} as a subtag child. |
| 134 | * |
| 135 | * @param tag |
| 136 | * the tag to add |
| 137 | */ |
| 138 | public void add(SearchableTag tag) { |
| 139 | children.add(tag); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Display a DEBUG {@link String} representation of this object. |
| 144 | */ |
| 145 | @Override |
| 146 | public String toString() { |
| 147 | String rep = name + " [" + id + "]"; |
| 148 | if (!complete) { |
| 149 | rep += "*"; |
| 150 | } |
| 151 | |
| 152 | if (getCount() > 0) { |
| 153 | rep += " (" + getCountDisplay() + ")"; |
| 154 | } |
| 155 | |
| 156 | if (!children.isEmpty()) { |
| 157 | String tags = ""; |
| 158 | int i = 1; |
| 159 | for (SearchableTag tag : children) { |
| 160 | if (!tags.isEmpty()) { |
| 161 | tags += ", "; |
| 162 | } |
| 163 | |
| 164 | if (i > 10) { |
| 165 | tags += "..."; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | tags += tag; |
| 170 | i++; |
| 171 | } |
| 172 | |
| 173 | rep += ": " + tags; |
| 174 | } |
| 175 | |
| 176 | return rep; |
| 177 | } |
| 178 | } |