Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #433 from thespags/master
Browse files Browse the repository at this point in the history
  • Loading branch information
alicederyn authored Aug 31, 2020
2 parents e19d636 + 618229d commit 43c5581
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public void addTo(SourceBuilder code) {
.addLine(" }")
.addLine("")
.addLine(" @Override public V put(K key, V value) {")
.addLine(" K oldKey = biMap.inverse().get(value);")
.addLine(" %s.checkArgument(", Preconditions.class)
.addLine(" !biMap.containsValue(value), \"value already present: %%s\", value);")
.addLine(" oldKey == null || %s.equals(oldKey, key),"
+ " \"value already present: %%s\", value);",
Objects.class)
.addLine(" V oldValue = biMap.get(key);")
.addLine(" forcePut.accept(key, value);")
.addLine(" return oldValue;")
Expand Down Expand Up @@ -126,9 +129,12 @@ public void addTo(SourceBuilder code) {
.addLine(" }")
.addLine("")
.addLine(" @Override public V setValue(V value) {")
.addLine(" K oldKey = biMap.inverse().get(value);")
.addLine(" %s.checkArgument(", Preconditions.class)
.addLine(" !biMap.containsValue(value), \"value already present: %%s\", value);")
.addLine(" V oldValue = value;")
.addLine(" oldKey == null || %s.equals(oldKey, key),"
+ " \"value already present: %%s\", value);",
Objects.class)
.addLine(" V oldValue = this.value;")
.addLine(" this.value = %s.requireNonNull(value);", Objects.class)
.addLine(" forcePut.accept(key, value);")
.addLine(" return oldValue;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public Optional<BiMapProperty> create(Config config) {
config, unboxedKeyType.orElse(keyType), unboxedValueType.orElse(valueType));
boolean overridesForcePutMethod = hasForcePutMethodOverride(
config, unboxedKeyType.orElse(keyType), unboxedValueType.orElse(valueType));

if (putMethodOverride.isPresent() && !overridesForcePutMethod) {
config.getEnvironment().getMessager().printMessage(
Kind.ERROR,
Kind.ERROR,
"Overriding "
+ putMethod(property)
+ " will not correctly validate all inputs. Please override "
Expand Down Expand Up @@ -226,9 +226,11 @@ private void addPut(SourceBuilder code) {
putMethod(property),
unboxedKeyType.orElse(keyType),
unboxedValueType.orElse(valueType));
code.addLine(" %s.checkArgument(", Preconditions.class)
.addLine(" !%s.containsValue(value), \"value already present: %%s\", value);",
property.getField())
code.addLine(" %s oldKey = %s.inverse().get(value);", keyType, property.getField())
.addLine(" %s.checkArgument(", Preconditions.class)
.addLine(" oldKey == null || %s.equals(oldKey, key),"
+ " \"value already present: %%s\", value);",
Objects.class)
.addLine(" %s(key, value);", forcePutMethod(property))
.addLine(" return (%s) this;", datatype.getBuilder())
.addLine("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ public void putReplacesDuplicateKey() {
.runTest();
}

@Test
public void putReplacesDuplicateKeyAndValue() {
behaviorTester
.with(bimapPropertyType)
.with(testBuilder()
.addLine("DataType value = new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .mutateItems(items -> items.put(%s, %s))",
keys.example(0), values.example(0))
.addLine(" .build();")
.addLine("assertThat(value.%s).isEqualTo(%s);",
convention.get(), exampleBiMap(0, 0))
.build())
.runTest();
}

@Test
public void putRejectsDuplicateValue() {
thrown.expect(IllegalArgumentException.class);
Expand Down Expand Up @@ -234,6 +250,22 @@ public void inversePutReplacesDuplicateValue() {
.runTest();
}

@Test
public void inversePutReplacesDuplicateKeyAndValue() {
behaviorTester
.with(bimapPropertyType)
.with(testBuilder()
.addLine("DataType value = new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .mutateItems(items -> items.inverse().put(%s, %s))",
values.example(0), keys.example(0))
.addLine(" .build();")
.addLine("assertThat(value.%s).isEqualTo(%s);",
convention.get(), exampleBiMap(0, 0))
.build())
.runTest();
}

@Test
public void inversePutRejectsDuplicateKey() {
thrown.expect(IllegalArgumentException.class);
Expand Down Expand Up @@ -659,6 +691,19 @@ public void callSetValueOnEntryChecksDuplicateValue() {
.runTest();
}

@Test
public void callSetValueOnEntryAllowsDuplicateKeyAndValue() {
behaviorTester
.with(bimapPropertyType)
.with(testBuilder()
.addLine("new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .mutateItems(items -> items.entrySet().iterator().next().setValue(%s));",
values.example(0))
.build())
.runTest();
}

@Test
public void callSetValueOnEntryModifiesUnderlyingProperty() {
behaviorTester
Expand All @@ -674,6 +719,24 @@ public void callSetValueOnEntryModifiesUnderlyingProperty() {
.runTest();
}

@Test
public void callSetValueReturnsOldValue() {
behaviorTester
.with(bimapPropertyType)
.with(testBuilder()
.addLine("new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .mutateItems(items -> {")
.addLine(" Iterator<Map.Entry<%s, %s>> i = items.entrySet().iterator();",
keys.type(), values.type())
.addLine(" Map.Entry<%s, %s> entry = i.next();", keys.type(), values.type())
.addLine(" %s oldValue = entry.setValue(%s);", values.type(), values.example(2))
.addLine(" assertThat(oldValue).isEqualTo(%s);", values.example(0))
.addLine(" });")
.build())
.runTest();
}

@Test
public void callSetValueOnInverseEntryChecksArguments() {
if (checked) {
Expand Down Expand Up @@ -709,6 +772,20 @@ public void callSetValueOnInverseEntryChecksDuplicateValue() {
.runTest();
}

@Test
public void callSetValueOnInverseEntryAllowsDuplicateKeyAndValue() {
behaviorTester
.with(bimapPropertyType)
.with(testBuilder()
.addLine("new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .mutateItems(items -> items")
.addLine(" .inverse().entrySet().iterator().next().setValue(%s));",
keys.example(0))
.build())
.runTest();
}

@Test
public void callSetValueOnInverseEntryModifiesUnderlyingProperty() {
behaviorTester
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testPut_nullValue() {
}

@Test
public void testPut_duplicate() {
public void testPut_duplicateKey() {
behaviorTester
.with(biMapPropertyType)
.with(testBuilder()
Expand All @@ -191,6 +191,33 @@ public void testPut_duplicate() {
.runTest();
}

@Test
public void testPut_duplicateValue() {
thrown.expect(IllegalArgumentException.class);
behaviorTester
.with(biMapPropertyType)
.with(testBuilder()
.addLine("new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .putItems(%s, %s);", keys.example(1), values.example(0))
.build())
.runTest();
}

@Test
public void testPut_duplicateKeyAndValue() {
behaviorTester
.with(biMapPropertyType)
.with(testBuilder()
.addLine("DataType value = new DataType.Builder()")
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .putItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .build();")
.addLine("assertThat(value.%s).isEqualTo(%s);", convention.get(), exampleMap(0, 0))
.build())
.runTest();
}

@Test
public void testForcePut() {
behaviorTester
Expand Down Expand Up @@ -237,9 +264,9 @@ public void testForcePut_duplicate() {
.with(testBuilder()
.addLine("DataType value = new DataType.Builder()")
.addLine(" .forcePutItems(%s, %s)", keys.example(0), values.example(0))
.addLine(" .forcePutItems(%s, %s)", keys.example(0), values.example(1))
.addLine(" .forcePutItems(%s, %s)", keys.example(1), values.example(0))
.addLine(" .build();")
.addLine("assertThat(value.%s).isEqualTo(%s);", convention.get(), exampleMap(0, 1))
.addLine("assertThat(value.%s).isEqualTo(%s);", convention.get(), exampleMap(1, 0))
.build())
.runTest();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public void test_guava() {
" * @throws NullPointerException if {@code value} is null",
" */",
" public Person.Builder putName(int key, String value) {",
" Preconditions.checkArgument(!name.containsValue(value), \"value already present: %s\", value);",
" Integer oldKey = name.inverse().get(value);",
" Preconditions.checkArgument(",
" oldKey == null || Objects.equals(oldKey, key), \"value already present: %s\", value);",
" forcePutName(key, value);",
" return (Person.Builder) this;",
" }",
Expand Down

0 comments on commit 43c5581

Please sign in to comment.