Tenho uma classe Planilha que possui uma Matriz da classe Celula.
A classe Planilha tem um método SetCel(valor, linha, coluna), que insere um valor para a Matriz de Celulas nos índices passados por parâmetros.
Tenho outra Classe Chamada Formula que tem um método Soma(linha,coluna, linha2,coluna2) que recebe como parâmetros quatro inteiros que seriam índices da Matriz de Celula que fica na classe Planilha. Preciso acessar a Matriz da classe Planilha nesse método da Classe Formula, para então através dos índices planilha[0][0] somar com planilha[1][1].
/** * @author itamar */publicclassSpreadSheet{privateMap<Cell,Integer>cells;// Cell/ValueprivateSpreadSheet(){this.cells=newHashMap<>();}publicintgetCellValue(Cellcell){returncells.getOrDefault(cell,0);}publicStream<Integer>getValueStream(CellSelectionselection){returnselection.getCellStream().map(e->getCellValue(e));}publicvoidsetCellValue(Cellcell,intvalue){cells.computeIfAbsent(cell,e->value);}publicvoidsetCellValues(CellSelectionselection,Integervalue){selection.getCellStream().forEach(e->setCellValue(e,value));}staticclassCellSelection{privatefinalSet<Cell>selectedCells;publicCellSelection(Cell...initialSelection){this.selectedCells=newHashSet<>();Stream.of(initialSelection).forEach(e->this.addCell(e));}publicCellSelectionaddCell(Cellcell){this.selectedCells.add(cell);returnthis;}publicCellSelectionremoveCell(Cellcell){this.selectedCells.remove(cell);returnthis;}publicOpenSelectionfrom(Cellcell){returnnewOpenSelection(cell);}classOpenSelection{privatefinalCellfromCell;publicOpenSelection(CellfromCell){this.fromCell=fromCell;}publicCellSelectionto(CelltoCell){for(intcol=fromCell.getCol();col<=toCell.getCol();col++){for(introw=fromCell.getRow();row<=toCell.getRow();row++){addCell(newCell(col,row));}}returnCellSelection.this;}}publicStream<Cell>getCellStream(){returnselectedCells.stream();}}staticclassCell{privatefinalintcol;privatefinalintrow;publicCell(intcol,introw){this.col=col;this.row=row;}publicintgetCol(){returncol;}publicintgetRow(){returnrow;}publicCellSelectiontoCellSelection(){returnnewCellSelection(this);}@Overridepublicbooleanequals(Objecto){// Intentionally only col and rowif(this==o)returntrue;if(!(oinstanceofCell))returnfalse;Cellcell=(Cell)o;if(col!=cell.col)returnfalse;returnrow==cell.row;}@OverridepublicinthashCode(){// Only col and row intentionallyintresult=col;result=31*result+row;returnresult;}}publicstaticvoidmain(String...args){SpreadSheetspreadSheet=newSpreadSheet();spreadSheet.setCellValue(newCell(1,1),10);// Set value for 1 X 1CellSelectionsetSelection=// Let's try with many cellsnewCellSelection(newCell(2,2)).addCell(newCell(2,3)).from(newCell(3,3))// Select range from 3 X 3 to 5 X 10.to(newCell(5,10));spreadSheet.setCellValues(setSelection,4);// Let's select a few cells for the operationsCellSelectionopSelection=newCellSelection().from(newCell(1,1)).to(newCell(5,5));// SumSystem.out.println("Sum: "+spreadSheet.getValueStream(opSelection).mapToInt(e->e)// This will be useful when you have other data types to handle.sum());// CountSystem.out.println("Count: "+spreadSheet.getValueStream(opSelection).mapToInt(e->e)// This will be useful when you have other data types to handle.count());// AverageSystem.out.println("Average: "+spreadSheet.getValueStream(opSelection).mapToInt(e->e)// This will be useful when you have other data types to handle.average().getAsDouble());}}