-
Notifications
You must be signed in to change notification settings - Fork 101
Add BiMap to the special support for collections and maps. #419
Conversation
Hi @alicederyn here's a rough draft of adding BiMaps. I do have some questions inlined for you. Mostly I went the lazy approach of using the existing implementation for Map and moving it over to BiMap. |
src/test/java/org/inferred/freebuilder/processor/property/BiMapPropertyTest.java
Show resolved
Hide resolved
addPut(code); | ||
addPutAll(code); | ||
addRemoveKey(code); | ||
addRemoveValue(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may have been misguided but since BiMap's are 1-1, you may want to make modifications based on the value and not the key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable. You could do .mutateX(m -> m.inverse().remove(value))
so it really comes down to convenience.
src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedBiMap.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answers and some points I noted in a quick skim. Please make sure you have the full API tested — I noted some examples but I wasn't exhaustive.
src/main/java/org/inferred/freebuilder/processor/property/BiMapProperty.java
Outdated
Show resolved
Hide resolved
src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedBiMap.java
Outdated
Show resolved
Hide resolved
src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedBiMap.java
Show resolved
Hide resolved
addPut(code); | ||
addPutAll(code); | ||
addRemoveKey(code); | ||
addRemoveValue(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable. You could do .mutateX(m -> m.inverse().remove(value))
so it really comes down to convenience.
.addLine( | ||
"public %s %s(%s key) {", | ||
datatype.getBuilder(), | ||
"removeKey" + property.getCapitalizedName(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"removeKeyFrom" + ...
seems more readable — also, should live in BuilderMethods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "from" reads much better, but doesn't the method currently live in BuilderMethods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to add a new static method, removeKeyFromMethod
, to BuilderMethods.java
, so that you're not writing out the formula "removeKeyFrom" + x
everywhere.
src/main/java/org/inferred/freebuilder/processor/property/BiMapProperty.java
Outdated
Show resolved
Hide resolved
src/test/java/org/inferred/freebuilder/processor/property/BiMapPropertyTest.java
Show resolved
Hide resolved
If I understand it correctly, that means defining a forcePut method on the builder. As the passed around put is simply the implementation defined (overridden or not) for the builder.
to the put in the checked bimap. Is that correct? |
Yes, that seems correct: generate a Note that this also means that the generated |
I've updated to testOverridingAdd to forcePut. I'm a bit confused why Any more feedback for either entryRemainsUsableAfterCallingSetValue or the overall review? Thanks in advance. |
If you dig into the logic of |
Hi @alicederyn . I've updated to copy key and value from the underlying entry. As well as updating equals/hashcode to use those values. |
@alicederyn sorry for the delay. I finally fixed the checkstyle issues. I wasn't sure how to suppress my comment line with a url, and checkstyle didn't like nested static method calls. Thank you. |
Hi @alicederyn I was wondering when you would have a chance to go over my latest changes. Thank you! |
src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedBiMap.java
Show resolved
Hide resolved
Sorry for the delay |
@Test | ||
public void callSetValueOnEntryChecksDuplicateValue() { | ||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("value already present: "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure for your framework the cleanest way to change a literal "beta" -> beta. I could do values.example(1).replace('\"', '')
but I was thinking there was a better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd have to add that method to ElementFactory. exampleToString
?
@alicederyn no worries. |
src/main/java/org/inferred/freebuilder/processor/excerpt/CheckedBiMap.java
Show resolved
Hide resolved
.addLine(" }") | ||
.addLine("") | ||
.addLine(" @Override public V setValue(V value) {") | ||
.addLine(" %s.checkArgument(!hasValue.test(value), \"value already present: \" + value);", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use %s
like in the put method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@Test | ||
public void callSetValueOnEntryChecksDuplicateValue() { | ||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("value already present: "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd have to add that method to ElementFactory. exampleToString
?
Done. |
Build failed on checkstyle though: https://travis-ci.org/github/inferred/FreeBuilder/builds/669328480 |
Follows the behavoir of Maps under https://github.com/inferred/FreeBuilder#collections-and-maps
Thanks @alicederyn, I've fixed the offending lines and cleaned up my commits. Things are passing now. |
Follows the behavoir of Maps under https://github.com/inferred/FreeBuilder#collections-and-maps.
Addresses #418