-
Notifications
You must be signed in to change notification settings - Fork 14
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
Introduce Count unit #8
Open
kokosing
wants to merge
2
commits into
airlift:master
Choose a base branch
from
kokosing:origin/master/001_count
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,203 @@ | ||||||
/* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
package io.airlift.units; | ||||||
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator; | ||||||
import com.fasterxml.jackson.annotation.JsonValue; | ||||||
|
||||||
import java.util.regex.Matcher; | ||||||
import java.util.regex.Pattern; | ||||||
|
||||||
import static io.airlift.units.Preconditions.checkArgument; | ||||||
import static java.lang.Long.parseLong; | ||||||
import static java.lang.Math.multiplyExact; | ||||||
import static java.lang.String.format; | ||||||
import static java.util.Objects.requireNonNull; | ||||||
|
||||||
public final class Count | ||||||
implements Comparable<Count> | ||||||
{ | ||||||
private static final Pattern PATTERN = Pattern.compile("^\\s*(\\d+)\\s*([a-zA-Z]?)\\s*$"); | ||||||
|
||||||
// We iterate over the MAGNITUDES constant in convertToMostSuccinctRounded() | ||||||
// instead of Magnitude.values() as the latter results in non-trivial amount of memory | ||||||
// allocation when that method is called in a tight loop. The reason is that the values() | ||||||
// call allocates a new array at each call. | ||||||
private static final Magnitude[] MAGNITUDES = Magnitude.values(); | ||||||
|
||||||
/** | ||||||
* @return count with no bigger value than 1000 in succinct magnitude, fractional part is rounded | ||||||
*/ | ||||||
public static Count succinctRounded(long count) | ||||||
{ | ||||||
return succinctRounded(count, Magnitude.SINGLE); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return count with no bigger value than 1000 in succinct magnitude, fractional part is rounded | ||||||
*/ | ||||||
public static Count succinctRounded(long count, Magnitude magnitude) | ||||||
{ | ||||||
return new Count(count, magnitude).convertToMostSuccinctRounded(); | ||||||
} | ||||||
|
||||||
private final long value; | ||||||
private final Magnitude magnitude; | ||||||
|
||||||
public Count(long count, Magnitude magnitude) | ||||||
{ | ||||||
checkArgument(count >= 0, "count is negative"); | ||||||
requireNonNull(magnitude, "magnitude is null"); | ||||||
|
||||||
this.value = count; | ||||||
this.magnitude = magnitude; | ||||||
} | ||||||
|
||||||
public long getValue() | ||||||
{ | ||||||
return value; | ||||||
} | ||||||
|
||||||
public Magnitude getMagnitude() | ||||||
{ | ||||||
return magnitude; | ||||||
} | ||||||
|
||||||
public long getValue(Magnitude magnitude) | ||||||
{ | ||||||
requireNonNull(magnitude, "magnitude is null"); | ||||||
|
||||||
if (value == 0L) { | ||||||
return 0L; | ||||||
} | ||||||
|
||||||
long scale = this.magnitude.getFactor() / magnitude.getFactor(); | ||||||
if (scale * magnitude.getFactor() != this.magnitude.getFactor()) { | ||||||
throw new IllegalArgumentException(format("Unable to represent %s in %s, conversion would cause a precision loss", this, magnitude)); | ||||||
} | ||||||
try { | ||||||
return multiplyExact(value, scale); | ||||||
} | ||||||
catch (ArithmeticException e) { | ||||||
throw new IllegalArgumentException(format("Unable to represent %s in %s due the Long value overflow", this, magnitude)); | ||||||
} | ||||||
} | ||||||
|
||||||
public Count convertTo(Magnitude magnitude) | ||||||
{ | ||||||
return new Count(getValue(magnitude), magnitude); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return converted count with no bigger value than 1000 in succinct magnitude, fractional part is rounded | ||||||
*/ | ||||||
public Count convertToMostSuccinctRounded() | ||||||
{ | ||||||
for (Magnitude magnitude : MAGNITUDES) { | ||||||
double converted = (double) value * this.magnitude.getFactor() / magnitude.getFactor(); | ||||||
if (converted < 1000) { | ||||||
return new Count(Math.round(converted), magnitude); | ||||||
} | ||||||
} | ||||||
throw new IllegalStateException(); | ||||||
} | ||||||
|
||||||
@JsonValue | ||||||
@Override | ||||||
public String toString() | ||||||
{ | ||||||
return value + magnitude.getMagnitudeString(); | ||||||
} | ||||||
|
||||||
@JsonCreator | ||||||
public static Count valueOf(String count) | ||||||
throws IllegalArgumentException | ||||||
{ | ||||||
requireNonNull(count, "count is null"); | ||||||
checkArgument(!count.isEmpty(), "count is empty"); | ||||||
|
||||||
Matcher matcher = PATTERN.matcher(count); | ||||||
if (!matcher.matches()) { | ||||||
throw new IllegalArgumentException("Not a valid count string: " + count); | ||||||
} | ||||||
|
||||||
long value = parseLong(matcher.group(1)); | ||||||
String magnitutdeString = matcher.group(2); | ||||||
|
||||||
for (Magnitude magnitude : Magnitude.values()) { | ||||||
if (magnitude.getMagnitudeString().equals(magnitutdeString)) { | ||||||
return new Count(value, magnitude); | ||||||
} | ||||||
} | ||||||
|
||||||
throw new IllegalArgumentException("Unknown magnitude: " + magnitutdeString); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public int compareTo(Count o) | ||||||
{ | ||||||
return Long.compare(getValue(Magnitude.SINGLE), o.getValue(Magnitude.SINGLE)); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean equals(Object o) | ||||||
{ | ||||||
if (this == o) { | ||||||
return true; | ||||||
} | ||||||
if (o == null || getClass() != o.getClass()) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
Count count = (Count) o; | ||||||
|
||||||
return compareTo(count) == 0; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public int hashCode() | ||||||
{ | ||||||
return Long.hashCode(getValue(Magnitude.SINGLE)); | ||||||
} | ||||||
|
||||||
public enum Magnitude | ||||||
{ | ||||||
// must be in increasing magnitude order | ||||||
SINGLE(1L, ""), | ||||||
THOUSAND(1000L, "K"), | ||||||
MILLION(1000_000L, "M"), | ||||||
BILLION(1000_000_000L, "B"), | ||||||
TRILION(1000_000_000_000L, "T"), | ||||||
QUADRILLION(1000_000_000_000_000L, "P"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not consistent with using
Suggested change
|
||||||
|
||||||
private final long factor; | ||||||
private final String magnitudeString; | ||||||
|
||||||
Magnitude(long factor, String magnitudeString) | ||||||
{ | ||||||
this.factor = factor; | ||||||
this.magnitudeString = requireNonNull(magnitudeString, "magnitudeString is null"); | ||||||
} | ||||||
|
||||||
long getFactor() | ||||||
{ | ||||||
return factor; | ||||||
} | ||||||
|
||||||
public String getMagnitudeString() | ||||||
{ | ||||||
return magnitudeString; | ||||||
} | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.airlift.units; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({METHOD, ANNOTATION_TYPE}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = MaxCountValidator.class) | ||
public @interface MaxCount | ||
{ | ||
String value(); | ||
|
||
String message() default "{io.airlift.units.MaxCount.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.airlift.units; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class MaxCountValidator | ||
implements ConstraintValidator<MaxCount, Count> | ||
{ | ||
private Count max; | ||
|
||
@Override | ||
public void initialize(MaxCount count) | ||
{ | ||
this.max = Count.valueOf(count.value()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Count count, ConstraintValidatorContext context) | ||
{ | ||
return (count == null) || (count.compareTo(max) <= 0); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "max:" + max; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.airlift.units; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({METHOD, ANNOTATION_TYPE}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = MinCountValidator.class) | ||
public @interface MinCount | ||
{ | ||
String value(); | ||
|
||
String message() default "{io.airlift.units.MinCount.message}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.airlift.units; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class MinCountValidator | ||
implements ConstraintValidator<MinCount, Count> | ||
{ | ||
private Count min; | ||
|
||
@Override | ||
public void initialize(MinCount dataSize) | ||
{ | ||
this.min = Count.valueOf(dataSize.value()); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Count count, ConstraintValidatorContext context) | ||
{ | ||
return (count == null) || (count.compareTo(min) >= 0); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "min:" + min; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How is there a fractional part when
value
is along
?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.
When you pass 123456 in SINGLE then you will get 123.456K that will be rounded to 123K.