search: .equals
authorNiki Roo <niki@nikiroo.be>
Wed, 17 Apr 2019 16:39:43 +0000 (18:39 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 17 Apr 2019 16:39:43 +0000 (18:39 +0200)
src/be/nikiroo/fanfix/searchable/SearchableTag.java

index 338f84daa950b39eb4cac62ec487120738faa35b..de8679834f5fa20a72826fd6deb89cf405613de9 100644 (file)
@@ -104,7 +104,7 @@ public class SearchableTag {
                        return parent.getFqName() + " / " + name;
                }
 
-               return name;
+               return "" + name;
        }
 
        /**
@@ -232,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;
        }
@@ -281,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;
+       }
 }