it.tidalwave.util.F8 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of it-tidalwave-accounting-model Show documentation
Show all versions of it-tidalwave-accounting-model Show documentation
This module implements the data model.
/*
* #%L
* *********************************************************************************************************************
*
* blueHour
* http://bluehour.tidalwave.it - git clone [email protected]:tidalwave/bluehour-src.git
* %%
* Copyright (C) 2013 - 2015 Tidalwave s.a.s. (http://tidalwave.it)
* %%
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* $Id$
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.util;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import it.tidalwave.util.spi.ExtendedFinder8Support;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici ([email protected])
* @version $Id$
*
**********************************************************************************************************************/
// TODO: merge to Finder8
public interface F8> extends ExtendedFinder8Support
{
static class LambdaFinder>
extends Finder8Support
implements F8
{
private static final long serialVersionUID = 2688397754942922706L;
@Nonnull
private final Optional, List>> computeResults;
@Nonnull
private final Optional, List>> computeNeededResults;
private LambdaFinder (final @Nonnull Optional, List>> computeResults,
final @Nonnull Optional, List>> computeNeededResults)
{
this.computeResults = computeResults;
this.computeNeededResults = computeNeededResults;
if (!this.computeResults.isPresent() && !this.computeNeededResults.isPresent())
{
throw new ExceptionInInitializerError("One of computeResults or computeNeededResults must be present");
}
}
public LambdaFinder (final @Nonnull LambdaFinder other, final @Nonnull Object override)
{
super(other, override);
final LambdaFinder source = getSource(LambdaFinder.class, other, override);
this.computeResults = source.computeResults;
this.computeNeededResults = source.computeNeededResults;
}
@Override @Nonnull
protected final List extends TYPE> computeResults()
{
return computeResults.isPresent() ? computeResults.get().apply(this) : super.computeResults();
// return computeResults.flatMap(f -> f.apply(this)).orElse((List)super.computeResults());
}
@Override @Nonnull
protected final List extends TYPE> computeNeededResults()
{
return computeNeededResults.isPresent() ? computeNeededResults.get().apply(this) : super.computeNeededResults();
// return computeNeededResults.map(f -> f.apply(this)).orElse((List)super.computeNeededResults());
}
}
@Nonnull
public static > F8 ofComputeResults (
final @Nonnull Function, List> computeResults)
{
return new LambdaFinder(Optional.of(computeResults), Optional.empty());
}
@Nonnull
public static > F8 ofComputeNeededResults (
final @Nonnull Function, List> computeNeededResults)
{
return new LambdaFinder(Optional.empty(), Optional.of(computeNeededResults));
}
}