Override hashCode() and equals() in Phrase.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
index d50a254..c1b7c3c 100644 (file)
@@ -175,11 +175,20 @@ public class SearchPage extends SoneTemplatePage {
                List<Phrase> phrases = new ArrayList<Phrase>();
                for (String phrase : parsedPhrases) {
                        if (phrase.startsWith("+")) {
-                               phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
+                               if (phrase.length() > 1) {
+                                       phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
+                               } else {
+                                       phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
+                               }
                        } else if (phrase.startsWith("-")) {
-                               phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
+                               if (phrase.length() > 1) {
+                                       phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
+                               } else {
+                                       phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
+                               }
+                       } else {
+                               phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
                        }
-                       phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
                }
                return phrases;
        }
@@ -404,6 +413,30 @@ public class SearchPage extends SoneTemplatePage {
                        return optionality;
                }
 
+               //
+               // OBJECT METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public int hashCode() {
+                       return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public boolean equals(Object object) {
+                       if (!(object instanceof Phrase)) {
+                               return false;
+                       }
+                       Phrase phrase = (Phrase) object;
+                       return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
+               }
+
        }
 
        /**