summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java
new file mode 100644
index 00000000000..fc7aec62412
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/engine/Match.java
@@ -0,0 +1,80 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.prelude.semantics.engine;
+
+import com.yahoo.prelude.query.CompositeItem;
+import com.yahoo.prelude.query.Item;
+import com.yahoo.prelude.query.TermItem;
+import com.yahoo.prelude.query.WordItem;
+
+/**
+ * A match
+ *
+ * @author <a href="mailto:bratseth@yahoo-inc.com">Jon S Bratseth</a>
+ */
+public class Match {
+
+ /** The start position of this match */
+ private int position;
+
+ private TermItem item;
+
+ /** The string to replace the match by, usually item.getIndexedString() */
+ private String replaceValue;
+
+ /** The parent of the matched item */
+ private CompositeItem parent=null;
+
+ /**
+ * Creates a match
+ *
+ * @param item the match to add
+ * @param replaceValue the string to replace this match by, usually the item.getIndexedString()
+ * which is what the replace value will be if it is passed as null here
+ */
+ public Match(FlattenedItem item,String replaceValue) {
+ this.item=item.getItem();
+ if (replaceValue==null)
+ this.replaceValue=item.getItem().getIndexedString();
+ else
+ this.replaceValue=replaceValue;
+ this.parent=this.item.getParent();
+ this.position=item.getPosition();
+ }
+
+ public int getPosition() { return position; }
+
+ public TermItem getItem() { return item; }
+
+ public String getReplaceValue() {
+ return replaceValue;
+ }
+
+ /**
+ * Returns the parent in which the item was matched, or null if the item was root.
+ * Note that the item may subsequently have been removed, so it does not necessarily
+ * have this parent
+ */
+ public CompositeItem getParent() { return parent; }
+
+ public int hashCode() {
+ return
+ 17*item.getIndexedString().hashCode()+
+ 33*item.getIndexName().hashCode();
+ }
+
+ /** Returns a new item representing this match */
+ public Item toItem(String label) {
+ return new WordItem(getReplaceValue(),label);
+ }
+
+ public boolean equals(Object o) {
+ if (! (o instanceof Match)) return false;
+
+ Match other=(Match)o;
+ if (other.position!=position) return false;
+ if (!other.item.equals(item)) return false;
+
+ return true;
+ }
+
+}