Split.java

  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. /** Class containing the result of splitting an object with a hyperplane.
  19.  * @param <T> Split type
  20.  */
  21. public final class Split<T> {

  22.     /** Part of the object lying on the minus side of the splitting hyperplane.
  23.      */
  24.     private final T minus;

  25.     /** Part of the object lying on the plus side of the splitting hyperplane.
  26.      */
  27.     private final T plus;

  28.     /** Build a new instance from its parts.
  29.      * @param minus part of the object lying on the minus side of the
  30.      *      splitting hyperplane or null if no such part exists
  31.      * @param plus part of the object lying on the plus side of the
  32.      *      splitting hyperplane or null if no such part exists.
  33.      */
  34.     public Split(final T minus, final T plus) {
  35.         this.minus = minus;
  36.         this.plus = plus;
  37.     }

  38.     /** Get the part of the object lying on the minus side of the splitting
  39.      * hyperplane or null if no such part exists.
  40.      * @return part of the object lying on the minus side of the splitting
  41.      *      hyperplane
  42.      */
  43.     public T getMinus() {
  44.         return minus;
  45.     }

  46.     /** Get the part of the object lying on the plus side of the splitting
  47.      * hyperplane or null if no such part exists.
  48.      * @return part of the object lying on the plus side of the splitting
  49.      *      hyperplane
  50.      */
  51.     public T getPlus() {
  52.         return plus;
  53.     }

  54.     /** Get the location of the object with respect to its splitting
  55.      * hyperplane.
  56.      * @return
  57.      *  <ul>
  58.      *      <li>{@link SplitLocation#PLUS} - if only {@link #getPlus()} is not null</li>
  59.      *      <li>{@link SplitLocation#MINUS} - if only {@link #getMinus()} is not null</li>
  60.      *      <li>{@link SplitLocation#BOTH} - if both {@link #getPlus()} and {@link #getMinus()}
  61.      *          are not null</li>
  62.      *      <li>{@link SplitLocation#NEITHER} - if both {@link #getPlus()} and {@link #getMinus()}
  63.      *          are null</li>
  64.      *  </ul>
  65.      */
  66.     public SplitLocation getLocation() {
  67.         if (minus != null) {
  68.             return plus != null ? SplitLocation.BOTH : SplitLocation.MINUS;
  69.         } else if (plus != null) {
  70.             return SplitLocation.PLUS;
  71.         }
  72.         return SplitLocation.NEITHER;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public String toString() {
  77.         final StringBuilder sb = new StringBuilder();
  78.         sb.append(this.getClass().getSimpleName())
  79.             .append("[location= ")
  80.             .append(getLocation())
  81.             .append(", minus= ")
  82.             .append(minus)
  83.             .append(", plus= ")
  84.             .append(plus)
  85.             .append(']');

  86.         return sb.toString();
  87.     }
  88. }