ComplexComparator.java

  1. /*
  2.  * Licensed to the Hipparchus project under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.hipparchus.complex;

  18. import java.io.Serializable;
  19. import java.util.Comparator;

  20. /**
  21.  * Comparator for Complex Numbers.
  22.  *
  23.  */
  24. public class ComplexComparator implements Comparator<Complex>, Serializable {

  25.     /** Serializable UID. */
  26.     private static final long serialVersionUID = 20171113L;

  27.     /** Empty constructor.
  28.      * <p>
  29.      * This constructor is not strictly necessary, but it prevents spurious
  30.      * javadoc warnings with JDK 18 and later.
  31.      * </p>
  32.      * @since 3.0
  33.      */
  34.     public ComplexComparator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  35.         // nothing to do
  36.     }

  37.     /** Compare two complex numbers, using real ordering as the primary sort order and
  38.      * imaginary ordering as the secondary sort order.
  39.      * @param o1 first complex number
  40.      * @param o2 second complex number
  41.      * @return a negative value if o1 real part is less than o2 real part
  42.      * or if real parts are equal and o1 imaginary part is less than o2 imaginary part
  43.      */
  44.     @Override
  45.     public int compare(Complex o1, Complex o2) {
  46.         if (o1 == null) {
  47.             return o2 == null ? 0 : -1;
  48.         } else if (o2 == null) {
  49.             return 1;
  50.         } else {
  51.             return o1.compareTo(o2);
  52.         }
  53.     }

  54. }