diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/result/DefaultErrorHit.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/result/DefaultErrorHit.java | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/result/DefaultErrorHit.java b/container-search/src/main/java/com/yahoo/search/result/DefaultErrorHit.java new file mode 100644 index 00000000000..79b8d55bb07 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/result/DefaultErrorHit.java @@ -0,0 +1,135 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.result; + +import com.yahoo.collections.ArraySet; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * A hit which holds information on error conditions in a result. + * En error hit maintains a main error - the main error of the result. + * + * @author bratseth + * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + */ +public class DefaultErrorHit extends Hit implements ErrorHit, Cloneable { + + /** + * A list of unique error messages, where the first is considered the "main" + * error. It should always contain at least one error. + */ + private List<ErrorMessage> errors = new ArrayList<>(); + + /** + * Creates an error hit with a main error + * + * @param source the name of the source or backend of this hit + * @param error an initial main error to add to this hit, cannot be null + */ + public DefaultErrorHit(String source, ErrorMessage error) { + super("error:" + source, new Relevance(Double.POSITIVE_INFINITY), source); + addError(error); + } + + public void setSource(String source) { + super.setSource(source); + for (Iterator<ErrorMessage> i = errorIterator(); i.hasNext();) { + ErrorMessage error = i.next(); + + if (error.getSource() == null) { + error.setSource(source); + } + } + } + + /** + * Returns the main error of this result, never null. + * + * @deprecated since 5.18, use {@link #errors()} + */ + @Override + public ErrorMessage getMainError() { + return errors.get(0); + } + + /** + * Insert the new "main" error at head of list, remove from the list if it + * already exists elsewhere. + */ + private void removeAndAddAtHead(ErrorMessage mainError) { + errors.remove(mainError); // avoid error duplication + errors.add(0, mainError); + } + + /** + * This is basically a way of making a list simulate a set. + */ + private void removeAndAdd(ErrorMessage error) { + errors.remove(error); + errors.add(error); + } + + /** + * Adds an error to this. This may change the main error + * and/or the list of detailed errors + */ + public void addError(ErrorMessage error) { + if (error.getSource() == null) { + error.setSource(getSource()); + } + removeAndAdd(error); + } + + + /** Add all errors from another error hit to this */ + public void addErrors(ErrorHit errorHit) { + for (Iterator<? extends ErrorMessage> i = errorHit.errorIterator(); i.hasNext();) { + addError(i.next()); + } + } + + /** + * Returns all the detail errors of this error hit, not including the main error. + * The iterator is modifiable. + */ + public Iterator<ErrorMessage> errorIterator() { + return errors.iterator(); + } + + /** Returns a read-only set containing all the error of this */ + public Set<ErrorMessage> errors() { + Set<ErrorMessage> s = new ArraySet<>(errors.size()); + s.addAll(errors); + return s; + } + + public String toString() { + return "Error: " + errors.get(0).toString(); + } + + /** Returns true - this is a meta hit containing information on other hits */ + public boolean isMeta() { + return true; + } + + /** + * Returns true if all errors in this have the given code + */ + public boolean hasOnlyErrorCode(int code) { + for (ErrorMessage error : errors) { + if (error.getCode() != code) + return false; + } + return true; + } + + public DefaultErrorHit clone() { + DefaultErrorHit clone = (DefaultErrorHit) super.clone(); + + clone.errors = new ArrayList<>(this.errors); + return clone; + } + +} |