BoundaryList.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. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.stream.Stream;

  21. import org.apache.commons.geometry.core.Point;

  22. /** Simple implementation of {@link BoundarySource} containing boundaries stored in a list.
  23.  * Lists given during construction are used directly; no copies are made. Thread safety and
  24.  * immutability therefore depend on the underlying list and its usage outside of this class.
  25.  * The boundary list cannot be modified through this class.
  26.  * @param <P> Point implementation type
  27.  * @param <S> Hyperplane convex subset implementation type
  28.  */
  29. public class BoundaryList<P extends Point<P>, S extends HyperplaneConvexSubset<P>>
  30.     implements BoundarySource<S> {

  31.     /** List of boundaries. */
  32.     private final List<S> boundaries;

  33.     /** Construct a new instance containing the given boundaries. The input list is
  34.      * used directly; no copy is made.
  35.      * @param boundaries boundary list
  36.      */
  37.     public BoundaryList(final List<? extends S> boundaries) {
  38.         this.boundaries = Collections.unmodifiableList(boundaries);
  39.     }

  40.     /** Get the boundaries for the instance. The returned list cannot be modified.
  41.      * @return boundaries for the instance
  42.      */
  43.     public List<S> getBoundaries() {
  44.         return boundaries;
  45.     }

  46.     /** Get the number of boundaries in the instance. This is exactly
  47.      * equivalent to {@code boundaryList.getBoundaries().size()} but the
  48.      * word "size" is avoided here to prevent confusion with geometric
  49.      * size.
  50.      * @return number of boundaries in the instance
  51.      */
  52.     public int count() {
  53.         return boundaries.size();
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public Stream<S> boundaryStream() {
  58.         return boundaries.stream();
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public String toString() {
  63.         final StringBuilder sb = new StringBuilder();
  64.         sb.append(this.getClass().getSimpleName())
  65.             // only display the count and not the actual boundaries
  66.             // since the list could be huge
  67.             .append("[count= ")
  68.             .append(count())
  69.             .append(']');

  70.         return sb.toString();
  71.     }
  72. }