diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java index 71d090736fe..aaa4d33c6dc 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/CompositeItem.java @@ -74,9 +74,8 @@ public abstract class CompositeItem extends Item { * @throws IndexOutOfBoundsException if the index is out of range */ public void addItem(int index, Item item) { - if (index > subitems.size() || index < 0) { + if (index > subitems.size() || index < 0) throw new IndexOutOfBoundsException("Could not add a subitem at position " + index + " to " + this); - } adding(item); subitems.add(index, item); } @@ -97,7 +96,7 @@ public abstract class CompositeItem extends Item { } /** - * Replaces the item at the given index + * Replaces the item at the given index. * * @param index the (0-base) index of the item to replace * @param item the new item @@ -118,7 +117,7 @@ public abstract class CompositeItem extends Item { /** * Returns the index of a subitem * - * @param item The child item to find the index of + * @param item the child item to find the index of * @return the 0-base index of the child or -1 if there is no such child */ public int getItemIndex(Item item) { @@ -218,6 +217,7 @@ public abstract class CompositeItem extends Item { } /** Returns a deep copy of this item */ + @Override public CompositeItem clone() { CompositeItem copy = (CompositeItem) super.clone(); @@ -271,12 +271,11 @@ public abstract class CompositeItem extends Item { return -1; } + @Override public int hashCode() { int code = getName().hashCode() + subitems.size() * 17; - - for (int i = 0; i < subitems.size() && i <= 5; i++) { + for (int i = 0; i < subitems.size() && i <= 5; i++) code += subitems.get(i).hashCode(); - } return code; } @@ -284,17 +283,12 @@ public abstract class CompositeItem extends Item { * Returns whether this item is of the same class and * contains the same state as the given item */ + @Override public boolean equals(Object object) { - if (!super.equals(object)) { - return false; - } + if (!super.equals(object)) return false; CompositeItem other = (CompositeItem) object; // Ensured by superclass - - if (!this.subitems.equals(other.subitems)) { - return false; - } - + if ( ! this.subitems.equals(other.subitems)) return false; return true; } @@ -306,12 +300,30 @@ public abstract class CompositeItem extends Item { return false; } + @Override + public int getTermCount() { + int terms = 0; + for (Item item : subitems) { + terms += item.getTermCount(); + } + return terms; + } + + /** + * Will return its single child if itself can safely be omitted. + * + * @return a valid Item or empty Optional if it can not be done + */ + public Optional<Item> extractSingleChild() { + return getItemCount() == 1 ? Optional.of(getItem(0)) : Optional.empty(); + } + /** Handles mutator calls correctly */ private static class ListIteratorWrapper implements ListIterator<Item> { - private CompositeItem owner; + private final CompositeItem owner; - private ListIterator<Item> wrapped; + private final ListIterator<Item> wrapped; private Item current = null; @@ -320,47 +332,54 @@ public abstract class CompositeItem extends Item { wrapped = owner.subitems.listIterator(); } + @Override public boolean hasNext() { return wrapped.hasNext(); } + @Override public Item next() { current = wrapped.next(); return current; } + @Override public boolean hasPrevious() { return wrapped.hasPrevious(); } + @Override public Item previous() { - Item current = wrapped.previous(); - + current = wrapped.previous(); return current; } + @Override public int nextIndex() { return wrapped.nextIndex(); } + @Override public int previousIndex() { return wrapped.previousIndex(); } + @Override public void remove() { owner.removing(current); wrapped.remove(); } + @Override public void set(Item o) { Item newItem = o; - owner.removing(current); owner.adding(newItem); current = newItem; wrapped.set(newItem); } + @Override public void add(Item o) { Item newItem = o; @@ -371,22 +390,4 @@ public abstract class CompositeItem extends Item { } - @Override - public int getTermCount() { - int terms = 0; - for (Item item : subitems) { - terms += item.getTermCount(); - } - return terms; - } - - /** - * Will return its single child if itself can safely be omitted. - * - * @return a valid Item or empty Optional if it can not be done - */ - public Optional<Item> extractSingleChild() { - return getItemCount() == 1 ? Optional.of(getItem(0)) : Optional.empty(); - } - } |