// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import com.google.common.annotations.Beta;
import com.yahoo.prelude.Location;
import java.nio.ByteBuffer;

/**
 * This represents a geo-location for matching.
 * Note that this won't produce summary fields.
 *
 * @author  arnej
 */
@Beta
public class GeoLocationItem extends TermItem {

    private Location location;

    /**
     */
    public GeoLocationItem(Location location) {
        this(location, location.getAttribute());
        if (! location.hasAttribute()) {
            throw new IllegalArgumentException("missing attribute on location: "+location);
        }
    }

    /**
     */
    public GeoLocationItem(Location location, String indexName) {
        super(indexName, false);
        if (location.hasAttribute() && ! location.getAttribute().equals(indexName)) {
            throw new IllegalArgumentException("inconsistent attribute on location: "+location.getAttribute()+" versus indexName: "+indexName);
        }
        if (! location.isGeoCircle()) {
            throw new IllegalArgumentException("GeoLocationItem only supports Geo Circles, got: "+location);
        }
        if (location.hasBoundingBox()) {
            throw new IllegalArgumentException("GeoLocationItem does not support bounding box yet, got: "+location);
        }
        this.location = new Location(location.toString());
        this.location.setAttribute(null); // keep this in indexName only
        setNormalizable(false);
    }

    public Location getLocation() {
        return location;
    }

    @Override
    public String getRawWord() {
        return stringValue();
    }

    @Override
    public ItemType getItemType() {
        return ItemType.GEO_LOCATION_TERM;
    }

    @Override
    public String getName() {
        return "GEO_LOCATION";
    }

    @Override
    public String stringValue() {
        return location.toString();
    }

    @Override
    public void setValue(String value) {
        throw new UnsupportedOperationException("Cannot setValue("+value+") on "+getName());
    }

    @Override
    public int hashCode() {
        return super.hashCode() + 199 * location.hashCode();
    }

    @Override
    public boolean equals(Object object) {
        if ( ! super.equals(object)) return false;
        GeoLocationItem other = (GeoLocationItem) object; // Ensured by superclass
        if ( ! location.equals(other.location)) return false;
        return true;
    }

    @Override
    public String getIndexedString() {
        return location.toString();
    }

    @Override
    protected void encodeThis(ByteBuffer buffer) {
        super.encodeThis(buffer); // takes care of index bytes
        putString(location.backendString(), buffer);
    }

    @Override
    public int getNumWords() {
        return 1;
    }

    @Override
    public boolean isStemmed() {
        return true;
    }

    @Override
    public boolean isWords() {
        return false;
    }

}

@@ -10,27 +10,21 @@
 ght 
-2017 Yahoo Holdings
+Verizon Media
 . Li
@@ -106,16 +106,17 @@
 t root.%0A
+%0A
 package 
@@ -290,72 +290,98 @@
 ion 
-for matching.%0A * Note that this won't produce summary fields.%0A *
+in the query tree.%0A * Used for closeness(fieldname) and distance(fieldname) rank features.
 %0A * 
@@ -388,17 +388,16 @@
 @author 
- 
 arnej%0A *
@@ -491,32 +491,115 @@
 %0A%0A    /**%0A     *
+ Construct from a Location, which must be geo circle with an attribute set.%0A     **
 /%0A    public Geo
@@ -845,16 +845,185 @@
 *%0A     *
+ Construct from a Location and a field name.%0A     * The Location must be a geo circle.%0A     * If the Location has an attribute set, it must match the field name.%0A     **
 /%0A    pu
@@ -1069,21 +1069,21 @@
  String 
-index
+field
 Name) %7B%0A
@@ -1096,21 +1096,21 @@
   super(
-index
+field
 Name, fa
@@ -1187,21 +1187,21 @@
 .equals(
-index
+field
 Name)) %7B
@@ -1324,21 +1324,21 @@
 sus 
-index
+field
 Name: %22+
 inde
@@ -1333,21 +1333,21 @@
 Name: %22+
-index
+field
 Name);%0A 
@@ -1737,24 +1737,24 @@
 oString());%0A
-
         this
@@ -1798,16 +1798,29 @@
  this in
+ (superclass)
  indexNa

fix copyright and comments