View Javadoc
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  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  
22  /**
23   * Comparator for Complex Numbers.
24   *
25   */
26  public class ComplexComparator implements Comparator<Complex>, Serializable {
27  
28      /** Serializable UID. */
29      private static final long serialVersionUID = 20171113L;
30  
31      /** Empty constructor.
32       * <p>
33       * This constructor is not strictly necessary, but it prevents spurious
34       * javadoc warnings with JDK 18 and later.
35       * </p>
36       * @since 3.0
37       */
38      public ComplexComparator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
39          // nothing to do
40      }
41  
42      /** Compare two complex numbers, using real ordering as the primary sort order and
43       * imaginary ordering as the secondary sort order.
44       * @param o1 first complex number
45       * @param o2 second complex number
46       * @return a negative value if o1 real part is less than o2 real part
47       * or if real parts are equal and o1 imaginary part is less than o2 imaginary part
48       */
49      @Override
50      public int compare(Complex o1, Complex o2) {
51          if (o1 == null) {
52              return o2 == null ? 0 : -1;
53          } else if (o2 == null) {
54              return 1;
55          } else {
56              return o1.compareTo(o2);
57          }
58      }
59  
60  }