0

In stock trading, quantities are usually integers (e.g. 5x shares, 10x options, etc). With cryptocurrencies, quantities are fractions (e.g. 0.01 bitcoin). In both scenarios, there is typically a minimum unit (e.g. multiples of 100x shares).

I would like to wrap this logic in a Quantity class. However:

  • Java native types (e.g. double) are final, so I cannot extend them
  • Java does not support operator overloading, so arithmetic will be ugly
  • Java does not support typedefs, so I cannot wrap a double with a Quantity type

So I guess my question is this, if I want to create something akin to a native type (lots of instances, pass-by-value, lots of arithmetic), is there a "classic" Java solution? From memory, C# has a struct type which is pass-by-value, is there something similar in Java?

Thank you,

EDIT: Is it possible to import a native type from C++ code, and effectively bypass Java?

mils
  • 1,878
  • 2
  • 21
  • 42
  • No. But you could use scala, and write your own dsl and interact with Java just fine. – Elliott Frisch Feb 22 '18 at 22:32
  • Thanks Elliott, would that mean rewriting the existing codebase in scala, or some kind of interop? – mils Feb 22 '18 at 22:35
  • Yes. And as for your edit, sure. You can use JNA. Prepare to write way more code. And you still won't have operator overloading or typedefs. – Elliott Frisch Feb 22 '18 at 22:41
  • 1
    You shouldn't be using `double` for currency anyway, you should be using `BigDecimal`, which already has the issues you're talking about, and that's a built-in type. Learn from [`BigDecimal`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html), and implement your `Quantity` class in a similar way. – Andreas Feb 22 '18 at 22:43
  • `BigDecimal` is a step in the right direction, but it is not enough for my purposes. I need more elaborate logic. Since I need to extend it, I'm looking for a solution that will also handle the aforementioned points. – mils Feb 22 '18 at 22:45

1 Answers1

3

Yes, it is possible to declare typedefs in Java, with a guarantee of type-correctness, but without the syntactic complexity and run-time overhead of declaring a new Java class.

As a user, you can do this by writing a type annotation and using an annotation processor at compile time. For example, you would write @Quantity int for quantities, and you would compile with javac -processor QuantityProcessor MyFile.java. You can use any arithmetic operation on a @Quantity int, and the compiler issues a warning if you mix regular ints with @Quantity ints, or if there are other errors such as a value not being in the right range or not being a multiple of 100.

Someone needs to define the type annotations and the annotation processor, and because your requirements are unique, you will probably need to create your own. One framework that makes it easy to do create type annotations and annotation processors is the Checker Framework. (Disclosure: I am a developer.)

The following parts of the Checker Framework manual may be particularly helpful.

mernst
  • 7,437
  • 30
  • 45