Skip to content

Commit 053b19b

Browse files
author
bjrara
committed
REFACTOR
1 parent 47645dc commit 053b19b

File tree

5 files changed

+89
-66
lines changed

5 files changed

+89
-66
lines changed

project/src/main/java/com/offline/training/project/pj1/DoublyLinkedList.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ public void add(int runLength, int red, int green, int blue) {
2626
if (!(validateColor(red) && validateColor(green) && validateColor(blue))) {
2727
throw new RuntimeException("Invalid pixel value found!");
2828
}
29-
DoublyLinkedNode appendObj = new DoublyLinkedNode(runLength, red,
30-
green, blue);
29+
DoublyLinkedNode appendObj = new DoublyLinkedNode(new Run(red, green, blue, runLength));
3130
if (head == null) {
3231
head = tail = appendObj;
3332
} else {
34-
appendObj.setPrevious(tail);
33+
appendObj.setPrevious(tail);
3534
tail.setNext(appendObj);
3635
tail = tail.getNext();
3736
}
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,12 @@
11
package com.offline.training.project.pj1;
22

33
public class DoublyLinkedNode {
4-
private int red;
5-
private int green;
6-
private int blue;
7-
private int runLength;
8-
4+
private Run value;
95
private DoublyLinkedNode previous;
106
private DoublyLinkedNode next;
117

12-
public DoublyLinkedNode(int runLength, int red, int green, int blue) {
13-
this.runLength = runLength;
14-
this.red = red;
15-
this.green = green;
16-
this.blue = blue;
17-
}
18-
19-
public void Increment() {
20-
runLength++;
21-
}
22-
23-
public void Decrement() {
24-
runLength--;
25-
}
26-
27-
public int getRed() {
28-
return red;
29-
}
30-
31-
public int getGreen() {
32-
return green;
33-
}
34-
35-
public int getBlue() {
36-
return blue;
37-
}
38-
39-
public int getRunLength() {
40-
return runLength;
41-
}
42-
43-
public void setValue(int red, int green, int blue, int runLength) {
44-
this.red = red;
45-
this.green = green;
46-
this.blue = blue;
47-
this.runLength = runLength;
8+
public DoublyLinkedNode(Run value) {
9+
this.value = value;
4810
}
4911

5012
public void setPrevious(DoublyLinkedNode previous) {
@@ -62,4 +24,8 @@ public void setNext(DoublyLinkedNode next) {
6224
public DoublyLinkedNode getNext() {
6325
return next;
6426
}
27+
28+
public Run getValue() {
29+
return value;
30+
}
6531
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.offline.training.project.pj1;
2+
3+
public class Run {
4+
private int red;
5+
private int green;
6+
private int blue;
7+
private int runLength;
8+
9+
public Run(int red, int green, int blue, int runLength) {
10+
this.red = red;
11+
this.green = green;
12+
this.blue = blue;
13+
this.runLength = runLength;
14+
}
15+
16+
public void setValue(int red, int green, int blue, int runLength) {
17+
this.red = red;
18+
this.green = green;
19+
this.blue = blue;
20+
this.runLength = runLength;
21+
}
22+
23+
public int getRed() {
24+
return red;
25+
}
26+
27+
public void setRed(int red) {
28+
this.red = red;
29+
}
30+
31+
public int getGreen() {
32+
return green;
33+
}
34+
35+
public void setGreen(int green) {
36+
this.green = green;
37+
}
38+
39+
public int getBlue() {
40+
return blue;
41+
}
42+
43+
public void setBlue(int blue) {
44+
this.blue = blue;
45+
}
46+
47+
public int getRunLength() {
48+
return runLength;
49+
}
50+
51+
public void setRunLength(int runLength) {
52+
this.runLength = runLength;
53+
}
54+
55+
public void Increment() {
56+
runLength++;
57+
}
58+
59+
public void Decrement() {
60+
runLength--;
61+
}
62+
}

project/src/main/java/com/offline/training/project/pj1/RunIterator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public int[] next() {
8989
if (next != null) {
9090
DoublyLinkedNode currNext = next;
9191
next = next.getNext();
92-
return new int[] { currNext.getRunLength(), currNext.getRed(),
93-
currNext.getGreen(), currNext.getBlue() };
92+
return new int[] { currNext.getValue().getRunLength(), currNext.getValue().getRed(),
93+
currNext.getValue().getGreen(), currNext.getValue().getBlue() };
9494
}
9595
throw new NoSuchElementException("Iterator reaches the end of the list.");
9696
}

project/src/main/java/com/offline/training/project/pj1/RunLengthEncoding.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ public RunLengthEncoding(PixImage image) {
203203
image.getBlue(0, 0));
204204
continue;
205205
}
206-
if (runs.tail.getRed() == (int) image.getRed(x, y)
207-
&& runs.tail.getGreen() == (int) image.getGreen(x, y)
208-
&& runs.tail.getBlue() == (int) image.getBlue(x, y)) {
209-
runs.tail.Increment();
206+
if (runs.tail.getValue().getRed() == (int) image.getRed(x, y)
207+
&& runs.tail.getValue().getGreen() == (int) image.getGreen(x, y)
208+
&& runs.tail.getValue().getBlue() == (int) image.getBlue(x, y)) {
209+
runs.tail.getValue().Increment();
210210
} else {
211211
runs.add(1, (int) image.getRed(x, y),
212212
(int) image.getGreen(x, y),
@@ -267,27 +267,27 @@ public void setPixel(int x, int y, short red, short green, short blue) {
267267

268268
while (curr != null) {
269269
previousIndex = currIndex;
270-
currIndex += curr.getRunLength();
270+
currIndex += curr.getValue().getRunLength();
271271
if (currIndex >= index)
272272
break;
273273
curr = curr.getNext();
274274
}
275275

276-
if (curr.getRed() == (int) red && curr.getGreen() == (int) green
277-
&& curr.getBlue() == (int) blue) {
276+
if (curr.getValue().getRed() == (int) red && curr.getValue().getGreen() == (int) green
277+
&& curr.getValue().getBlue() == (int) blue) {
278278
return;
279279
} else {
280280
if (index == previousIndex + 1) {
281281
if (tryMerge(curr, curr.getPrevious(), red, green, blue)) {
282-
if (curr.getRunLength() == 0) {
282+
if (curr.getValue().getRunLength() == 0) {
283283
curr.getPrevious().setNext(curr.getNext());
284284
}
285285
return;
286286
}
287287
}
288288
if (index == currIndex) {
289289
if (tryMerge(curr, curr.getNext(), red, green, blue)) {
290-
if (curr.getRunLength() == 0) {
290+
if (curr.getValue().getRunLength() == 0) {
291291
curr.getNext().setPrevious(curr.getPrevious());
292292
}
293293
return;
@@ -305,10 +305,10 @@ private boolean tryMerge(DoublyLinkedNode curr, DoublyLinkedNode target,
305305
int red, int green, int blue) {
306306
if (target == null)
307307
return false;
308-
if (target.getRed() == (int) red && target.getGreen() == (int) green
309-
&& target.getBlue() == (int) blue) {
310-
target.Increment();
311-
curr.Decrement();
308+
if (target.getValue().getRed() == (int) red && target.getValue().getGreen() == (int) green
309+
&& target.getValue().getBlue() == (int) blue) {
310+
target.getValue().Increment();
311+
curr.getValue().Decrement();
312312
return true;
313313
}
314314
return false;
@@ -317,13 +317,9 @@ private boolean tryMerge(DoublyLinkedNode curr, DoublyLinkedNode target,
317317
private void split(DoublyLinkedNode curr, int red, int green, int blue,
318318
int previousRunLength, int nextRunLength) {
319319
DoublyLinkedNode previousRun, nextRun;
320-
previousRun = previousRunLength <= 0 ? curr.getPrevious()
321-
: new DoublyLinkedNode(previousRunLength, (int) curr.getRed(),
322-
(int) curr.getGreen(), (int) curr.getBlue());
323-
nextRun = nextRunLength <= 0 ? curr.getNext() : new DoublyLinkedNode(
324-
nextRunLength, (int) curr.getRed(), (int) curr.getGreen(),
325-
(int) curr.getBlue());
326-
curr.setValue(red, green, blue, 1);
320+
previousRun = previousRunLength <= 0 ? curr.getPrevious() : new DoublyLinkedNode(new Run((int) curr.getValue().getRed(), (int) curr.getValue().getGreen(), (int) curr.getValue().getBlue(), previousRunLength));
321+
nextRun = nextRunLength <= 0 ? curr.getNext() : new DoublyLinkedNode(new Run((int) curr.getValue().getRed(),(int) curr.getValue().getGreen(), (int) curr.getValue().getBlue(), nextRunLength));
322+
curr.getValue().setValue(red, green, blue, 1);
327323
if (previousRunLength > 0) {
328324
if (curr.getPrevious() != null)
329325
curr.getPrevious().setNext(previousRun);

0 commit comments

Comments
 (0)