org.javimmutable.collections.cursors.LazyMultiCursor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javimmutable-collections Show documentation
Show all versions of javimmutable-collections Show documentation
Library providing immutable/persistent collection classes for
Java. While collections are immutable they provide methods for
adding and removing values by creating new modified copies of
themselves. Each copy shares almost all of its structure with
other copies to minimize memory consumption.
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2017, Burton Computer Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Burton Computer Corporation nor the names
// of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package org.javimmutable.collections.cursors;
import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Cursorable;
import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Indexed;
import org.javimmutable.collections.SplitCursor;
import javax.annotation.Nonnull;
public class LazyMultiCursor
extends AbstractStartedCursor
{
@Nonnull
private final Cursor> source;
@Nonnull
private final Cursor cursor;
private LazyMultiCursor(@Nonnull Cursor> source,
@Nonnull Cursor cursor)
{
this.source = source;
this.cursor = cursor;
}
/**
* Constructs a cursor that visits all of the values reachable from all of the
* Cursorables visited by source.
*/
public static Cursor cursor(@Nonnull Cursor> source)
{
return new AbstractStartCursor()
{
@Nonnull
@Override
public Cursor next()
{
return advance(source.start());
}
};
}
/**
* Constructs a cursor that visits all of the values reachable from all of the
* Cursorables contained in source.
*/
public static Cursor cursor(@Nonnull Indexed> source)
{
return cursor(StandardCursor.of(source));
}
/**
* Constructs a cursor that visits all of the values reachable from all of the
* Cursorables contained in source. All values are transformed using the provided method.
*/
public static Cursor transformed(Indexed source,
Func1> transforminator)
{
return transformed(StandardCursor.of(source), transforminator);
}
/**
* Constructs a cursor that visits all of the values reachable from all of the
* Cursorables visited by source. All values are transformed using the provided method.
*/
public static Cursor transformed(Cursor source,
Func1> transforminator)
{
return cursor(TransformCursor.of(source, transforminator));
}
/**
* Constructs a Cursorable with a cursor() method that returns a LazyMultiCursor from the
* provided sources. Note: the type signature has changed from v1.9.1 to require a
* Cursorable of type T rather than a super class.
*/
public static Cursorable cursorable(@Nonnull final Cursor> sources)
{
return () -> LazyMultiCursor.cursor(sources);
}
/**
* Constructs a Cursorable with a cursor() method that returns a LazyMultiCursor from the
* provided sources. Note: the type signature has changed from v1.9.1 to require a
* Cursorable of type T rather than a super class.
*/
public static Cursorable cursorable(@Nonnull final Indexed> sources)
{
return () -> LazyMultiCursor.cursor(sources);
}
/**
* Used internally to create a LazyMultiCursor with already started source and cursor fields.
*/
private static Cursor resumeAfterSplitCursor(@Nonnull Cursor> source,
@Nonnull Cursor cursor)
{
return new AbstractStartCursor()
{
@Nonnull
@Override
public Cursor next()
{
return new LazyMultiCursor<>(source, cursor);
}
};
}
private static Cursor advance(Cursor> source)
{
while (source.hasValue()) {
Cursor cursor = source.getValue().cursor().start();
if (cursor.hasValue()) {
return new LazyMultiCursor<>(source, cursor);
}
source = source.next();
}
return EmptyStartedCursor.of();
}
@Nonnull
@Override
public Cursor next()
{
if (cursor.hasValue()) {
Cursor nextCursor = cursor.next();
if (nextCursor.hasValue()) {
return new LazyMultiCursor<>(source, nextCursor);
}
}
return advance(source.next());
}
@Override
public boolean hasValue()
{
return cursor.hasValue();
}
@Override
public T getValue()
{
return cursor.getValue();
}
@Override
public boolean isSplitAllowed()
{
return source.isSplitAllowed();
}
@Override
public SplitCursor splitCursor()
{
final SplitCursor> split = source.splitCursor();
return new SplitCursor<>(resumeAfterSplitCursor(split.getLeft().start(), cursor), cursor(split.getRight()));
}
}