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.io.core;
018
019import java.util.stream.Stream;
020
021import org.apache.commons.geometry.core.partitioning.BoundarySource;
022import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
023import org.apache.commons.geometry.io.core.input.GeometryInput;
024import org.apache.commons.numbers.core.Precision;
025
026/** Basic interface for reading geometric boundary representations
027 * (<a href="https://en.wikipedia.org/wiki/Boundary_representation">B-reps</a>) from a specific data storage
028 * format. This interface is intended primarily for use with {@link BoundaryIOManager}.
029 *
030 * <p><strong>Implementation note:</strong> implementations of this interface <em>must</em>
031 * be thread-safe.</p>
032 * @param <H> Geometric boundary type
033 * @param <B> Boundary source type
034 * @see BoundaryWriteHandler
035 * @see BoundaryIOManager
036 * @see <a href="https://en.wikipedia.org/wiki/Boundary_representations">Boundary representations</a>
037 */
038public interface BoundaryReadHandler<H extends HyperplaneConvexSubset<?>, B extends BoundarySource<H>> {
039
040    /** Get the {@link GeometryFormat data format} supported by this handler.
041     * @return data format supported by this handler
042     */
043    GeometryFormat getFormat();
044
045    /** Return an object containing all boundaries read from {@code input} using the handler's
046     * supported data format.
047     * @param input input to read from
048     * @param precision precision context used for floating point comparisons
049     * @return object containing all boundaries read from {@code input}
050     * @throws IllegalArgumentException if mathematically invalid data is encountered
051     * @throws IllegalStateException if a data format error occurs
052     * @throws java.io.UncheckedIOException if an I/O error occurs
053     */
054    B read(GeometryInput input, Precision.DoubleEquivalence precision);
055
056    /** Return a {@link Stream} that can be used to access all boundary information from the given input,
057     * which is expected to contain data in the format supported by this handler. Unlike the
058     * {@link #read(GeometryInput, Precision.DoubleEquivalence) read} method, this method does not <em>require</em>
059     * that all input be read immediately and stored in memory (although implementations of this interface are
060     * still free to do so). Callers may therefore prefer to use this method in cases where memory usage is a
061     * concern or transformations and/or filters must be applied to the boundaries before use.
062     *
063     * <p>Implementing class will usually keep the source input stream open during stream iteration. Callers
064     * should therefore use the returned stream in a try-with-resources statement to ensure that all resources
065     * are properly released. Ex:
066     * </p>
067     * <pre>
068     *  try (Stream&lt;H&gt; stream = handler.boundaries(in, precision)) {
069     *      // access stream content
070     *  }
071     * </pre>
072     *
073     * <p>The following exceptions may be thrown during stream iteration:
074     *  <ul>
075     *      <li>{@link IllegalArgumentException} if mathematically invalid data is encountered</li>
076     *      <li>{@link IllegalStateException} if a data format error occurs</li>
077     *      <li>{@link java.io.UncheckedIOException UncheckedIOException} if an I/O error occurs</li>
078     *  </ul>
079     * @param in input to read from
080     * @param precision precision context used for floating point comparisons
081     * @return stream providing access to the boundary information from the given input
082     * @throws IllegalStateException if a data format error occurs during stream creation
083     * @throws java.io.UncheckedIOException if an I/O error occurs during stream creation
084     */
085    Stream<H> boundaries(GeometryInput in, Precision.DoubleEquivalence precision);
086}