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

Added FormattedDouble Class #2668

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions gson/src/main/java/com/google/gson/internal/FormattedDouble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
public class FormattedDouble extends Number {
private static final long serialVersionUID = 1L;

// ThreadLocal because DecimalFormat is not thread-safe
private static final ThreadLocal<DecimalFormat> format =
// Specify DecimalFormatSymbols to make code independent from default Locale
ThreadLocal.withInitial(() -> new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH)));

private final Double delegate;

public FormattedDouble(Double value) {
Objects.requireNonNull(value, "Value should not be null");
this.delegate = value;
}

@Override
public byte byteValue() {
return delegate.byteValue();
}

@Override
public short shortValue() {
return delegate.shortValue();
}

@Override
public int intValue() {
return delegate.intValue();
}

@Override
public long longValue() {
return delegate.longValue();
}

@Override
public float floatValue() {
return delegate.floatValue();
}

@Override
public double doubleValue() {
return delegate.doubleValue();
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj instanceof FormattedDouble) {
return ((FormattedDouble) obj).delegate.equals(delegate);
} else {
return false;
}
}

@Override
public int hashCode() {
return 31 * delegate.hashCode() + format.get().hashCode();
}

@Override
public String toString() {
return format.get().format((double) delegate);
}
}
Loading