-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplified and improved kotlin forwarding service #3400
Simplified and improved kotlin forwarding service #3400
Conversation
ItoroD
commented
May 18, 2024
•
edited
edited
- One main difference to note in the kotlin version of forwarding service is that in kotlin its not the best best practice to use multiple constructors (secondary constructors). Hence default values have been used for some member variables of config class.
- With this we can initialize config as if it had multiple constructors
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.
Overall, this looks great!
I am being exceptionally picky about the ForwardingService
because for many it will be their first impression of bitcoinj (and maybe Kotlin or modern Java) and also because if we try really hard and can't eliminate any unwieldy code then we need to look at improving the bitcoinj API itself to fix them.
I really appreciate your work and your willingness to keep fine-tuning this!
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
0b91ab2
to
6853d4f
Compare
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.
Getting close! This is looking good!
@@ -24,63 +24,58 @@ import org.bitcoinj.wallet.Wallet | |||
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener | |||
import java.io.Closeable | |||
import java.io.File | |||
import java.util.Objects.* | |||
import java.util.* |
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.
We generally try to avoid using wildcard imports...
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.
will look out those
constructor(forwardingAddress: Address) : this( | ||
forwardingAddress, | ||
forwardingAddress.network() as BitcoinNetwork | ||
) |
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.
Is this constructor necessary?
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.
Yes, I noticed you do the address check in a second constructor that has (address, network) as parameters. So this constructor for when only address is passed, calls the other constructor that checks address before address is set on member variables.
since forwardingAddress.network() as BitcoinNetwork is now being done in the constructor, i should also take it out of the default set value for network
…ied coin listener with send function
6853d4f
to
fcb51db
Compare
Thanks! @msgilligan |
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.
Approved.
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 wonder can we do anything about these warnings as well?
w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:81 Parameter 'prevBalance' is never used
w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:100 Parameter 'newBalance' is never used
Obviously we can't change the method signature, but is it possible to declare the parameters as "intentionally not being used"?
examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt
Outdated
Show resolved
Hide resolved
One nitpick about the commit message: we use present tense to describe the semantics of the commit, rather than past tense. So "simplify coin listener" rather than "simplified…". |
Noted. Thanks |
We can use the "Suppress" annotation like so
|
Can't they be replaced with Note that this convention is now in Java 22 and later: https://openjdk.org/jeps/456 |
Yes the underscore exists in kotlin but it cannot be used on parameter of a function. Its used in special cases like parameter of a lamda function. Also in java 22 I doubt you can use the underscore in a method param. Its used in other cases in java 22 as well. (not in method params) to the best of my knowledge |
@msgilligan Are you fine with the added |
I'm fine with it for now. I spent some time looking at alternatives and while doing so discovered that the neither the Kotlin nor the Java example correctly removes the listener. The problem is that when creating the method references now objects are created. So the listener passed in to remove does not match the one stored in the list... |
Merged. |