-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveReport.java
116 lines (90 loc) · 2.28 KB
/
MoveReport.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Author: Jacob Schramkowski
/**
* A class containing information about a MapList entry modification
* @param <K> the key class type
* @param <V> the value class type
*/
public class MoveReport<K, V> {
// enum for type of modification outcome
public enum Type {
INSERT,
MOVE,
UPDATE,
REMOVE,
NONE
}
private int prevIndex, newIndex;
private K key;
private V value;
public MoveReport(int prevIndex, int newIndex, K key, V value){
this.prevIndex = prevIndex;
this.newIndex = newIndex;
this.key = key;
this.value = value;
}
public Type type(){
if(prevIndex != -1){
if(newIndex == -1){
return Type.REMOVE;
} else if(newIndex != prevIndex){
return Type.MOVE;
} else {
return Type.UPDATE;
}
} else {
if(newIndex == -1){
return Type.NONE;
} else {
return Type.INSERT;
}
}
}
/**
* Gets the index of the entry before the operation
* @return the previous index of the modified entry
*/
public int from() {
return prevIndex;
}
/**
* Gets the index of the entry after the operation
* @return the new index of the modified entry
*/
public int to() {
return newIndex;
}
/**
* Gets the key of the modified entry
* @return the key of the entry
*/
public K key(){
return key;
}
/**
* Gets the value of the modified entry
* @return the value of the entry
*/
public V value() {
return value;
}
@Override public String toString() {
switch (type()) {
case INSERT: {
return String.format("Inserted key \"%s\" at index %d", key, newIndex);
}
case UPDATE: {
return String.format("Updated key \"%s\" at index %d", key, newIndex);
}
case MOVE: {
return String.format("Moved key \"%s\" from index %d to index %d", key, prevIndex, newIndex);
}
case REMOVE: {
return String.format("Removed key \"%s\" from index %d", key, prevIndex);
}
case NONE: {
return String.format("Key \"%s\" not contained", key);
}
}
throw new IllegalStateException("Move type not properly set");
}
}