All Downloads are FREE. Search and download functionalities are using the official Maven repository.

template.GroupByTemplate.vsl Maven / Gradle / Ivy

## Velocity Template.
package ${functionPackage};

#foreach( $import in $imports)
import $import;
#end
#importslf4j
/**
 * Generated group by aggregation
 *
 *
 *  
    *
  • template file : ${currentTemplate} *
  • target class : {@link ${targetClass}} #foreach( $source in $sourceMappingList) #if(${source.wrapped}) *
  • input class : {@link ${source.sourceInfo.wrapperType}} #else *
  • input class : {@link ${source.sourceInfo.type}} #end #end *
*
* @author Greg Higgins */ public final class ${functionClass} implements GroupBy<${targetClass}>{ #slf4j(${functionClass}) @NoEventReference public Object resetNotifier; @SepNode @PushReference public ArrayListWrappedCollection<${targetClass}> wrappedList; private boolean allMatched; private MutableNumber number = new MutableNumber(); #foreach( $source in $sourceMappingList) #if($!{source.eventClass}) #elseif(${source.wrapped}) public ${source.sourceInfo.wrapperType} ${source.sourceInfo.id}; #else public ${source.sourceInfo.type} ${source.sourceInfo.id}; #end #end private ${targetClass} target; private GroupByTargetMap<${keyClass}, ${targetClass}, ${calcStateClass}> calcState; #if(${isMultiKey}) private ${multiKeyClassName} multiKey; #end #foreach( $source in $sourceMappingList) #if(${source.initialiserRequired}) private GroupByIniitialiser<${source.sourceInfo.type}, ${targetClass}> ${source.initialiserId}; #end #foreach( $functionInfo in ${source.functionSet} ) #if( ${functionInfo.stateless} && ${functionInfo.instanceFunction} ) private ${functionInfo.functionClassName} ${functionInfo.functionInstanceId}; #end #end #end private SerializableBiConsumer<${targetClass},${targetClass}> initFromCopy; public ${functionClass}(){ wrappedList = new ArrayListWrappedCollection<>(); } @Override public void setTargetCollecion(ArrayListWrappedCollection<${targetClass}> targetCollection) { this.wrappedList = targetCollection; } #foreach( $source in $sourceMappingList) #set( $id = ${source.sourceInfo.id} ) #set( $type = ${source.sourceInfo.type} ) #set( $wrapperType = ${source.sourceInfo.wrapperType} ) #if(${source.eventClass}) @EventHandler #else @OnParentUpdate( "$id") #end #if(${source.wrapped}) public boolean update$id($wrapperType eventWrapped){ $type event = ($type)eventWrapped.event(); #else public boolean update$id($type event){ #end #if(${isMultiKey}) multiKey.setKey(event); ${calcStateClass} instance = calcState.getOrCreateInstance(multiKey); #else ${calcStateClass} instance = calcState.getOrCreateInstance(event.${source.keyMethod}()); #end boolean firstMatched = instance.processSource( $velocityCount,#if(${source.initialiserRequired}) ${source.initialiserId}, #end event); allMatched = instance.allMatched(); target = instance.target; #foreach( $functionInfo in ${source.functionSet} ) { ${functionInfo.updateTarget} } #end #if(${source.eventClass}) // updated(); #end if (firstMatched) { wrappedList.addItem(target); } #if(${eventMethod}) if(allMatched){ target.${eventMethod}(); } #end return allMatched; } #end @OnEvent public boolean updated() { boolean updated = allMatched; allMatched = false; return updated; } ## @OnEvent ## public boolean updated(){ ## return true; ## } ## #if(${eventCompleteMethod}) @OnEventComplete public void onEventComplete(){ if (target != null) { target.${eventCompleteMethod}(); } } #end @Initialise public void init(){ calcState = new GroupByTargetMap<>(${calcStateClass}::new); wrappedList.init(); allMatched = false; target = null; number.set(0); #if(${isMultiKey}) multiKey = new ${multiKeyClassName}(); #end #foreach( $source in $sourceMappingList) #if(${source.initialiserRequired}) ${source.initialiserId} = new GroupByIniitialiser<${source.sourceInfo.type}, ${targetClass}>() { @Override public void apply(${source.sourceInfo.type} source, ${targetClass} target) { #foreach( $initialserInfo in ${source.initialiserSet} ) ${initialserInfo.initialiseFunction}; #end } }; #end #foreach( $functionInfo in ${source.functionSet} ) #if( ${functionInfo.stateless} && ${functionInfo.instanceFunction} ) ${functionInfo.functionInstanceId} = new ${functionInfo.functionClassName}(); #end #end #end initFromCopy = ${initCopy}; } #if(${isMultiKey}) //multikey finder @Override public ${targetClass} value(Object o){ if(o instanceof ${keyClass}){ return calcState.getInstance((${keyClass})o).target; } #foreach( $srcInfo in $multiKeyFunctionSet.entrySet() ) if(o instanceof ${srcInfo.key}){ return value((${srcInfo.key}) o); } #end return null; } #foreach( $srcInfo in $multiKeyFunctionSet.entrySet() ) public ${targetClass} value(${srcInfo.key} source){ multiKey.setKey(source); return value(multiKey); } #end #else @Override public ${targetClass} value(Object key) { return calcState.getInstance((${keyClass})key).target; } #end @Override public Collection<${targetClass}> collection() { return wrappedList==null?Collections.EMPTY_LIST:wrappedList.collection(); } @Override public > Map<${keyClass}, V> getMap() { return (Map<${keyClass}, V>) calcState.getInstanceMap(); } @Override public ${targetClass} record() { return target; } @Override public Class<${targetClass}> recordClass() { return ${targetClass}.class; } @Override public ${functionClass} resetNotifier(Object resetNotifier) { this.resetNotifier = resetNotifier; return this; } @OnParentUpdate("resetNotifier") public void resetNotification(Object resetNotifier) { init(); } @Override public void reset() { init(); } @Override public void combine(GroupBy<${targetClass}> other) { #loginfo( "'${functionClass} combining'" ) ${functionClass} otherGroupBy = (${functionClass}) other; Map<${keyClass}, ${calcStateClass}> sourceMap = otherGroupBy.calcState.getInstanceMap(); Map<${keyClass}, ${calcStateClass}> targetMap = calcState.getInstanceMap(); sourceMap .entrySet() .forEach( (Map.Entry<${keyClass}, ${calcStateClass} > e) -> { if (targetMap.containsKey(e.getKey())) { final ${calcStateClass} sourceState = e.getValue(); ${calcStateClass} newInstance = calcState.getOrCreateInstance(e.getKey()); newInstance.combine(sourceState); #foreach( $src in $functionSet ) $src.copyToTarget; #end } else { final ${calcStateClass} sourceState = e.getValue(); ${calcStateClass} newInstance = calcState.getOrCreateInstance(e.getKey()); newInstance.updateMap.clear(); newInstance.updateMap.or(sourceState.updateMap); initFromCopy.accept(sourceState.target, newInstance.target); newInstance.combine(sourceState); #foreach( $src in $functionSet ) $src.copyToTarget; #end wrappedList.addItem(newInstance.target); } }); wrappedList.sort(); } @Override public void deduct(GroupBy<${targetClass}> other) { #loginfo( "'${functionClass} deduct'" ) ${functionClass} otherGroupBy = (${functionClass}) other; Map<${keyClass}, ${calcStateClass}> sourceMap = otherGroupBy.calcState.getInstanceMap(); Map<${keyClass}, ${calcStateClass}> targetMap = calcState.getInstanceMap(); sourceMap .entrySet() .forEach( (Map.Entry<${keyClass}, ${calcStateClass}> e) -> { if (targetMap.containsKey(e.getKey())) { final ${calcStateClass} sourceState = e.getValue(); ${calcStateClass} newInstance = calcState.getOrCreateInstance(e.getKey()); newInstance.deduct(sourceState); #foreach( $src in $functionSet ) $src.copyToTarget; #end #loginfo( "'deduct existing instance combinCount:{}',newInstance.getCombineCount()" ) if(newInstance.isExpired()){ #loginfo( "'expiring key:{} value:{}', e.getKey(), e.getValue()" ) calcState.expireInstance(e.getKey()); wrappedList.removeItem(newInstance.target); } } else { } }); wrappedList.sort(); } @Override public WrappedList<${targetClass}> comparator(Comparator comparator) { return wrappedList.comparator(comparator); } @Override public WrappedList<${targetClass}> comparing(SerializableFunction comparingFunction) { return wrappedList.comparing(comparingFunction); } @Override public ${functionClass} newInstance(){ return new ${functionClass}(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy