77import java .util .*;
88import java .util .stream .Collectors ;
99
10- public abstract class DFSMethods {
11- private static final Logger LOGGER = LoggerFactory .getLogger (DFSMethods .class );
10+ public abstract class SlicingMethods {
11+ private static final Logger LOGGER = LoggerFactory .getLogger (SlicingMethods .class );
1212
13- private DFSMethods () {
13+ private SlicingMethods () {
1414 }
1515
1616 /**
17- * For each slice find all available steps.<br>
18- * If founded start position that haven't any steps and it is unvalid ->
19- * remove this slice from startPositions and add all it's cells to pizza.
17+ * Find all available steps for a given start positions, considering:<br>
18+ * * slice instructions for a given pizza<br>
19+ * * start position + slices should form a rectangle<br>
20+ * <p>
21+ * If there is no steps fo a given start position:<br>
22+ * 1. A start position is valid as a slice and can be cutted -> move it to output slices<br>
23+ * 2. A start position ISN'T valid as a slice -> remove it from startPositions and add all it cells back to a pizza
24+ *
2025 * @param pizza given pizza
21- * @param startPositions given slices in the pizza
22- * @param output
26+ * @param startPositions given start positions in the pizza(a slice with cells number 1..max slice cells number)
27+ * @param output list of valid and cutted slices
2328 * @return available steps
2429 */
2530 public static Map <Slice , List <Step >> getAvailableSteps (Pizza pizza , List <Slice > startPositions , List <Slice > output ) {
26- Map <Slice , List <Step >> groupedSteps = new HashMap <>();
27- Iterator iter = startPositions .iterator ();
28- while (iter .hasNext ()) {
29- Slice startPosition = (Slice ) iter .next ();
31+ Map <Slice , List <Step >> groupedByAStartPositionSteps = new HashMap <>();
32+ Iterator iterator = startPositions .iterator ();
33+ while (iterator .hasNext ()) {
34+ Slice startPosition = (Slice ) iterator .next ();
3035
3136 List <Step > steps = new ArrayList <>();
3237 Step stepLeft = startPosition .generateStepLeft (pizza );
@@ -38,71 +43,69 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
3843 steps .add (stepLeft );
3944 steps .add (stepBelow );
4045 steps .add (stepAbove );
41- steps = steps .stream ().filter (Objects ::nonNull ).collect (Collectors .toList ());
42-
43- if (steps .size () == 0 ) {
46+ steps = steps .stream ()
47+ .filter (Objects ::nonNull )
48+ .collect (Collectors .toList ());
49+ LOGGER .debug ("There is no steps fo a given start position !" );
50+ if (steps .isEmpty ()) {
51+ LOGGER .debug ("A start position is valid as a slice and can be cutted ->" +
52+ " move it to output slices" );
4453 if (startPosition .isValid (pizza )) {
45- // if slice is valid and have'nt any steps -> cut it from
46- // startPositions
4754 output .add (startPosition );
48- iter .remove ();
55+ iterator .remove ();
4956 } else {
50- // if slice isn't valid and have'nt any steps -> return all
51- // it cells to pizza
57+ LOGGER . warn ( "A start position ISN'T valid as a slice -> " +
58+ "remove it from startPositions and add all it cells" );
5259 pizza .getCells ().addAll (startPosition .cells );
53- iter .remove ();
60+ iterator .remove ();
5461 }
5562 } else {
56- groupedSteps .put (startPosition , steps );
63+ groupedByAStartPositionSteps .put (startPosition , steps );
5764 }
5865 }
5966 LOGGER .info ("available steps for" +
6067 "\n pizza: " + pizza
61- + "\n steps: " + groupedSteps );
62- return groupedSteps ;
68+ + "\n steps: " + groupedByAStartPositionSteps );
69+ return groupedByAStartPositionSteps ;
6370 }
6471
6572 /**
66- * Pick-ups a step with a minimal cells delta number,
67- * execute it(cut it from the pizza, and add to a slice)
73+ * Performs a step with a minimal cells delta number and executes it (cut it from a pizza, and add to a slice)
6874 *
69- * @param pizza given pizza
70- * @param step step to perform
71- * @param startPositions
72- *@param output @return formed slice that includes an original slice and delta from a step
75+ * @param pizza given pizza
76+ * @param step step to perform
77+ * @param startPositions given start positions in the pizza(a slice with cells number 1..max slice cells number)
78+ * @param output list of valid and cutted slices
7379 */
7480 public static void performStep (Pizza pizza , Step step , List <Slice > startPositions , List <Slice > output ) {
7581 //1. Pick ups a steps list with minimal total cells number
76- LOGGER .info ("STEP TO PERFORM " + step );
77- //2. Cut all the step delta cells from pizza
78- LOGGER .info ("pizza before step: " + pizza
82+ LOGGER .debug ("STEP TO PERFORM " + step );
83+ //2. Cut all the step delta cells from a pizza
84+ LOGGER .debug ("pizza before step: " + pizza
7985 + "\n delta to remove from the pizza: " + step .delta );
8086 pizza .getCells ().removeAll (step .delta .cells );
81-
8287 //3. remove previous version start position from startPositions
8388 startPositions .remove (step .startPosition );
84-
8589 List <Cell > returnedList = step .startPosition .cells ;
8690 returnedList .addAll (step .delta .cells );
8791 Slice finalSlice = new Slice (returnedList );
88-
89- LOGGER .info ("PIZZA AFTER STEP:" + pizza );
92+ LOGGER .debug ("PIZZA AFTER STEP:" + pizza );
9093 //3. Add the step cells to an output slice
91-
92- if (finalSlice .cells .size () == pizza .getSliceInstruction ().getMaxNumberOfCellsPerSlice ()){
94+ if (finalSlice .cells .size () == pizza .getSliceInstruction ().getMaxNumberOfCellsPerSlice ()) {
9395 output .add (finalSlice );
94- } else {
96+ } else {
9597 startPositions .add (finalSlice );
9698 }
9799 }
98100
99101 /**
100102 * Selects a step which start position has minimal delta in all the steps
101103 *
102- * @param steps
103- * @return
104+ * @param steps available steps
105+ * @return a step with minimal delta
104106 */
105107 public static Step selectStep (Map <Slice , List <Step >> steps ) {
108+ //TODO test and refactor this peace of shit properly !!
106109 List <Step > min = steps .values ().stream ()
107110 .min (Comparator .comparingLong (value -> value .stream ().map (step -> step .delta .cells .size ()).count ())).get ();
108111 if (!min .isEmpty ()) {
@@ -119,13 +122,15 @@ public static Step selectStep(Map<Slice, List<Step>> steps) {
119122 }
120123
121124 /**
122- * Finds a cells type with minimal cells numbers and generates one cell slices from them
123- * Delete the slices from the pizza
125+ * * Finds a cell type(tomato or mushroom) with minimal cells numbers<br>
126+ * * Generates a list of one cell slices from them<br>
127+ * * Deletes the slices from the pizza<br>
124128 *
125129 * @param pizza given pizza
126- * @return slices that are start positions for future slicing
130+ * @return slices that are start positions for future slicing process
127131 */
128132 public static List <Slice > cutAllStartPositions (Pizza pizza ) {
133+ //1.Finds a cell type(tomato or mushroom) with minimal cells numbers
129134 List <Cell > mushrooms = pizza .getCells ().stream ()
130135 .filter (cell -> cell .ingredient .equals (Ingredient .MUSHROOM ))
131136 .collect (Collectors .toList ());
@@ -154,7 +159,7 @@ public static List<Slice> cutAllStartPositions(Pizza pizza) {
154159 .collect (Collectors .toList ());
155160 pizza .getCells ().removeAll (cellsToRemove );
156161 }
157- LOGGER .info ("pizza without start positions:"
162+ LOGGER .debug ("pizza with removed start positions:"
158163 + "\n " + pizza );
159164 return startPositions ;
160165 }
0 commit comments