/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.document;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.ShapeField.QueryRelation;
import org.apache.lucene.geo.Component2D;
import org.apache.lucene.geo.Polygon;

/** random bounding box, line, and polygon query tests for random indexed arrays of {@link Polygon} types */
public class TestLatLonMultiPolygonShapeQueries extends BaseLatLonShapeTestCase {

  @Override
  protected ShapeType getShapeType() {
    return ShapeType.POLYGON;
  }

  @Override
  protected Polygon[] nextShape() {
    int n = random().nextInt(4) + 1;
    Polygon[] polygons = new Polygon[n];
    for (int i =0; i < n; i++) {
      polygons[i] = (Polygon) getShapeType().nextShape();
    }
    return polygons;
  }
  
  @Override
  protected Field[] createIndexableFields(String name, Object o) {
    Polygon[] polygons = (Polygon[]) o;
    List<Field> allFields = new ArrayList<>();
    for (Polygon polygon : polygons) {
      Field[] fields = LatLonShape.createIndexableFields(name, polygon);
      for (Field field : fields) {
        allFields.add(field);
      }
    }
    return allFields.toArray(new Field[allFields.size()]);
  }

  @Override
  protected Validator getValidator() {
    return new MultiPolygonValidator(ENCODER);
  }

  protected class MultiPolygonValidator extends Validator {
    TestLatLonPolygonShapeQueries.PolygonValidator POLYGONVALIDATOR;

    MultiPolygonValidator(Encoder encoder) {
      super(encoder);
      POLYGONVALIDATOR = new TestLatLonPolygonShapeQueries.PolygonValidator(encoder);
    }

    @Override
    public Validator setRelation(QueryRelation relation) {
      super.setRelation(relation);
      POLYGONVALIDATOR.queryRelation = relation;
      return this;
    }

    @Override
    public boolean testComponentQuery(Component2D query, Object shape) {
      Polygon[] polygons = (Polygon[]) shape;
      if (queryRelation == QueryRelation.CONTAINS) {
        return testWithinPolygon(query, polygons);
      }
      for (Polygon p : polygons) {
        boolean b = POLYGONVALIDATOR.testComponentQuery(query, p);
        if (b == true && queryRelation == QueryRelation.INTERSECTS) {
          return true;
        } else if (b == true && queryRelation == QueryRelation.CONTAINS) {
          return true;
        } else if (b == false && queryRelation == QueryRelation.DISJOINT) {
          return false;
        } else if (b == false && queryRelation == QueryRelation.WITHIN) {
          return false;
        }
      }
      return queryRelation != QueryRelation.INTERSECTS && queryRelation != QueryRelation.CONTAINS;
    }

    private boolean testWithinPolygon(Component2D query, Polygon[] polygons) {
      Component2D.WithinRelation answer = Component2D.WithinRelation.DISJOINT;
      for (Polygon p : polygons) {
        Component2D.WithinRelation relation = POLYGONVALIDATOR.testWithinPolygon(query, p);
        if (relation == Component2D.WithinRelation.NOTWITHIN) {
          return false;
        } else if (relation == Component2D.WithinRelation.CANDIDATE) {
          answer = relation;
        }
      }
      return answer == Component2D.WithinRelation.CANDIDATE;
    }
  }

  @Slow
  @Nightly
  @Override
  public void testRandomBig() throws Exception {
    doTestRandom(10000);
  }
}

@@ -1472,19 +1472,67 @@
     
-p
+int repetitions = 0;%0A      while (true) %7B%0A        P
 olygon
-s%5Bi%5D
+ p
  = (
@@ -1576,29 +1576,1050 @@
     
-%7D%0A    return polygons
+    // polygons are disjoint so CONTAINS works. Note that if we intersect%0A        // any shape then contains return false.%0A        if (isDisjoint(polygons, p)) %7B%0A          polygons%5Bi%5D = p;%0A          break;%0A        %7D%0A        repetitions++;%0A        if (repetitions %3E 50) %7B%0A          // try again%0A          return nextShape();%0A        %7D%0A      %7D%0A    %7D%0A    return polygons;%0A  %7D%0A%0A  private boolean isDisjoint(Polygon%5B%5D polygons, Polygon check) %7B%0A    // we use bounding boxes so we do not get intersecting polygons.%0A    for (Polygon polygon : polygons) %7B%0A      if (polygon != null) %7B%0A        if (getEncoder().quantizeY(polygon.minLat) %3E getEncoder().quantizeY(check.maxLat)%0A                %7C%7C getEncoder().quantizeY(polygon.maxLat) %3C getEncoder().quantizeY(check.minLat)%0A                %7C%7C getEncoder().quantizeX(polygon.minLon) %3E getEncoder().quantizeX(check.maxLon)%0A                %7C%7C getEncoder().quantizeX(polygon.maxLon) %3C getEncoder().quantizeX(check.minLon)) %7B%0A          continue;%0A        %7D%0A        return false;%0A      %7D%0A    %7D%0A    return true
 ;%0A  

Index disjoint multipolygons in TestLatLonMultiPolygonShapeQueries