2017-04-14 132 views
0

我會盡我所能來解釋這種情況。Groovy,在當前節點之後插入節點

我有以下DB列:

oid - task - start - end - realstart - realend 

我的要求是有類似以下的輸出:

oid1 - task1 - start1 - end1 
oid2 - task2 - start2 - end2 

其中TASK1是task,TASK2是task + "real",啓動1是start,START2是realstart,end1是end,end2是realend

BUT

在第一行應始終被創建(那些start/end字段是從未空)的第二行只應創建如果realstartrealend存在可能不是正確的。

輸入是6門陣列(每列一個),輸出必須是4個陣列,這樣的事情:

#input oid,task,start,end,realstart,realend 
#output oid,task,start,end 

我想使用類似oid.each,但我不知道如何添加節點在當前之後。訂單在要求中很重要。

對於任何解釋請問,謝謝!

回答

1

,你不希望(或不能)改變輸入/輸出數據格式,您的評論和理解後,這裏的另一個解決方案,它可以滿足您所要求的使用類對數據進行分組並使其更易於管理:

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 all entries have the same size 
     def entries = [ oids, tasks, starts, ends, realstarts, realends ] 

     assert entries.collect { it.size() }.unique().size() == 1, 
       'The input arrays do not all have the same size' 

     return entries 
    } 

    int getSize() { 
     oids.size() // any field would do, they have the 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() 

for (int i = 0; i < input.size; i++) { 
    def entry = entries.collect { it[ i ] } 
    output.add(*entry) 
} 

println output 

安排數據的責任在Input課上,知道如何組織輸出數據的責任在Output課上。

運行這段代碼打印:

Output([oid1, oid1, oid2], [task1, task1real, task2], [10, 12, 30], [20, 21, 42]) 

你可以從output對象的數組(列表,實際上,卻叫toArray()如果在列表獲取數組)與output.oidsoutput.tasksoutput.startsoutput.ends

@Canonical註釋只是使類實現equals,hashCode時的toString等等...

如果你不明白的地方,請在評論中。

1

如果您需要一個「數組」,其大小從一開始就不知道,則應該使用List來代替。但在Groovy中,這非常易於使用。

下面是一個例子:

final int OID = 0 
final int TASK = 1 
final int START = 2 
final int END = 3 
final int R_START = 4 
final int R_END = 5 

List<Object[]> input = [ 
     //oid,  task, start, end, realstart, realend 
     [ 'oid1', 'task1', 10, 20, 12, 21 ], 
     [ 'oid2', 'task2', 30, 42, null, null ] 
] 

List<List> output = [ ] 

input.each { row -> 
    output << [ row[ OID ], row[ TASK ], row[ START ], row[ END ] ] 
    if (row[ R_START ] && row[ R_END ]) { 
     output << [ row[ OID ], row[ TASK ] + 'real', row[ R_START ], row[ R_END ] ] 
    } 
} 

println output 

,輸出:

[[oid1, task1, 10, 20], [oid1, task1real, 12, 21], [oid2, task2, 30, 42]] 
+0

感謝您的有用解決方案,但在我的情況下,列表中的每個屬性都是一個數組,所有長度都相同,所以我總共有6個數組,它的工作原理是否相同? – GiLA3

+0

爲什麼你會以這種奇怪的格式獲取數據?無論如何,我會編輯答案以嘗試提供幫助。 – Renato