org.pragmaticminds.crunch.api.trigger.strategy.TriggerStrategies Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crunch-api Show documentation
Show all versions of crunch-api Show documentation
Programing Interfaces for other CRUNCH Projects.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.pragmaticminds.crunch.api.trigger.strategy;
import org.pragmaticminds.crunch.api.records.MRecord;
import org.pragmaticminds.crunch.api.trigger.comparator.Supplier;
import org.pragmaticminds.crunch.api.values.TypedValues;
import java.io.Serializable;
import java.util.HashSet;
/**
* A collection of {@link TriggerStrategy}s for boolean decision making.
*
* @author Erwin Wagasow
* Created by Erwin Wagasow on 13.08.2018
*/
public class TriggerStrategies {
private TriggerStrategies() { /* does nothing */}
/**
* This method triggers always on supplied value = true
* @param supplier extracts the relevant values from a {@link TypedValues}
* @return true if triggered
*/
public static TriggerStrategy onTrue(Supplier supplier){
return new LambdaTriggerStrategy(
values -> supplier.extract(values) != null && supplier.extract(values),
() -> new HashSet<>(supplier.getChannelIdentifiers())
);
}
/**
* This method triggers always on supplied value = false
* @param supplier extracts the relevant values from a {@link TypedValues}
* @return true if triggered, false if not or supplied value is null
*/
public static TriggerStrategy onFalse(Supplier supplier){
return new LambdaTriggerStrategy(
values ->
// null check
supplier.extract(values) != null
// on false check
&& !supplier.extract(values),
() -> new HashSet<>(supplier.getChannelIdentifiers())
);
}
/**
* This method triggers on supplied value = true and the last supplied value = false
* @param supplier extracts the relevant values from a {@link TypedValues}
* @return true if triggered
*/
public static TriggerStrategy onBecomeTrue(Supplier supplier){
return onBecomeTrue(supplier, null);
}
/**
* This method triggers on supplied value = true and the last supplied value = false
* @param supplier extracts the relevant values from a {@link TypedValues}
* @param initialValue the first value to compare with
* @return true if triggered
*/
public static TriggerStrategy onBecomeTrue(Supplier supplier, Boolean initialValue){
return new MemoryTriggerStrategy(supplier, 1, initialValue) {
@Override
public boolean isToBeTriggered(Boolean decisionBase) {
return
// null check
decisionBase != null
// condition check
&& !lastDecisionBases.isEmpty() && !lastDecisionBases.get(0) && decisionBase;
}
};
}
/**
* This method triggers on supplied value = false and the last supplied value = true
* @param supplier extracts the relevant values from a {@link TypedValues}
* @return true if triggered
*/
public static TriggerStrategy onBecomeFalse(Supplier supplier){
return onBecomeFalse(supplier, null);
}
/**
* This method triggers on supplied value = false and the last supplied value = true
* @param supplier extracts the relevant values from a {@link TypedValues}
* @param initialValue the first value to compare with
* @return true if triggered
*/
public static TriggerStrategy onBecomeFalse(Supplier supplier, Boolean initialValue){
return new MemoryTriggerStrategy(supplier, 1, initialValue) {
@Override
public boolean isToBeTriggered(Boolean decisionBase) {
return
// null check
decisionBase != null
// condition check
&& !lastDecisionBases.isEmpty() && lastDecisionBases.get(0) && !decisionBase;
}
};
}
/**
* This method triggers on supplied value != last supplied value
* @param supplier extracts the relevant values from a {@link TypedValues}
* @return true if triggered
*/
public static TriggerStrategy onChange(Supplier supplier){
return onChange(supplier, null);
}
/**
* This method triggers on supplied value != last supplied value
* @param supplier extracts the relevant values from a {@link TypedValues}
* @param initialValue the first value to compare with
* @return true if triggered
*/
public static TriggerStrategy onChange(Supplier supplier, T initialValue){
return new MemoryTriggerStrategy(supplier, 1, initialValue) {
/**
* This method is to be implemented by the user of this class, with the final decision making.
*
* @param decisionBase the extracted value
* @return true if triggered, otherwise false
*/
@Override
public boolean isToBeTriggered(T decisionBase) {
return
// null check
decisionBase != null
// condition check
&& !lastDecisionBases.isEmpty() && !lastDecisionBases.get(0).equals(decisionBase);
}
};
}
/**
* This method triggers on supplied value is not available
* @param supplier extracts the relevant values from a {@link MRecord}
* @param type of the Supplier
* @return true if triggered
*/
public static TriggerStrategy onNull(Supplier supplier){
return new LambdaTriggerStrategy(
values -> supplier.extract(values) == null,
() -> new HashSet<>(supplier.getChannelIdentifiers())
);
}
/**
* This method triggers on supplied value is available
* @param supplier extracts the relevant values from a {@link MRecord}
* @param type of the Supplier
* @return true if triggered
*/
public static TriggerStrategy onNotNull(Supplier supplier){
return new LambdaTriggerStrategy(
values -> supplier.extract(values) != null,
() -> new HashSet<>(supplier.getChannelIdentifiers())
);
}
/**
* This method triggers always
* @return always true
*/
public static TriggerStrategy always(){
return new LambdaTriggerStrategy(
values -> true, HashSet::new
);
}
}