Skip to content

Commit a6828da

Browse files
authored
Merge pull request #28 from JCEmmons/listfornotes
Change notes to use List<String> throughout. Add addNote() API
2 parents f10bd1e + 1ec9e12 commit a6828da

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/Bundle.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ public final class Bundle {
2626
private Collection<ResourceString> resStrings;
2727

2828
public Bundle () {
29+
this.notes = null;
30+
this.resStrings = null;
2931
}
3032

3133
public Bundle(Collection<ResourceString> resStrings, List<String> notes) {
3234
if (resStrings != null) {
33-
this.resStrings = new ArrayList<>(resStrings);
35+
this.resStrings = new LinkedList<ResourceString>(resStrings);
3436
}
3537
if (notes != null) {
3638
this.notes = new ArrayList<>(notes);
@@ -49,6 +51,9 @@ public void addResourceString(String key, String value, int sequenceNumber) {
4951
}
5052

5153
public void addNote(String note) {
54+
if (notes == null) {
55+
notes = new ArrayList<>();
56+
}
5257
notes.add(note);
5358
}
5459

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/ResourceString.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.ibm.g11n.pipeline.resfilter;
1717

18+
import java.util.ArrayList;
19+
import java.util.Collections;
1820
import java.util.Comparator;
21+
import java.util.List;
1922
import java.util.Objects;
2023

2124
/**
@@ -24,20 +27,20 @@
2427
*/
2528

2629
public final class ResourceString {
27-
private final String note;
2830
private final String key;
2931
private final String value;
3032
private int sequenceNumber;
33+
private List<String> notes;
3134

32-
public ResourceString(String note, String key, String value, int sequenceNumber) {
33-
this.note = note;
35+
public ResourceString(String key, String value, int sequenceNumber, List<String> notes ) {
3436
this.key = key;
3537
this.value = value;
3638
this.sequenceNumber = sequenceNumber;
39+
this.notes = notes == null ? null : new ArrayList<>(notes);
3740
}
38-
41+
3942
public ResourceString(String key, String value, int sequenceNumber) {
40-
this(null, key, value, sequenceNumber);
43+
this(key, value, sequenceNumber, null);
4144
}
4245

4346
public ResourceString(String key, String value) {
@@ -52,8 +55,18 @@ public String getValue() {
5255
return value;
5356
}
5457

55-
public String getNote() {
56-
return note;
58+
public List<String> getNotes() {
59+
if (notes == null) {
60+
return Collections.emptyList();
61+
}
62+
return Collections.unmodifiableList(notes);
63+
}
64+
65+
public void addNote(String note) {
66+
if (notes == null) {
67+
notes = new ArrayList<>();
68+
}
69+
notes.add(note);
5770
}
5871

5972
public void setSequenceNumber(int sequenceNumber) {
@@ -70,10 +83,8 @@ public boolean equals(Object obj) {
7083
return false;
7184
}
7285
ResourceString rs = (ResourceString) obj;
73-
return Objects.equals(this.key, rs.key)
74-
&& Objects.equals(this.value, rs.value)
75-
&& Objects.equals(this.note, rs.note)
76-
&& this.sequenceNumber == rs.sequenceNumber;
86+
return Objects.equals(this.key, rs.key) && Objects.equals(this.value, rs.value)
87+
&& Objects.equals(this.notes, rs.notes) && this.sequenceNumber == rs.sequenceNumber;
7788
}
7889

7990
@Override
@@ -85,8 +96,8 @@ public String toString() {
8596
builder.append(getKey());
8697
builder.append(" Value=");
8798
builder.append(getValue());
88-
builder.append(" Note=");
89-
builder.append(getNote());
99+
builder.append(" Notes=");
100+
builder.append(getNotes().toString());
90101
return builder.toString();
91102
}
92103

@@ -132,7 +143,7 @@ public int compare(ResourceString o1, ResourceString o2) {
132143
cmp = compareStrings(o1.getValue(), o2.getValue());
133144
if (cmp == 0) {
134145
// Note value's natural order as tie-breaker
135-
cmp = compareStrings(o1.getNote(), o2.getNote());
146+
cmp = compareNotes(o1.getNotes(), o2.getNotes());
136147
}
137148
}
138149

@@ -152,5 +163,27 @@ private static int compareStrings(String s1, String s2) {
152163
}
153164
return s1.compareTo(s2);
154165
}
166+
167+
private static int compareNotes(List<String> n1, List<String> n2) {
168+
// null as lowest value
169+
if (n1 == null) {
170+
if (n2 == null) {
171+
return 0;
172+
} else {
173+
return -1;
174+
}
175+
} else if (n2 == null) {
176+
return 1;
177+
}
178+
int cmp = 0;
179+
int index = 0;
180+
while (cmp == 0 && index < n1.size()) {
181+
String s1 = n1.get(index);
182+
String s2 = index < n2.size() ? n2.get(index) : null;
183+
cmp = compareStrings(s1, s2);
184+
index++;
185+
}
186+
return cmp;
187+
}
155188
}
156189
}

0 commit comments

Comments
 (0)