001/*
002 * ============================================================================
003 * Copyright © 2014 by Alexis Cartier, Dominic Fox.
004 * All Rights Reserved.
005 * ============================================================================
006 * The MIT License (MIT)
007 *
008 * Permission is hereby granted, free of charge, to any person obtaining a copy
009 * of this software and associated documentation files (the "Software"), to
010 * deal in the Software without restriction, including without limitation the
011 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
012 * sell copies of the Software, and to permit persons to whom the Software is
013 * furnished to do so, subject to the following conditions:
014 *
015 * The above copyright notice and this permission notice shall be included in
016 * all copies or substantial portions of the Software.
017 *
018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
024 * IN THE SOFTWARE.
025 */
026
027package org.tquadrat.foundation.stream.internal;
028
029import static org.apiguardian.api.API.Status.INTERNAL;
030import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
031
032import java.util.Comparator;
033import java.util.Iterator;
034import java.util.Map.Entry;
035import java.util.Optional;
036import java.util.Spliterator;
037import java.util.function.BiConsumer;
038import java.util.function.BiFunction;
039import java.util.function.BinaryOperator;
040import java.util.function.Consumer;
041import java.util.function.Function;
042import java.util.function.IntFunction;
043import java.util.function.Predicate;
044import java.util.function.Supplier;
045import java.util.function.ToDoubleFunction;
046import java.util.function.ToIntFunction;
047import java.util.function.ToLongFunction;
048import java.util.stream.Collector;
049import java.util.stream.DoubleStream;
050import java.util.stream.IntStream;
051import java.util.stream.LongStream;
052import java.util.stream.Stream;
053
054import org.apiguardian.api.API;
055import org.tquadrat.foundation.annotation.ClassVersion;
056import org.tquadrat.foundation.stream.MapStream;
057
058/**
059 *  The default implementation for
060 *  {@link MapStream MapStream}.
061 *
062 *  @param  <K> The type of the map keys.
063 *  @param  <V> The type of the map values.
064 *
065 *  @author Alexis Cartier (alexcrt)
066 *  @author Dominic Fox
067 *  @modified Thomas Thrien - thomas.thrien@tquadrat.org
068 *  @version $Id: DefaultMapStream.java 1060 2023-09-24 19:21:40Z tquadrat $
069 *  @since 0.0.7
070 *
071 *  @UMLGraph.link
072 */
073@ClassVersion( sourceVersion = "$Id: DefaultMapStream.java 1060 2023-09-24 19:21:40Z tquadrat $" )
074@API( status = INTERNAL, since = "0.0.7" )
075public final class DefaultMapStream<K,V> implements MapStream<K,V>
076{
077        /*------------*\
078    ====** Attributes **=======================================================
079        \*------------*/
080    /**
081     *  The wrapped stream.
082     */
083    private final Stream<Entry<K,V>> m_Delegate;
084
085        /*--------------*\
086    ====** Constructors **=====================================================
087        \*--------------*/
088    /**
089     *  Creates a new {@code DefaultMapStream} instance from the provided
090     *  stream.
091     *
092     *  @param  stream  The stream to wrap.
093     */
094    public DefaultMapStream( final Stream<Entry<K,V>> stream ) { m_Delegate = requireNonNullArgument( stream, "stream" ); }
095
096        /*---------*\
097    ====** Methods **==========================================================
098        \*---------*/
099    /**
100     *  {@inheritDoc}
101     */
102    @Override
103    public final boolean allMatch( final Predicate<? super Entry<K,V>> predicate ) { return m_Delegate.allMatch( predicate ); }
104
105    /**
106     *  {@inheritDoc}
107     */
108    @Override
109    public final boolean anyMatch( final Predicate<? super Entry<K,V>> predicate ) { return m_Delegate.anyMatch( predicate ); }
110
111    /**
112     *  {@inheritDoc}
113     */
114    @Override
115    public final void close() { m_Delegate.close(); }
116
117    /**
118     *  {@inheritDoc}
119     */
120    @Override
121    public final <R,A> R collect( final Collector<? super Entry<K,V>,A,R> collector ) { return m_Delegate.collect( collector ); }
122
123    /**
124     *  {@inheritDoc}
125     */
126    @Override
127    public final <R> R collect( final Supplier<R> supplier, final BiConsumer<R,? super Entry<K,V>> accumulator, final BiConsumer<R,R> combiner )
128    {
129        return m_Delegate.collect( supplier, accumulator, combiner );
130    }   //  collect()
131
132    /**
133     *  {@inheritDoc}
134     */
135    @Override
136    public final long count() { return m_Delegate.count(); }
137
138    /**
139     *  {@inheritDoc}
140     */
141    @Override
142    public final MapStream<K,V> distinct() { return new DefaultMapStream<>( m_Delegate.distinct() ); }
143
144    /**
145     *  {@inheritDoc}
146     */
147    @Override
148    public final MapStream<K,V> filter( final Predicate<? super Entry<K,V>> predicate )
149    {
150        return new DefaultMapStream<>( m_Delegate.filter( predicate ) );
151    }   //  filter()
152
153    /**
154     *  {@inheritDoc}
155     */
156    @Override
157    public final Optional<Entry<K,V>> findAny() { return m_Delegate.findAny(); }
158
159    /**
160     *  {@inheritDoc}
161     */
162    @Override
163    public final Optional<Entry<K,V>> findFirst() { return m_Delegate.findFirst(); }
164
165    /**
166     *  {@inheritDoc}
167     */
168    @Override
169    public final <R> Stream<R> flatMap( final Function<? super Entry<K,V>,? extends Stream<? extends R>> mapper )
170    {
171        return m_Delegate.flatMap( mapper );
172    }   //  flatMap()
173
174    /**
175     *  {@inheritDoc}
176     */
177    @Override
178    public final DoubleStream flatMapToDouble( final Function<? super Entry<K,V>,? extends DoubleStream> mapper )
179    {
180        return m_Delegate.flatMapToDouble( mapper );
181    }   //  flatMapToDouble()
182
183    /**
184     *  {@inheritDoc}
185     */
186    @Override
187    public final IntStream flatMapToInt( final Function<? super Entry<K,V>,? extends IntStream> mapper )
188    {
189        return m_Delegate.flatMapToInt( mapper );
190    }   //  flatMapToInt()
191
192    /**
193     *  {@inheritDoc}
194     */
195    @Override
196    public final LongStream flatMapToLong( final Function<? super Entry<K,V>,? extends LongStream> mapper )
197    {
198        return m_Delegate.flatMapToLong( mapper );
199    }   //  flatMapToLong()
200
201    /**
202     *  {@inheritDoc}
203     */
204    @Override
205    public final void forEach( final Consumer<? super Entry<K,V>> action ) { m_Delegate.forEach( action ); }
206
207    /**
208     *  {@inheritDoc}
209     */
210    @Override
211    public final void forEachOrdered( final Consumer<? super Entry<K,V>> action ) { m_Delegate.forEachOrdered( action ); }
212
213    /**
214     *  {@inheritDoc}
215     */
216    @Override
217    public final boolean isParallel() { return m_Delegate.isParallel(); }
218
219    /**
220     *  {@inheritDoc}
221     */
222    @Override
223    public final Iterator<Entry<K,V>> iterator() { return m_Delegate.iterator(); }
224
225    /**
226     *  {@inheritDoc}
227     */
228    @Override
229    public final MapStream<K,V> limit( final long maxSize ) { return new DefaultMapStream<>( m_Delegate.limit( maxSize ) ); }
230
231    /**
232     *  {@inheritDoc}
233     */
234    @Override
235    public final <R> Stream<R> map( final Function<? super Entry<K,V>,? extends R> mapper ) { return m_Delegate.map( mapper ); }
236
237    /**
238     *  {@inheritDoc}
239     */
240    @Override
241    public final DoubleStream mapToDouble( final ToDoubleFunction<? super Entry<K,V>> mapper ) { return m_Delegate.mapToDouble( mapper ); }
242
243    /**
244     *  {@inheritDoc}
245     */
246    @Override
247    public final IntStream mapToInt( final ToIntFunction<? super Entry<K,V>> mapper ) { return m_Delegate.mapToInt( mapper ); }
248
249    /**
250     *  {@inheritDoc}
251     */
252    @Override
253    public final LongStream mapToLong( final ToLongFunction<? super Entry<K,V>> mapper ) { return m_Delegate.mapToLong( mapper ); }
254
255    /**
256     *  {@inheritDoc}
257     */
258    @Override
259    public final Optional<Entry<K,V>> max( final Comparator<? super Entry<K,V>> comparator ) { return m_Delegate.max( comparator ); }
260
261    /**
262     *  {@inheritDoc}
263     */
264    @Override
265    public final Optional<Entry<K,V>> min( final Comparator<? super Entry<K,V>> comparator ) { return m_Delegate.min( comparator ); }
266
267    /**
268     *  {@inheritDoc}
269     */
270    @Override
271    public final boolean noneMatch( final Predicate<? super Entry<K,V>> predicate ) { return m_Delegate.noneMatch( predicate ); }
272
273    /**
274     *  {@inheritDoc}
275     */
276    @Override
277    public final MapStream<K,V> onClose( final Runnable closeHandler ) { return new DefaultMapStream<>( m_Delegate.onClose( closeHandler ) ); }
278
279    /**
280     *  {@inheritDoc}
281     */
282    @Override
283    public final MapStream<K,V> parallel() { return new DefaultMapStream<>( m_Delegate.parallel() ); }
284
285    /**
286     *  {@inheritDoc}
287     */
288    @Override
289    public final MapStream<K,V> peek( final Consumer<? super Entry<K,V>> action ) { return new DefaultMapStream<>( m_Delegate.peek( action ) ); }
290
291    /**
292     *  {@inheritDoc}
293     */
294    @Override
295    public final Optional<Entry<K,V>> reduce( final BinaryOperator<Entry<K,V>> accumulator ) { return m_Delegate.reduce( accumulator ); }
296
297    /**
298     *  {@inheritDoc}
299     */
300    @Override
301    public final Entry<K,V> reduce( final Entry<K,V> identity, final BinaryOperator<Entry<K,V>> accumulator )
302    {
303        return m_Delegate.reduce( identity, accumulator );
304    }   //  reduce()
305
306    /**
307     *  {@inheritDoc}
308     */
309    @Override
310    public final <U> U reduce( final U identity, final BiFunction<U,? super Entry<K,V>,U> accumulator, final BinaryOperator<U> combiner )
311    {
312        return m_Delegate.reduce( identity, accumulator, combiner );
313    }   //  reduce()
314
315    /**
316     *  {@inheritDoc}
317     */
318    @Override
319    public final MapStream<K,V> sequential() { return new DefaultMapStream<>( m_Delegate.sequential() ); }
320
321    /**
322     *  {@inheritDoc}
323     */
324    @Override
325    public final MapStream<K,V> skip( final long n ) { return new DefaultMapStream<>( m_Delegate.skip( n ) ); }
326
327    /**
328     *  {@inheritDoc}
329     */
330    @Override
331    public final MapStream<K,V> sorted() { return new DefaultMapStream<>( m_Delegate.sorted() ); }
332
333    /**
334     *  {@inheritDoc}
335     */
336    @Override
337    public final MapStream<K,V> sorted( final Comparator<? super Entry<K,V>> comparator ) { return new DefaultMapStream<>( m_Delegate.sorted( comparator ) ); }
338
339    /**
340     *  {@inheritDoc}
341     *  @see java.util.stream.BaseStream#spliterator()
342     */
343    @Override
344    public final Spliterator<Entry<K,V>> spliterator() { return m_Delegate.spliterator(); }
345
346    /**
347     *  {@inheritDoc}
348     */
349    @Override
350    public final Object [] toArray() { return m_Delegate.toArray(); }
351
352    /**
353     *  {@inheritDoc}
354     */
355    @Override
356    public final <A> A [] toArray( final IntFunction<A []> generator ) { return m_Delegate.toArray( generator ); }
357
358    /**
359     *  {@inheritDoc}
360     */
361    @Override
362    public final MapStream<K,V> unordered() { return new DefaultMapStream<>( m_Delegate.unordered() ); }
363}
364//  class DefaultMapStream
365
366/*
367 *  End of File
368 */