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  import java.util.Collections;
20  import java.util.List;
21  import java.util.stream.Stream;
22  
23  import org.apache.commons.geometry.core.Point;
24  
25  /** Simple implementation of {@link BoundarySource} containing boundaries stored in a list.
26   * Lists given during construction are used directly; no copies are made. Thread safety and
27   * immutability therefore depend on the underlying list and its usage outside of this class.
28   * The boundary list cannot be modified through this class.
29   * @param <P> Point implementation type
30   * @param <S> Hyperplane convex subset implementation type
31   */
32  public class BoundaryList<P extends Point<P>, S extends HyperplaneConvexSubset<P>>
33      implements BoundarySource<S> {
34  
35      /** List of boundaries. */
36      private final List<S> boundaries;
37  
38      /** Construct a new instance containing the given boundaries. The input list is
39       * used directly; no copy is made.
40       * @param boundaries boundary list
41       */
42      public BoundaryList(final List<? extends S> boundaries) {
43          this.boundaries = Collections.unmodifiableList(boundaries);
44      }
45  
46      /** Get the boundaries for the instance. The returned list cannot be modified.
47       * @return boundaries for the instance
48       */
49      public List<S> getBoundaries() {
50          return boundaries;
51      }
52  
53      /** Get the number of boundaries in the instance. This is exactly
54       * equivalent to {@code boundaryList.getBoundaries().size()} but the
55       * word "size" is avoided here to prevent confusion with geometric
56       * size.
57       * @return number of boundaries in the instance
58       */
59      public int count() {
60          return boundaries.size();
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public Stream<S> boundaryStream() {
66          return boundaries.stream();
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public String toString() {
72          final StringBuilder sb = new StringBuilder();
73          sb.append(this.getClass().getSimpleName())
74              // only display the count and not the actual boundaries
75              // since the list could be huge
76              .append("[count= ")
77              .append(count())
78              .append(']');
79  
80          return sb.toString();
81      }
82  }