Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / searchable / SearchableTag.java
index f73dd15d7ce627fc3d3f7a72eb16959db52c96a6..de8679834f5fa20a72826fd6deb89cf405613de9 100644 (file)
@@ -67,7 +67,7 @@ public class SearchableTag {
        public SearchableTag(String id, String name, boolean leaf, boolean complete) {
                this.id = id;
                this.name = name;
-               this.complete = complete;
+               this.complete = leaf || complete;
 
                setLeaf(leaf);
 
@@ -103,8 +103,8 @@ public class SearchableTag {
                if (parent != null) {
                        return parent.getFqName() + " / " + name;
                }
-               
-               return name;
+
+               return "" + name;
        }
 
        /**
@@ -112,7 +112,7 @@ public class SearchableTag {
         * {@link BasicSearchable#fillTag(SearchableTag)} operation from a
         * {@link BasicSearchable}, in order to gain (more?) subtag children.
         * <p>
-        * This method does not make sense for leaf tags.
+        * Leaf tags are always considered complete.
         * 
         * @return TRUE if it is complete
         */
@@ -125,13 +125,13 @@ public class SearchableTag {
         * {@link BasicSearchable#fillTag(SearchableTag)} operation from a
         * {@link BasicSearchable}, in order to gain (more?) subtag children.
         * <p>
-        * This method does not make sense for leaf tags.
+        * Leaf tags are always considered complete.
         * 
         * @param complete
         *            TRUE if it is complete
         */
        public void setComplete(boolean complete) {
-               this.complete = complete;
+               this.complete = isLeaf() || complete;
        }
 
        /**
@@ -206,6 +206,9 @@ public class SearchableTag {
         */
        public void setLeaf(boolean leaf) {
                pages = leaf ? -1 : -2;
+               if (leaf) {
+                       complete = true;
+               }
        }
 
        /**
@@ -229,6 +232,23 @@ public class SearchableTag {
         *            the tag to add
         */
        public void add(SearchableTag tag) {
+               if (tag == null) {
+                       throw new NullPointerException("tag");
+               }
+
+               for (SearchableTag p = this; p != null; p = p.parent) {
+                       if (p.equals(tag)) {
+                               throw new IllegalArgumentException(
+                                               "Tags do not allow recursion");
+                       }
+               }
+               for (SearchableTag p = tag; p != null; p = p.parent) {
+                       if (p.equals(this)) {
+                               throw new IllegalArgumentException(
+                                               "Tags do not allow recursion");
+                       }
+               }
+
                children.add(tag);
                tag.parent = this;
        }
@@ -278,4 +298,27 @@ public class SearchableTag {
 
                return rep;
        }
+
+       @Override
+       public int hashCode() {
+               return getFqName().hashCode();
+       }
+
+       @Override
+       public boolean equals(Object otherObj) {
+               if (otherObj instanceof SearchableTag) {
+                       SearchableTag other = (SearchableTag) otherObj;
+                       if ((id == null && other.id == null)
+                                       || (id != null && id.equals(other.id))) {
+                               if (getFqName().equals(other.getFqName())) {
+                                       if ((parent == null && other.parent == null)
+                                                       || (parent != null && parent.equals(other.parent))) {
+                                               return true;
+                                       }
+                               }
+                       }
+               }
+
+               return false;
+       }
 }