<commit_before>/**
 * 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 com.googlecode.lucene.spatial.base.shape;

import org.apache.lucene.spatial.base.IntersectCase;
import org.apache.lucene.spatial.base.context.SpatialContext;
import org.apache.lucene.spatial.base.shape.BBox;
import org.apache.lucene.spatial.base.shape.Point;
import org.apache.lucene.spatial.base.shape.Shape;
import org.apache.lucene.spatial.base.shape.simple.Point2D;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

public class JtsEnvelope implements BBox {

  public final Envelope envelope;

  public JtsEnvelope(Envelope envelope) {
    this.envelope = envelope;
  }

  public JtsEnvelope(double x1, double x2, double y1, double y2) {
    this.envelope = new Envelope(x1, x2, y1, y2);
  }

  @Override
  public boolean hasArea() {
    return getWidth() > 0 && getHeight() > 0;
  }

  public double getArea() {
    return getWidth() * getHeight();
  }

  public boolean getCrossesDateLine() {
    return false;
  }

  @Override
  public double getHeight() {
    return envelope.getWidth();
  }

  @Override
  public double getWidth() {
    return envelope.getHeight();
  }

  @Override
  public double getMaxX() {
    return envelope.getMaxX();
  }

  @Override
  public double getMaxY() {
    return envelope.getMaxY();
  }

  @Override
  public double getMinX() {
    return envelope.getMinX();
  }

  @Override
  public double getMinY() {
    return envelope.getMinY();
  }

  @Override
  public boolean hasSize() {
    return !envelope.isNull();
  }

  //----------------------------------------
  //----------------------------------------

  @Override
  public JtsEnvelope getBoundingBox() {
    return this;
  }

  @Override
  public IntersectCase intersect(Shape other, SpatialContext context) {
    if (BBox.class.isInstance(other)) {
      BBox ext = other.getBoundingBox();
      if (ext.getMinX() > envelope.getMaxX() ||
          ext.getMaxX() < envelope.getMinX() ||
          ext.getMinY() > envelope.getMaxY() ||
          ext.getMaxY() < envelope.getMinY()) {
        return IntersectCase.OUTSIDE;
      }

      if (ext.getMinX() >= envelope.getMinX() &&
          ext.getMaxX() <= envelope.getMaxX() &&
          ext.getMinY() >= envelope.getMinY() &&
          ext.getMaxY() <= envelope.getMaxY()) {
        return IntersectCase.CONTAINS;
      }

      if (envelope.getMinX() >= ext.getMinY() &&
          envelope.getMaxX() <= ext.getMaxX() &&
          envelope.getMinY() >= ext.getMinY() &&
          envelope.getMaxY() <= ext.getMaxY()) {
        return IntersectCase.WITHIN;
      }
      return IntersectCase.INTERSECTS;
    } else if (other instanceof Point) {
      Point p = (Point)other;
      if (envelope.contains(p.getX(),p.getY()))
        return IntersectCase.CONTAINS;
      else
        return IntersectCase.OUTSIDE;
    } else if (JtsGeometry.class.isInstance(other)) {
      throw new IllegalArgumentException("TODO...");
    }
    throw new IllegalArgumentException("JtsEnvelope can be compared with Envelope or Geogmetry");
  }

  @Override
  public String toString() {
    return envelope.toString();<commit_msg>For more complex shapes (polygon, point-distance), transpose intersect().<commit_after>/**
 * 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 com.googlecode.lucene.spatial.base.shape;

import org.apache.lucene.spatial.base.IntersectCase;
import org.apache.lucene.spatial.base.context.SpatialContext;
import org.apache.lucene.spatial.base.shape.BBox;
import org.apache.lucene.spatial.base.shape.Point;
import org.apache.lucene.spatial.base.shape.Shape;
import org.apache.lucene.spatial.base.shape.simple.Point2D;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

public class JtsEnvelope implements BBox {

  public final Envelope envelope;

  public JtsEnvelope(Envelope envelope) {
    this.envelope = envelope;
  }

  public JtsEnvelope(double x1, double x2, double y1, double y2) {
    this.envelope = new Envelope(x1, x2, y1, y2);
  }

  @Override
  public boolean hasArea() {
    return getWidth() > 0 && getHeight() > 0;
  }

  public double getArea() {
    return getWidth() * getHeight();
  }

  public boolean getCrossesDateLine() {
    return false;
  }

  @Override
  public double getHeight() {
    return envelope.getWidth();
  }

  @Override
  public double getWidth() {
    return envelope.getHeight();
  }

  @Override
  public double getMaxX() {
    return envelope.getMaxX();
  }

  @Override
  public double getMaxY() {
    return envelope.getMaxY();
  }

  @Override
  public double getMinX() {
    return envelope.getMinX();
  }

  @Override
  public double getMinY() {
    return envelope.getMinY();
  }

  @Override
  public boolean hasSize() {
    return !envelope.isNull();
  }

  //----------------------------------------
  //----------------------------------------

  @Override
  public JtsEnvelope getBoundingBox() {
    return this;
  }

  @Override
  public IntersectCase intersect(Shape other, SpatialContext context) {
    if (BBox.class.isInstance(other)) {
      BBox ext = other.getBoundingBox();
      if (ext.getMinX() > envelope.getMaxX() ||
          ext.getMaxX() < envelope.getMinX() ||
          ext.getMinY() > envelope.getMaxY() ||
          ext.getMaxY() < envelope.getMinY()) {
        return IntersectCase.OUTSIDE;
      }

      if (ext.getMinX() >= envelope.getMinX() &&
          ext.getMaxX() <= envelope.getMaxX() &&
          ext.getMinY() >= envelope.getMinY() &&
          ext.getMaxY() <= envelope.getMaxY()) {
        return IntersectCase.CONTAINS;
      }

      if (envelope.getMinX() >= ext.getMinY() &&
          envelope.getMaxX() <= ext.getMaxX() &&
          envelope.getMinY() >= ext.getMinY() &&
          envelope.getMaxY() <= ext.getMaxY()) {
        return IntersectCase.WITHIN;
      }
      return IntersectCase.INTERSECTS;
    } else if (other instanceof Point) {
      Point p = (Point)other;
      if (envelope.contains(p.getX(),p.getY()))
        return IntersectCase.CONTAINS;
      else
        return IntersectCase.OUTSIDE;
    } else if (JtsGeometry.class.isInstance(other)) {
      return other.intersect(this,context).transpose();
    }
    throw new IllegalArgumentException("JtsEnvelope can be compared with Envelope or Geogmetry");
  }

  @Override
  public String toString() {
    return envelope.toString();
{'fraction_non_alphanumeric': 0.13141631983856727, 'fraction_numerical': 0.0037835792659856224, 'mean_word_length': 2.9218595450049456, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016873', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2367, 'n_words': 629}