I'm writing a library that needs to have some code if a particular library is included. Since this code is scattered all around the project, it would be nice if users didn't have to comment/uncomment everything themselves.
In C, this would be easy enough with a #define
in a header, and then code blocks surrounded with #ifdefs
. Of course, Java doesn't have the C preprocessor...
To clarify - several external libraries will be distributed with mine. I do not want to have to include them all to minimize my executable size. If a developer does include a library, I need to be able to use it, and if not, then it can just be ignored.
What is the best way to do this in Java?
When equivalent is used as a noun, the most common preposition is of: 150 grams are the equivalent of a medium-sized potato. James thought retiring is the equivalent of a death sentence. Seven human years are the equivalent of one dog year.
Definition of equivalent 1 : equal in force, amount, or value also : equal in area or volume but not superposable a square equivalent to a triangle. 2a : like in signification or import. b : having logical equivalence equivalent statements. 3 : corresponding or virtually identical especially in effect or function.
In math, equivalent is different from equal. Equal means same in all aspects, whereas equivalent means similar but not identical. For example, 2 is said to be equal to 2 but equivalent to 1 + 1.
EQUIVALENT (noun) definition and synonyms | Macmillan Dictionary.
There's no way to do what you want from within Java. You could preprocess the Java source files, but that's outside the scope of Java.
Can you not abstract the differences and then vary the implementation?
Based on your clarification, it sounds like you might be able to create a factory method that will return either an object from one of the external libraries or a "stub" class whose functions will do what you would have done in the "not-available" conditional code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With