/*
 * Copyright 2020 Google LLC
 *
 * Licensed 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
 *
 *     https://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.google.vinet.data;

import java.time.Instant;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public abstract class TimeSlot implements Comparable<TimeSlot> {

  private final Instant start;
  private final Instant end;
  protected final RegisteredUser registeredUser;
  private final Set<TimeSlot> neighbours = new HashSet<>();
  private TimeSlot pairedSlot = null;
  private double distance = 0;

  /**
   * Create a new time slot. Start and end times must not be null, and start time must come before
   * end time.
   *
   * @param start          The starting instant for the slot.
   * @param end            The ending instant for the slot.
   * @param registeredUser The user for which this slot applies.
   */
  public TimeSlot(Instant start, Instant end, RegisteredUser registeredUser) {
    this.start = Objects.requireNonNull(start, "Start time must not be null!");
    this.end = Objects.requireNonNull(end, "End time must not be null!");
    if (start.isAfter(end)) throw new IllegalArgumentException();
    this.registeredUser = registeredUser;
  }

  public Instant getStart() {
    return start;
  }

  public Instant getEnd() {
    return end;
  }

  public double getDistance() {
    return distance;
  }

  public void setDistance(double distance) {
    this.distance = distance;
  }

  public boolean isPaired() {
    return pairedSlot != null;
  }

  public TimeSlot getPairedSlot() {
    return pairedSlot == null ? MatchingAlgorithm.NIL_NODE : pairedSlot;
  }

  public void setPairedSlot(TimeSlot pairedSlot) {
    this.pairedSlot = pairedSlot;
  }

  public Set<TimeSlot> getNeighbours() {
    return Collections.unmodifiableSet(neighbours);
  }

  public void addNeighbour(TimeSlot timeSlot) {
    neighbours.add(timeSlot);
  }

  /** Compares two time slots based on their start time */
  @Override
  public int compareTo(TimeSlot timeSlot) {
    return start.compareTo(timeSlot.getStart());
  }

  public static Comparator<TimeSlot> TimeSlotEndComparator = Comparator.comparing(TimeSlot::getEnd);

  /**
   * Checks whether this timeslot contains another
   *
   * @param timeSlot The slot that is to be contained
   * @return Whether this slot contains the specified slot
   */
  public boolean contains(TimeSlot timeSlot) {
    return compareTo(timeSlot) <= 0 && TimeSlotEndComparator.compare(this, timeSlot) >= 0;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    TimeSlot timeSlot = (TimeSlot) o;
    return Objects.equals(start, timeSlot.start)
            && Objects.equals(end, timeSlot.end)
            && Objects.equals(registeredUser, timeSlot.registeredUser);
  }

  @Override
  public int hashCode() {
    return Objects.hash(start, end, registeredUser);
  }
}

@@ -1,13 +1,14 @@
 /*%0A *
+ 
  Copyrig
@@ -31,16 +31,17 @@
 LC%0A *%0A *
+ 
  License
@@ -99,16 +99,17 @@
 se%22);%0A *
+ 
  you may
@@ -168,16 +168,17 @@
 ense.%0A *
+ 
  You may
@@ -224,16 +224,15 @@
     
+ 
 https:
-//
 www.
@@ -268,16 +268,17 @@
 .0%0A *%0A *
+ 
  Unless 
@@ -340,16 +340,17 @@
 tware%0A *
+ 
  distrib
@@ -410,16 +410,17 @@
 ASIS,%0A *
+ 
  WITHOUT
@@ -487,16 +487,17 @@
 lied.%0A *
+ 
  See the
@@ -559,16 +559,17 @@
 s and%0A *
+ 
  limitat
@@ -655,16 +655,54 @@
 nstant;%0A
+import java.time.temporal.ChronoUnit;%0A
 import j
@@ -3078,24 +3078,413 @@
  %3E= 0;%0A  %7D%0A%0A
+  public boolean hasMinimumOverlap(TimeSlot timeSlot, long minMinutesOverlap) %7B%0A    if (start.isBefore(timeSlot.start)) %7B%0A      // If difference is negative, they don't overlap, and will return false.%0A      return ChronoUnit.MINUTES.between(timeSlot.start, end) %3E minMinutesOverlap;%0A    %7D else %7B%0A      return ChronoUnit.MINUTES.between(start, timeSlot.end) %3E minMinutesOverlap;%0A    %7D%0A  %7D%0A%0A
   @Override%0A

Add time slot overlap method