Created
October 21, 2012 21:50
-
-
Save kaakaa/3928649 to your computer and use it in GitHub Desktop.
matrix.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Record> recordList = new ArrayList<Record>() | |
recordList.add(new Record().build("A1","B1","C1")) | |
recordList.add(new Record().build("A2","B2","C2")) | |
recordList.add(new Record().build("A3","B3","C3")) | |
recordNames = ["factorA","factorB","factorC"] | |
List<Column> columnList = new ArrayList<Column>() | |
recordNames.each{ columnName -> | |
Column col = new Column() | |
col.name = columnName | |
recordList.each{ col.add(it."$columnName") } | |
columnList.add(col) | |
} | |
for(int i = 0; i <= columnList.size()-1; i++){ | |
if(i > 0){ | |
columnList.get(i-1).column = columnList.get(i) | |
} | |
} | |
firstColumn = columnList.get(0) | |
resultList = firstColumn.createMatrix(new ArrayList<Record>()) | |
resultList.each{ println it.factorA + ":" + it.factorB + ":" + it.factorC + '\n'} | |
class Column{ | |
private String name | |
private Column column | |
private List<String> list = new ArrayList<String>() | |
def add(String name){ | |
if(name != null && !"".equals(name)) | |
this.list.add(name) | |
} | |
def createMatrix(List<Record> recordList){ | |
List<Record> result = new ArrayList<Record>() | |
if(recordList.size() == 0){ | |
list.each{ | |
def newRecord = new Record() | |
newRecord."$name" = it | |
result.add(newRecord) | |
} | |
} else { | |
recordList.each{ record -> | |
list.each{ | |
def newRecord = new Record().clone(record) | |
newRecord."$name" = it | |
result.add(newRecord) | |
} | |
} | |
} | |
if(column != null){ | |
return this.column.createMatrix(result) | |
}else{ | |
return result | |
} | |
} | |
} | |
class Record{ | |
private String factorA | |
private String factorB | |
private String factorC | |
def build(String A, String B,String C){ | |
this.factorA = A | |
this.factorB = B | |
this.factorC = C | |
return this | |
} | |
def clone(Record record){ | |
def recordNames = ["factorA","factorB","factorC"] | |
recordNames.each{ | |
this."$it" = record."$it" | |
} | |
return this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment