Skip to content

Commit 9351d52

Browse files
Also avoid Collections.singleton*
The same arguments as for Collections.emptyList apply, but additionally, the Immutable* classes forbid null. There were actually some cases of Collections.singleton where forbidden nulls are good because they will make us catch unexpected nulls earlier. Furthermore, the name of Collections.singleton() does not mention that it returns a set, so there were some callers where a list would better fit (because they assigned lists to a variable on some code paths and a singleton set on other paths).
1 parent 347437f commit 9351d52

File tree

7 files changed

+21
-25
lines changed

7 files changed

+21
-25
lines changed

src/org/sosy_lab/java_smt/api/InterpolatingProverEnvironment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
package org.sosy_lab.java_smt.api;
2121

2222
import com.google.common.base.Preconditions;
23+
import com.google.common.collect.ImmutableSet;
2324
import com.google.common.collect.Lists;
2425
import java.util.Collection;
25-
import java.util.Collections;
2626
import java.util.List;
2727

2828
/**
@@ -77,7 +77,7 @@ default List<BooleanFormula> getSeqInterpolants(List<? extends Collection<T>> pa
7777

7878
default List<BooleanFormula> getSeqInterpolants0(List<T> formulas)
7979
throws SolverException, InterruptedException {
80-
return getSeqInterpolants(Lists.transform(formulas, Collections::singleton));
80+
return getSeqInterpolants(Lists.transform(formulas, ImmutableSet::of));
8181
}
8282

8383
/**
@@ -120,7 +120,7 @@ List<BooleanFormula> getTreeInterpolants(
120120

121121
default List<BooleanFormula> getTreeInterpolants0(List<T> formulas, int[] startOfSubTree)
122122
throws SolverException, InterruptedException {
123-
return getTreeInterpolants(Lists.transform(formulas, Collections::singleton), startOfSubTree);
123+
return getTreeInterpolants(Lists.transform(formulas, ImmutableSet::of), startOfSubTree);
124124
}
125125

126126
/** Checks for a valid subtree-structure. This code is taken from SMTinterpol. */

src/org/sosy_lab/java_smt/api/QuantifiedFormulaManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
package org.sosy_lab.java_smt.api;
2121

22-
import java.util.Collections;
22+
import com.google.common.collect.ImmutableList;
2323
import java.util.List;
2424

2525
/**
@@ -56,12 +56,12 @@ default BooleanFormula forall(List<? extends Formula> pVariables, BooleanFormula
5656

5757
/** Syntax sugar, see {@link #forall(List, BooleanFormula)}. */
5858
default BooleanFormula forall(Formula quantifiedArg, BooleanFormula pBody) {
59-
return forall(Collections.singletonList(quantifiedArg), pBody);
59+
return forall(ImmutableList.of(quantifiedArg), pBody);
6060
}
6161

6262
/** Syntax sugar, see {@link #exists(List, BooleanFormula)}. */
6363
default BooleanFormula exists(Formula quantifiedArg, BooleanFormula pBody) {
64-
return exists(Collections.singletonList(quantifiedArg), pBody);
64+
return exists(ImmutableList.of(quantifiedArg), pBody);
6565
}
6666

6767
/**

src/org/sosy_lab/java_smt/example/Interpolation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.google.common.base.Preconditions;
44
import com.google.common.collect.ImmutableList;
5+
import com.google.common.collect.ImmutableSet;
56
import java.util.ArrayList;
6-
import java.util.Collections;
77
import java.util.HashSet;
88
import java.util.List;
99
import java.util.Set;
@@ -111,17 +111,17 @@ private static <T> void interpolateExample(
111111
{
112112
// example 1b :
113113
// alternative solution ... with more code and partitioned formulas.
114-
Set<T> partition0 = Collections.singleton(ip0);
115-
Set<T> partition1 = Collections.singleton(ip1);
116-
Set<T> partition2 = Collections.singleton(ip2);
114+
Set<T> partition0 = ImmutableSet.of(ip0);
115+
Set<T> partition1 = ImmutableSet.of(ip1);
116+
Set<T> partition2 = ImmutableSet.of(ip2);
117117
itps = prover.getSeqInterpolants(ImmutableList.of(partition0, partition1, partition2));
118118
logger.log(Level.INFO, "1b :: Interpolants for [{ip0},{ip1},{ip2}] are:", itps);
119119
}
120120

121121
{
122122
// example 2a :
123123
// get a sequence of interpolants for two formulas: (get-interpolants IP_1 (and IP_0 IP_2)).
124-
Set<T> partition3 = Collections.singleton(ip0);
124+
Set<T> partition3 = ImmutableSet.of(ip0);
125125
Set<T> partition4 = new HashSet<>();
126126
partition4.add(ip1);
127127
partition4.add(ip2);

src/org/sosy_lab/java_smt/solvers/princess/PrincessModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import com.google.common.collect.ImmutableSet;
3838
import java.util.ArrayList;
3939
import java.util.Collection;
40-
import java.util.Collections;
4140
import java.util.HashMap;
4241
import java.util.Map;
4342
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -135,7 +134,7 @@ private Map<IIntLit, ITerm> getArrayAddresses(
135134
}
136135
fKey = creator.getEnv().makeSelect(arrayF, arrayIndex);
137136
name = arrayF.toString();
138-
argumentInterpretations = Collections.singleton(creator.convertValue(arrayIndex));
137+
argumentInterpretations = ImmutableList.of(creator.convertValue(arrayIndex));
139138
break;
140139
case "store":
141140
// array-access, for explanation see #getArrayAddresses
@@ -152,7 +151,7 @@ private Map<IIntLit, ITerm> getArrayAddresses(
152151
fKey = creator.getEnv().makeSelect(arrayF2, arrayIndex2);
153152
fValue = arrayContent;
154153
name = arrayF2.toString();
155-
argumentInterpretations = Collections.singleton(creator.convertValue(arrayIndex2));
154+
argumentInterpretations = ImmutableList.of(creator.convertValue(arrayIndex2));
156155
break;
157156
default:
158157
// normal variable or UF

src/org/sosy_lab/java_smt/solvers/z3/Z3Model.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.microsoft.z3.enumerations.Z3_sort_kind;
2929
import java.util.ArrayList;
3030
import java.util.Collection;
31-
import java.util.Collections;
3231
import java.util.HashSet;
3332
import java.util.List;
3433
import java.util.Set;
@@ -111,7 +110,7 @@ private Collection<ValueAssignment> getConstAssignments(long keyDecl) {
111110
try {
112111
long symbol = Native.getDeclName(z3context, keyDecl);
113112
if (z3creator.isConstant(value)) {
114-
return Collections.singletonList(
113+
return ImmutableList.of(
115114
new ValueAssignment(
116115
z3creator.encapsulateWithTypeOf(var),
117116
z3creator.encapsulateWithTypeOf(value),

src/org/sosy_lab/java_smt/test/InterpolatingProverTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.ImmutableSet;
3030
import com.google.common.collect.Lists;
31-
import java.util.Collections;
3231
import java.util.List;
3332
import java.util.Set;
3433
import org.junit.Test;
@@ -87,7 +86,7 @@ public <T> void simpleInterpolation() throws SolverException, InterruptedExcepti
8786
T id2 = prover.push(f2);
8887
boolean check = prover.isUnsat();
8988
assertThat(check).named("formulas must be contradicting").isTrue();
90-
prover.getInterpolant(Collections.singletonList(id2));
89+
prover.getInterpolant(ImmutableList.of(id2));
9190
// we actually only check for a successful execution here, the result is irrelevant.
9291
}
9392
}
@@ -301,13 +300,13 @@ public <T> void sequentialInterpolation() throws SolverException, InterruptedExc
301300

302301
List<BooleanFormula> itps4 =
303302
stack.getSeqInterpolants(
304-
Lists.transform(ImmutableList.of(TA, TA, TA, TB, TC, TD, TD), Collections::singleton));
303+
Lists.transform(ImmutableList.of(TA, TA, TA, TB, TC, TD, TD), ImmutableSet::of));
305304
List<BooleanFormula> itps5 =
306305
stack.getSeqInterpolants(
307-
Lists.transform(ImmutableList.of(TA, TA, TB, TC, TD, TA, TD), Collections::singleton));
306+
Lists.transform(ImmutableList.of(TA, TA, TB, TC, TD, TA, TD), ImmutableSet::of));
308307
List<BooleanFormula> itps6 =
309308
stack.getSeqInterpolants(
310-
Lists.transform(ImmutableList.of(TB, TC, TD, TA, TA, TA, TD), Collections::singleton));
309+
Lists.transform(ImmutableList.of(TB, TC, TD, TA, TA, TA, TD), ImmutableSet::of));
311310

312311
stack.pop(); // clear stack, such that we can re-use the solver
313312
stack.pop();

src/org/sosy_lab/java_smt/test/SolverFormulaWithAssumptionsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.common.collect.Lists;
2828
import java.math.BigDecimal;
2929
import java.util.ArrayList;
30-
import java.util.Collections;
3130
import java.util.List;
3231
import org.junit.Test;
3332
import org.junit.runner.RunWith;
@@ -112,13 +111,13 @@ public <T> void basicAssumptionsTest()
112111
env.isUnsatWithAssumptions(
113112
ImmutableList.of(bmgr.not(suffix1), bmgr.not(suffix2), suffix3)))
114113
.isTrue();
115-
assertThat(env.getInterpolant(Collections.singletonList(firstPartForInterpolant)).toString())
114+
assertThat(env.getInterpolant(ImmutableList.of(firstPartForInterpolant)).toString())
116115
.doesNotContain("suffix");
117116
assertThat(
118117
env.isUnsatWithAssumptions(
119118
ImmutableList.of(bmgr.not(suffix1), bmgr.not(suffix3), suffix2)))
120119
.isTrue();
121-
assertThat(env.getInterpolant(Collections.singletonList(firstPartForInterpolant)).toString())
120+
assertThat(env.getInterpolant(ImmutableList.of(firstPartForInterpolant)).toString())
122121
.doesNotContain("suffix");
123122
}
124123
}
@@ -160,7 +159,7 @@ public <T> void assumptionsTest()
160159
.isTrue();
161160

162161
for (int j = 0; j < i; j++) {
163-
BooleanFormula itp = env.getInterpolant(Collections.singletonList(ids.get(j)));
162+
BooleanFormula itp = env.getInterpolant(ImmutableList.of(ids.get(j)));
164163
for (String var : mgr.extractVariables(itp).keySet()) {
165164
assertThat(var).doesNotContain("suffix");
166165
}

0 commit comments

Comments
 (0)