Integer representation specification - #182
Conversation
|
I really don't think it is useful to provide all this customizability. Putting effort in creating code that is optimized for people but solely read by compilers is imho not a big priority... |
|
It depends. If you work on custom int format, you want to have eg the mask for the different part avail. Also if you want to parse data to build other code from it, it may be better to parse the data faithfully. |
|
Anyhow, it's good it's not released, because I think it needs more thinking. I will try to have common interface-ish for all number AtomX classes. Even though for example you have only dec and hex representation for fp. I think I will go for a more complex internal field to represent the formatting, shared for long/int, and another shared for float/double . So IntegerLiteralFormat / FloatingPointLiteralFormat . |
|
But try to keep the default case (no formatting, decimal output) as efficient as possible - thx |
|
ok so here is what I want :
So for me it's best to have a record holding the representation params, including the enum base (hex, dec, bin, oct) , but have the already present methods visible on the JAtom to change the record to a new one. |
|
Much better this way. We can update the representation internally without changing the signature. |
|
fyi: I am on vacation next week - so no expectations towards me pls - thx :-) |
I did not expect you would review all the PRs so fast. gladly surprised. Also it's nice you took the formatting. Will help me focus on the important things. But yes, I don't expect anything, except that you take care of yourself during your vacations :) |
|
I made a pass on the tests, ready to review. |
| return (T) this; | ||
| } | ||
|
|
||
| public IntegerRepresentation representation () |
There was a problem hiding this comment.
I would prefer the annotations on the method return types and method parameters. If not, then not
There was a problem hiding this comment.
you mean add @NonNull on the return ? Sure. Can you do it, or I do it ?
There was a problem hiding this comment.
If it is too cumbersome for you, let me know
| public T representation (IntegerRepresentation representation) | ||
| { | ||
| if (representation != null) | ||
| this.representation = representation; |
There was a problem hiding this comment.
no, representation is @NonNull
There was a problem hiding this comment.
Then I'd prefer a "if (null) throw" with the parameter - to make sure the contract of the method is clear
| // copier | ||
|
|
||
| /// Intermediate mutable class for mutate a record. Waiting for java withers … | ||
| public static class Copier |
There was a problem hiding this comment.
Would we spare that, when we change the record to a regular class?
| } | ||
|
|
||
| /// @return sb | ||
| public StringBuilder represent (int i, |
There was a problem hiding this comment.
To me, this method (and the long variant) would better suit in IntegerRepresentation - otherwise we have some weird circle dependency between them
There was a problem hiding this comment.
@glelouet any comment on this one? Objections again moving it?
| reversed.append (source.charAt (i)); | ||
| break; | ||
| } | ||
| else |
| reversed.append (SEP_CHAR); | ||
| else | ||
| break; | ||
| else |
There was a problem hiding this comment.
But the block above the else in brackets - this could be very misleading otherwise ;-)
| static void checkFormat (String expected, String source, String format, boolean allowLeadingSep) | ||
| { | ||
| StringBuilder sb = new StringBuilder (); | ||
| EIntegerBase.addSep (source, format, allowLeadingSep, 0, 0, sb); |
There was a problem hiding this comment.
You could extend the tests to cover the two "0" parameters as well
Objective, constraints and explanations
A previous PR already embarked the "base" feature, but this one is cleaner/more detailed, especially regarding sharing settings.
This PR aims at providing ways to represent integer/long format in the generated code.
When we want to write an int, for example "404", with
JExpr.lit(404), we may also want to specify how this value will be written in the resulting code, egIn short, "+4_0_4" is just as valid as "0000624".
This set of choices is a "representation" of integers. It's not in the formatting settings, because formatting is project-wide, while those can change on a class or even field basis.
The main use case of those representations are defining them, and using them in int/long fields (a long is just an int with tailing l/L), therefore the building of one received special attention, with default representations available, both to use directly or to be based on.
As constraints, the default representation of an int should produce the same, when possible, as before ; and should impend no performance hit.
Since creating user-specific representation should not modify the default values, they are all records to be read only. Updating a representation's value actually create a new representation with that value updated. The JAtomInt and JAtomLong can be set their representation, and provide a few methods to update specific fields though it replaces the internal representation with an updated copy.
Those representation start with a base, for example octal, then add options, in the order : force positive sign, padding, use lower caps letters, use separators.
Features
Format class
A new
IntegerRepresentationrecord contains information to represent int and long in the code.Creation
A few default representation are available ; you are recommended to create your own that you use in your own library, since the default ones are not designed to be core part of the lib (they are arbitrary choices so not as stable as choice presentation).
Usage
JAtomInteger and JAtomLong have that new field. They extend a common class that allows to manipulate those fields internally, replacing the representation each time.
Formatting process
positiveSignis trueprefixUpperindicates if use upper or lower case prefixsuffixUpperindicates if use 'L' or 'l'separateEveryandseparateSizefields.Padding example
(those are the tests)
42 padded 5 is
Separator Format example
1000 decimal with format :