Skip to content
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

Add Quantity type #129

Merged
merged 7 commits into from
Nov 29, 2017
Merged

Conversation

lewisheadden
Copy link
Contributor

Adds an implementation of Kubernetes Quantity handling in the Java client.

This allows users to interact with Quantitys as their raw value without having to parse units (e.g. 1024 instead of 1Ki, 1000 instead of 1e3, 0.001 instead of 1m). This is helpful for example when you submit a value like 1024Mi and the Kubernetes API will return 1Gi. You cannot reliably manipulate the string without parsing it.

This Quantity implementation gives you an object containing its BigDecimal value and the format of the suffix (e.g. DECIMAL_SI, BINARY_SI, or DECIMAL_EXPONENT) when parsed or the desired format to be written. Using this normalized form you can then manipulate the BigDecimal representation and use the Quantity implementation to re-serialize it in the appropriate notation.

A PR will also be needed to gen to make the Swagger pre-processor not turn Quantity into a String but map it to this class.

Quantities are complex, to say the least, in Kubernetes but thanks to some guidance from @lavalamp and a lot of time in the Go Debugger, I think this behaves pretty accurately.

@brendandburns
Copy link
Contributor

Thanks for the PR! some small nits, but looks good to me.

@@ -0,0 +1,56 @@
package io.kubernetes.client.custom;

public class BaseExponent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaDoc?

Copy link
Contributor Author

@lewisheadden lewisheadden Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to add JavaDoc - to clarify, this is an across the board ask rather than just for BaseExponent?

private final int base;

private final int exponent;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line spacing is weird here. preference for:

private final int base;
private final int exponent;
...

public BaseExponent(...) {
...


BaseExponent that = (BaseExponent) o;

if (base != that.base) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Preference for:

return base == that.base && exponent == that.exponent && format == that.format;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ironically, in its own auto-generated code, IntelliJ suggests this quick fix.


@Override
public int hashCode() {
int result = base;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is funky, and I'm not entirely sure that it is correct. (or at least I don't understand why it is correct...

Why not:

this.toString().hashCode();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must confess that this was entirely auto-generated by IntelliJ, however, I don't see any error in it.

Given our use case, I think we can happily use this.toString().hashCode() as the cost is not prohibitive to us and it will be more future proof in the case of adding or removing fields.

@@ -0,0 +1,20 @@
package io.kubernetes.client.custom;

public class Pair<L, R> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already depend on apache-commons, rev the version to 3.0 and use the Pair implementation from there?

this.format = format;
}

public BigDecimal getBigDecimal() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: getNumber ?

@brendandburns
Copy link
Contributor

LGTM, JavaDoc can wait (but yes, for all classes would be great, but I realized we haven't really done a good job of that in general, so don't want to hold this PR on that...)

@brendandburns brendandburns merged commit 1e3fb8a into kubernetes-client:master Nov 29, 2017
@gzchen008
Copy link

没有说明,根本不知道单位,建议加上java doc或者demo

@@ -1,109 +1,109 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pom.xml is reformatted. hard to locate changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants