Skip to content

Commit 0ef21d9

Browse files
committed
♻️ | Refactoring code (Revisão, otimizações, exceptions costumizadas e nova logica)
1 parent 781319d commit 0ef21d9

File tree

8 files changed

+698
-362
lines changed

8 files changed

+698
-362
lines changed

src/Cloner.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.lang.reflect.InvocationTargetException;
2+
import java.lang.reflect.Method;
3+
4+
public class Cloner {
5+
public static class ClonerException extends Exception {
6+
public ClonerException(String errorMessage) {
7+
super(errorMessage);
8+
}
9+
}
10+
11+
public static Object verifyAndCopy(Object data) throws ClonerException {
12+
if (data instanceof Cloneable)
13+
return deepCopy(data);
14+
15+
return data;
16+
}
17+
18+
public static Object deepCopy(Object data) throws ClonerException {
19+
Class<?> classOfData = data.getClass();
20+
21+
Method methodClone=null;
22+
try
23+
{
24+
methodClone = classOfData.getMethod("clone");
25+
}
26+
catch (NoSuchMethodException error)
27+
{
28+
throw new ClonerException("Classe nao implementa Cloneable!");
29+
}
30+
31+
32+
Object cloneOfObject=null;
33+
try
34+
{
35+
cloneOfObject = methodClone.invoke(data);
36+
}
37+
catch (InvocationTargetException | IllegalAccessException | NullPointerException error)
38+
{
39+
throw new ClonerException("Erro ao clonar objeto!");
40+
}
41+
42+
return cloneOfObject;
43+
}
44+
}

src/Coordenada.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1-
public class Coordenada{
1+
public class Coordenada {
22
private int x;
33
private int y;
44

5-
public Coordenada (int x, int y){
5+
public Coordenada(int x, int y) {
66
this.x = x;
77
this.y = y;
88
}
99

10-
public int getLinha() { return x; }
11-
public int getColuna() { return y; }
10+
public int getLinha() {
11+
return x;
12+
}
13+
14+
public int getColuna() {
15+
return y;
16+
}
17+
18+
public void setLinha(int x) {
19+
if (x < 0)
20+
throw new IllegalArgumentException("Valor inválido para coordenada x: " + x);
21+
this.x = x;
22+
}
1223

13-
public void setLinha(int x) { this.x = x; }
1424
public void setColuna(int y) {
25+
if (y < 0)
26+
throw new IllegalArgumentException("Valor inválido para coordenada y: " + y);
1527
this.y = y;
1628
}
1729

1830
@Override
1931
public boolean equals(Object obj) {
20-
if (this == obj) return true;
32+
if (this == obj)
33+
return true;
2134

22-
if (obj == null) return false;
23-
24-
if (this.getClass() != obj.getClass()) return false;
35+
if (obj == null || this.getClass() != obj.getClass())
36+
return false;
2537

2638
Coordenada cord = (Coordenada) obj;
2739

28-
if (this.x != cord.x) return false;
29-
if (this.y != cord.y) return false;
40+
if (this.x != cord.x || this.y != cord.y)
41+
return false;
3042

3143
return true;
3244
}
@@ -35,42 +47,40 @@ public boolean equals(Object obj) {
3547
public int hashCode() {
3648
int ret = 666;
3749

38-
ret = ret*7+ Integer.valueOf(this.x).hashCode();
39-
ret = ret*7+ Integer.valueOf(this.y).hashCode();
50+
ret = ret * 7 + Integer.valueOf(this.x).hashCode();
51+
ret = ret * 7 + Integer.valueOf(this.y).hashCode();
4052

41-
if (ret<0) ret=-ret;
53+
if (ret < 0)
54+
ret = -ret;
4255

4356
return ret;
4457
}
4558

4659
@Override
4760
public String toString() {
48-
String coordX = this.x < 10 ? "0" + this.x : this.x +"";
49-
String coordY = this.y < 10 ? "0" + this.y : this.y +"";
61+
String coordX = String.format("%02d", x);
62+
String coordY = String.format("%02d", y);
5063

5164
return "(" + coordX + "," + coordY + ")";
5265
}
5366

5467
// construtor de copia
55-
public Coordenada (Coordenada modelo) throws Exception
56-
{
57-
if(modelo == null)
68+
public Coordenada(Coordenada model) throws Exception {
69+
if (model == null)
5870
throw new Exception("Modelo ausente");
5971

60-
this.x = modelo.x;
61-
this.y = modelo.y;
72+
this.x = model.x;
73+
this.y = model.y;
6274
}
6375

64-
public Object clone ()
65-
{
76+
@Override
77+
public Object clone() {
6678
Coordenada ret = null;
6779

68-
try
69-
{
80+
try {
7081
ret = new Coordenada(this);
82+
} catch (Exception ignored) {
7183
}
72-
catch(Exception ignored)
73-
{}
7484

7585
return ret;
7686
}

0 commit comments

Comments
 (0)