Skip to content

Commit

Permalink
Add options to disable NumberFormat formatting and to set decimal digits
Browse files Browse the repository at this point in the history
-Djoml.format=false produces output equivalent to Float.toString()

-Djoml.format.decimals=<N> controls the number of decimal digits when
using the NumberFormat formatting
  • Loading branch information
httpdigest committed Mar 31, 2017
1 parent 3792f21 commit 9eb908b
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 191 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ sourceCompatibility = 1.2
targetCompatibility = 1.2
sourceSets {
main {
java.srcDir 'src'
java {
srcDir 'src'
exclude 'org/joml/jre/**'
}
resources.srcDirs = ["native/build"]
}
test.java.srcDir 'test'
Expand Down
35 changes: 35 additions & 0 deletions src/org/joml/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ of this software and associated documentation files (the "Software"), to deal
*/
package org.joml;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;

/**
* Utility class for reading system properties.
*
Expand Down Expand Up @@ -57,6 +62,36 @@ class Options {
*/
static final int SIN_LOOKUP_BITS = Integer.parseInt(System.getProperty("joml.sinLookup.bits", "14"));

/**
* Whether to use a {@link NumberFormat} producing scientific notation output when formatting matrix,
* vector and quaternion components to strings.
*/
static final boolean useNumberFormat = hasOption(System.getProperty("joml.format", "true"));

/**
* When {@link #useNumberFormat} is <code>true</code> then this determines the number of decimal digits
* produced in the formatted numbers.
*/
static final int numberFormatDecimals = Integer.parseInt(System.getProperty("joml.format.decimals", "3"));

/**
* The {@link NumberFormat} used to format all numbers throughout all JOML classes.
*/
static final NumberFormat NUMBER_FORMAT = decimalFormat();

static NumberFormat decimalFormat() {
NumberFormat df;
if (useNumberFormat) {
char[] prec = new char[numberFormatDecimals];
Arrays.fill(prec, '0');
df = new DecimalFormat(" 0." + new String(prec) + "E0;-");
} else {
df = NumberFormat.getNumberInstance(Locale.ENGLISH);
df.setGroupingUsed(false);
}
return df;
}

static boolean hasOption(String v) {
if (v == null)
return false;
Expand Down
15 changes: 1 addition & 14 deletions src/org/joml/Quaterniond.java
Original file line number Diff line number Diff line change
Expand Up @@ -1868,20 +1868,7 @@ public Quaterniond lookAlong(double dirX, double dirY, double dirZ, double upX,
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat("0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
15 changes: 1 addition & 14 deletions src/org/joml/Quaternionf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2914,20 +2914,7 @@ public Quaternionf rotateAxis(float angle, float axisX, float axisY, float axisZ
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat("0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/org/joml/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ private static long doubleToLongBits1_2(double dbl) {
return Double.doubleToLongBits(dbl);
}

static String formatNumbers(String str) {
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
}

}
19 changes: 1 addition & 18 deletions src/org/joml/Vector2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,24 +864,7 @@ public boolean equals(Object obj) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector2f.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,24 +824,7 @@ public boolean equals(Object obj) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector2i.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,24 +748,7 @@ public boolean equals(Object obj) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -1999,24 +1999,7 @@ public Vector3d zero() {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector3f.java
Original file line number Diff line number Diff line change
Expand Up @@ -1532,24 +1532,7 @@ public Vector3f zero() {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector3i.java
Original file line number Diff line number Diff line change
Expand Up @@ -769,24 +769,7 @@ public Vector3i zero() {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector4d.java
Original file line number Diff line number Diff line change
Expand Up @@ -1352,24 +1352,7 @@ public Vector4d negate(Vector4d dest) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector4f.java
Original file line number Diff line number Diff line change
Expand Up @@ -1272,24 +1272,7 @@ public Vector4f negate(Vector4f dest) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down
19 changes: 1 addition & 18 deletions src/org/joml/Vector4i.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,24 +907,7 @@ public Vector4i negate(Vector4i dest) {
* @return the string representation
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat(" 0.000E0;-");
String str = toString(formatter);
StringBuffer res = new StringBuffer();
int eIndex = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'E') {
eIndex = i;
} else if (c == ' ' && eIndex == i - 1) {
// workaround Java 1.4 DecimalFormat bug
res.append('+');
continue;
} else if (Character.isDigit(c) && eIndex == i - 1) {
res.append('+');
}
res.append(c);
}
return res.toString();
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}

/**
Expand Down

0 comments on commit 9eb908b

Please sign in to comment.