Coverage Summary for Class: Cell (it.polimi.ingsw.view.model)

Class Method, % Line, %
Cell 100% (4/ 4) 100% (27/ 27)
Cell$1 100% (1/ 1) 100% (1/ 1)
total 100% (5/ 5) 100% (28/ 28)


1 package it.polimi.ingsw.view.model; 2  3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 import java.util.stream.Collectors; 7  8 import com.google.gson.Gson; 9 import com.google.gson.reflect.TypeToken; 10  11 /** 12  * A Stack of a cell in the Board 13  */ 14 public class Cell implements RawObj { 15  /** 16  * Stack of Block in the Board Cell 17  */ 18  private ArrayList<Block> blocks; 19  20  /** 21  * Cell Constructor 22  * 23  * @param blocks Block Stack on the Cell 24  */ 25  public Cell(List<Block> blocks) { 26  this.blocks = (ArrayList<Block>) blocks.stream().map(Block::new).collect(Collectors.toList()); 27  } 28  29  /** 30  * Get the Block Stack 31  * 32  * @return block stack 33  */ 34  public List<Block> getBlocks() { 35  return new Gson().fromJson(new Gson().toJson(blocks), new TypeToken<ArrayList<Block>>() { 36  }.getType()); 37  } 38  39  /** 40  * verify if this cell is equals to b 41  * 42  * @param b confront cell 43  * @return true if this equals b else false 44  */ 45  public boolean equals(Cell b) { 46  try { 47  if (blocks.size() != b.getBlocks().size()) { 48  return false; 49  } 50  int size = blocks.size(); 51  for (int i = 0; i < size; i++) { 52  if (!blocks.get(i).getTypeBlock().equals(b.getBlocks().get(i).getTypeBlock())) { 53  return false; 54  } else if (blocks.get(i).getTypeBlock().toUpperCase().equals("WORKER") 55  && !blocks.get(i).getColor().equals(b.getBlocks().get(i).getColor())) { 56  return false; 57  } 58  } 59  return true; 60  } catch (Exception e) { 61  return false; 62  } 63  } 64  65  @Override 66  public List<String> getRawData() { 67  List<String> toSend = (ArrayList<String>) blocks.stream().map(e -> e.getRawData().get(0)) 68  .collect(Collectors.toList()); 69  if (blocks.size() > 0 && blocks.get(blocks.size() - 1).getTypeBlock().equals("WORKER")) 70  toSend.add("- W -"); 71  if (!toSend.isEmpty()) { 72  int offset = toSend.get(toSend.size() - 1).equals("DOME") ? 1 : 0; 73  offset += (toSend.get(toSend.size() - 1).equals("- W -") ? 2 : 0); 74  toSend = new ArrayList<>(toSend.subList(Math.max(0, toSend.size() - 1 - offset), toSend.size())); 75  } 76  Collections.reverse(toSend); 77  return toSend; 78  } 79  80 }