This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.java
72 lines (57 loc) · 1.75 KB
/
State.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
public class State {
private String text;
private Date date;
public State(String text) {
this.text = text;
date = new Date();
}
public List<Difference> getDifferences(State that)
{
Scanner thisScanner = new Scanner(text);
Scanner thatScanner = new Scanner(that.text);
int lineNumber = 0;
List<Difference> result = new LinkedList<>();
String thisLine;
String thatLine;
while (thisScanner.hasNextLine() && thatScanner.hasNextLine())
{
thisLine = thisScanner.nextLine();
thatLine = thatScanner.nextLine();
if (!thisLine.equals(thatLine))
{
result.add(new Difference(lineNumber, thisLine, thatLine));
}
lineNumber++;
}
while (thisScanner.hasNextLine())
{
thisLine = thisScanner.nextLine();
result.add(new Difference(lineNumber, thisLine, ""));
lineNumber++;
}
while (thatScanner.hasNextLine())
{
thatLine = thatScanner.nextLine();
result.add(new Difference(lineNumber, "", thatLine));
lineNumber++;
}
return result;
}
@Override
public String toString() {
return "Saved: " + date + "\n"
+ text;
}
@Override
public boolean equals(Object that) {
if (this == that) return true;
if (that == null || ! (that instanceof State)) return false;
State state = (State) that;
return text.equals(state.text);
}
@Override
public int hashCode() {
return Objects.hash(text);
}
}