Skip to content

Commit 33f2bba

Browse files
committed
Change notes to use List<String> throughout. Add addNote() API
1 parent f10bd1e commit 33f2bba

2 files changed

Lines changed: 59 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ 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) {
36-
this.notes = new ArrayList<>(notes);
38+
this.notes = new ArrayList<String>(notes);
3739
}
3840
}
3941

4042
public void addResourceString(ResourceString resString) {
4143
if (resStrings == null) {
42-
resStrings = new LinkedList<>();
44+
resStrings = new LinkedList<ResourceString>();
4345
}
4446
resStrings.add(resString);
4547
}
@@ -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<String>();
56+
}
5257
notes.add(note);
5358
}
5459

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

Lines changed: 51 additions & 13 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,18 +27,18 @@
2427
*/
2528

2629
public final class ResourceString {
27-
private final String note;
30+
private List<String> notes;
2831
private final String key;
2932
private final String value;
3033
private int sequenceNumber;
3134

32-
public ResourceString(String note, String key, String value, int sequenceNumber) {
33-
this.note = note;
35+
public ResourceString(List<String> notes, String key, String value, int sequenceNumber) {
36+
this.notes = notes;
3437
this.key = key;
3538
this.value = value;
3639
this.sequenceNumber = sequenceNumber;
3740
}
38-
41+
3942
public ResourceString(String key, String value, int sequenceNumber) {
4043
this(null, key, value, sequenceNumber);
4144
}
@@ -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<String>();
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,32 @@ 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;
183+
try {
184+
s2 = n2.get(index);
185+
} catch (IndexOutOfBoundsException ex) {
186+
s2 = null;
187+
}
188+
cmp = compareStrings(s1, s2);
189+
index++;
190+
}
191+
return cmp;
192+
}
155193
}
156194
}

0 commit comments

Comments
 (0)