patternjavaMinor
Splitting two lists into OnlyA, Both, and OnlyB
Viewed 0 times
intoonlyasplittingbothtwolistsandonlyb
Problem
This is a part of my Minesweeper analyzer code that didn't fit into the last question.
This code uses
I am mostly wondering: Can any simplifications be made in the
The idea is to split two
The reason for why I am using
For testing purposes the
```
class FieldGroup extends ArrayList {
}
public class FieldGroupSplit {
private final FieldGroup onlyA;
private final FieldGroup both;
private final FieldGroup onlyB;
private FieldGroupSplit(FieldGroup onlyA, FieldGroup both, FieldGroup onlyB) {
this.onlyA = onlyA;
this.onlyB = onlyB;
this.both = both;
}
public FieldGroup getBoth() {
return both;
}
public FieldGroup getOnlyA() {
return onlyA;
}
public FieldGroup getOnlyB() {
return onlyB;
}
public boolean splitPerformed() {
return !onlyA.isEmpty() || !onlyB.isEmpty();
}
@Override
public String toString() {
return "FieldGroupSplit:" + onlyA + " -- " + both + " -- " + onlyB;
}
public static FieldGroupSplit split(FieldGroup a, FieldGroup b) {
if (a == b) {
return null;
}
if (Collections.disjoint(a, b)) {
return null; // Return if the groups have no fields in common
}
FieldGroup both = new Field
This code uses
FieldGroup which extends ArrayList because I am using some more data than just the elements themselves.I am mostly wondering: Can any simplifications be made in the
split method? Even though the current code is fast enough, I am wondering if it can be optimized further.The idea is to split two
FieldGroups (A and B) into three parts:onlyA: Elements that exists inAbut not inB
both: Elements that exists in bothAandB
onlyB: Elements that exists inBbut not inA
The reason for why I am using
List instead of Set is that I want the capability to have multiple equal elements. The order does not matter of the elements. I am aware of Guava's MultiSet but I'm not a big fan of introducing a dependency in this code.For testing purposes the
FieldGroup class can be as follows:```
class FieldGroup extends ArrayList {
}
public class FieldGroupSplit {
private final FieldGroup onlyA;
private final FieldGroup both;
private final FieldGroup onlyB;
private FieldGroupSplit(FieldGroup onlyA, FieldGroup both, FieldGroup onlyB) {
this.onlyA = onlyA;
this.onlyB = onlyB;
this.both = both;
}
public FieldGroup getBoth() {
return both;
}
public FieldGroup getOnlyA() {
return onlyA;
}
public FieldGroup getOnlyB() {
return onlyB;
}
public boolean splitPerformed() {
return !onlyA.isEmpty() || !onlyB.isEmpty();
}
@Override
public String toString() {
return "FieldGroupSplit:" + onlyA + " -- " + both + " -- " + onlyB;
}
public static FieldGroupSplit split(FieldGroup a, FieldGroup b) {
if (a == b) {
return null;
}
if (Collections.disjoint(a, b)) {
return null; // Return if the groups have no fields in common
}
FieldGroup both = new Field
Solution
Just a few quick minor notes:
-
This comment:
could be replaced with an explanatory local variable:
-
The same is true for this one:
Result:
-
160-character long line width is a little bit long here:
A few linebreak would avoid horizontal scrolling.
-
The following patterns is duplicated in the test method:
I would create a
-
I would consider using
-
This comment:
// Check if ALL fields are in common
if (onlyA.isEmpty() && onlyB.isEmpty()) {could be replaced with an explanatory local variable:
boolean allFieldsAreInCommon = onlyA.isEmpty() && onlyB.isEmpty();
if (allFieldsAreInCommon) {-
The same is true for this one:
if (Collections.disjoint(a, b)) {
return null; // Return if the groups have no fields in common
}Result:
boolean noFieldsInCommon = Collections.disjoint(a, b);
if (noFieldsInCommon) {
return null;
}-
160-character long line width is a little bit long here:
// If this is called in a loop an infinite loop can occur if we don't do this because we're creating a NEW object all the time to hold them both.
// We should reuse one of the existing ones and go back to using == above.A few linebreak would avoid horizontal scrolling.
-
The following patterns is duplicated in the test method:
new FieldGroup(Arrays.asList("a", "b", "c"))I would create a
createFieldGroup(String...) method for that.-
I would consider using
Optional from Guava instead of null return values.Code Snippets
// Check if ALL fields are in common
if (onlyA.isEmpty() && onlyB.isEmpty()) {boolean allFieldsAreInCommon = onlyA.isEmpty() && onlyB.isEmpty();
if (allFieldsAreInCommon) {if (Collections.disjoint(a, b)) {
return null; // Return if the groups have no fields in common
}boolean noFieldsInCommon = Collections.disjoint(a, b);
if (noFieldsInCommon) {
return null;
}// If this is called in a loop an infinite loop can occur if we don't do this because we're creating a NEW object all the time to hold them both.
// We should reuse one of the existing ones and go back to using == above.Context
StackExchange Code Review Q#56545, answer score: 5
Revisions (0)
No revisions yet.