arrays - Groovy, insert node after current node -
i'll try best explain situation.
i have following db columns:
oid - task - start - end - realstart - realend my requirement have output following:
oid1 - task1 - start1 - end1 oid2 - task2 - start2 - end2 where task1 task, task2 task + "real", start1 start, start2 realstart, end1 end, end2 realend
but
the first row should created (those start/end fields never empty) second row should created if realstart , realend exist may not true.
inputs 6 arrays (one each column), outputs must 4 arrays, this:
#input oid,task,start,end,realstart,realend #output oid,task,start,end i thinking using oid.each don't know how add nodes after current one. order important in requirement.
for explanation please ask, thanks!
after comment , understanding don't want (or cannot) change input/output data format, here's solution you've asked using classes group data , make easier manage:
import groovy.transform.canonical @canonical class input { string[] oids = [ 'oid1', 'oid2' ] string[] tasks = [ 'task1', 'task2' ] integer[] starts = [ 10, 30 ] integer[] ends = [ 20, 42 ] integer[] realstarts = [ 12, null ] integer[] realends = [ 21, null ] list<object[]> getentries() { // ensure entries have same size def entries = [ oids, tasks, starts, ends, realstarts, realends ] assert entries.collect { it.size() }.unique().size() == 1, 'the input arrays not have same size' return entries } int getsize() { oids.size() // field do, have same length } } @canonical class output { list oids = [ ] list tasks = [ ] list starts = [ ] list ends = [ ] void add( oid, task, start, end, realstart, realend ) { oids << oid; tasks << task; starts << start; ends << end if ( realstart != null && realend != null ) { oids << oid; tasks << task + 'real'; starts << realstart; ends << realend } } } def input = new input() def entries = input.entries def output = new output() ( int = 0; < input.size; i++ ) { def entry = entries.collect { it[ ] } output.add( *entry ) } println output responsibility of arranging data on input class, while responsibility of knowing how organize output data in output class.
running code prints:
output([oid1, oid1, oid2], [task1, task1real, task2], [10, 12, 30], [20, 21, 42]) you can arrays (lists, actually, call toarray() if on list array) output object output.oids, output.tasks, output.starts , output.ends.
the @canonical annotation makes class implement equals, hashcode, tostring , on...
if don't understand something, ask in comments.
Comments
Post a Comment