001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.core.partitioning;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.stream.Stream;
022
023import org.apache.commons.geometry.core.Point;
024
025/** Simple implementation of {@link BoundarySource} containing boundaries stored in a list.
026 * Lists given during construction are used directly; no copies are made. Thread safety and
027 * immutability therefore depend on the underlying list and its usage outside of this class.
028 * The boundary list cannot be modified through this class.
029 * @param <P> Point implementation type
030 * @param <S> Hyperplane convex subset implementation type
031 */
032public class BoundaryList<P extends Point<P>, S extends HyperplaneConvexSubset<P>>
033    implements BoundarySource<S> {
034
035    /** List of boundaries. */
036    private final List<S> boundaries;
037
038    /** Construct a new instance containing the given boundaries. The input list is
039     * used directly; no copy is made.
040     * @param boundaries boundary list
041     */
042    public BoundaryList(final List<? extends S> boundaries) {
043        this.boundaries = Collections.unmodifiableList(boundaries);
044    }
045
046    /** Get the boundaries for the instance. The returned list cannot be modified.
047     * @return boundaries for the instance
048     */
049    public List<S> getBoundaries() {
050        return boundaries;
051    }
052
053    /** Get the number of boundaries in the instance. This is exactly
054     * equivalent to {@code boundaryList.getBoundaries().size()} but the
055     * word "size" is avoided here to prevent confusion with geometric
056     * size.
057     * @return number of boundaries in the instance
058     */
059    public int count() {
060        return boundaries.size();
061    }
062
063    /** {@inheritDoc} */
064    @Override
065    public Stream<S> boundaryStream() {
066        return boundaries.stream();
067    }
068
069    /** {@inheritDoc} */
070    @Override
071    public String toString() {
072        final StringBuilder sb = new StringBuilder();
073        sb.append(this.getClass().getSimpleName())
074            // only display the count and not the actual boundaries
075            // since the list could be huge
076            .append("[count= ")
077            .append(count())
078            .append(']');
079
080        return sb.toString();
081    }
082}