View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) 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 ASF 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    *      http://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.apache.commons.geometry.core.partitioning;
    18
    19  /** Class containing the result of splitting an object with a hyperplane.
    20   * @param <T> Split type
    21   */
    22  public final class Split<T> {
    23
    24      /** Part of the object lying on the minus side of the splitting hyperplane.
    25       */
    26      private final T minus;
    27
    28      /** Part of the object lying on the plus side of the splitting hyperplane.
    29       */
    30      private final T plus;
    31
    32      /** Build a new instance from its parts.
    33       * @param minus part of the object lying on the minus side of the
    34       *      splitting hyperplane or null if no such part exists
    35       * @param plus part of the object lying on the plus side of the
    36       *      splitting hyperplane or null if no such part exists.
    37       */
    38      public Split(final T minus, final T plus) {
    39          this.minus = minus;
    40          this.plus = plus;
    41      }
    42
    43      /** Get the part of the object lying on the minus side of the splitting
    44       * hyperplane or null if no such part exists.
    45       * @return part of the object lying on the minus side of the splitting
    46       *      hyperplane
    47       */
    48      public T getMinus() {
    49          return minus;
    50      }
    51
    52      /** Get the part of the object lying on the plus side of the splitting
    53       * hyperplane or null if no such part exists.
    54       * @return part of the object lying on the plus side of the splitting
    55       *      hyperplane
    56       */
    57      public T getPlus() {
    58          return plus;
    59      }
    60
    61      /** Get the location of the object with respect to its splitting
    62       * hyperplane.
    63       * @return
    64       *  <ul>
    65       *      <li>{@link SplitLocation#PLUS} - if only {@link #getPlus()} is not null</li>
    66       *      <li>{@link SplitLocation#MINUS} - if only {@link #getMinus()} is not null</li>
    67       *      <li>{@link SplitLocation#BOTH} - if both {@link #getPlus()} and {@link #getMinus()}
    68       *          are not null</li>
    69       *      <li>{@link SplitLocation#NEITHER} - if both {@link #getPlus()} and {@link #getMinus()}
    70       *          are null</li>
    71       *  </ul>
    72       */
    73      public SplitLocation getLocation() {
    74          if (minus != null) {
    75              return plus != null ? SplitLocation.BOTH : SplitLocation.MINUS;
    76          } else if (plus != null) {
    77              return SplitLocation.PLUS;
    78          }
    79          return SplitLocation.NEITHER;
    80      }
    81
    82      /** {@inheritDoc} */
    83      @Override
    84      public String toString() {
    85          final StringBuilder sb = new StringBuilder();
    86          sb.append(this.getClass().getSimpleName())
    87              .append("[location= ")
    88              .append(getLocation())
    89              .append(", minus= ")
    90              .append(minus)
    91              .append(", plus= ")
    92              .append(plus)
    93              .append(']');
    94
    95          return sb.toString();
    96      }
    97  }