Skip to content

Commit

Permalink
fix thread problems with DecimalFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
volker committed Jul 14, 2023
1 parent 5f9aacb commit e46d8fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
25 changes: 9 additions & 16 deletions src/main/java/com/inet/sass/parser/LexicalUnitImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.inet.sass.ScssContext;
import com.inet.sass.function.SCSSFunctionGenerator;
Expand All @@ -47,8 +48,12 @@ public class LexicalUnitImpl implements SCSSLexicalUnit, SassListItem {

public static final long PRECISION = 100000L;

private static final DecimalFormat CSS_FLOAT_FORMAT = new DecimalFormat(
"0.0####");
private static final ThreadLocal<DecimalFormat> CSS_FLOAT_FORMAT = new ThreadLocal<>() {
@Override
protected DecimalFormat initialValue() {
return new DecimalFormat("0.#####", DecimalFormatSymbols.getInstance(Locale.US) );
}
};

public static final LexicalUnitImpl WHITESPACE = new LexicalUnitImpl( null, 0, 0, SAC_IDENT, " " );

Expand Down Expand Up @@ -176,13 +181,7 @@ public double getDoubleValue() {
* @return a string representing the value, either with or without decimals
*/
public String getDoubleOrInteger() {
double f = getDoubleValue();
long i = (long) f;
if (i == f) {
return Long.toString(i);
} else {
return CSS_FLOAT_FORMAT.format(f);
}
return CSS_FLOAT_FORMAT.get().format( f );
}

private void setDoubleValue( double f ) {
Expand Down Expand Up @@ -908,7 +907,7 @@ public String buildString(BuildStringStrategy strategy) {
text = ColorUtil.rgbToColorString( rgb );
break;
} else if( params.size() == 2 || ColorUtil.isHsla( this ) ) {
String alphaText = alpha == 0.0f ? "0" : CSS_FLOAT_FORMAT.format( alpha );
String alphaText = alpha == 0.0f ? "0" : CSS_FLOAT_FORMAT.get().format( alpha );
text = "rgba(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ", " + alphaText + ")";
break;
}
Expand Down Expand Up @@ -962,12 +961,6 @@ private int[] getRgb() {
return new int[] { red, green, blue };
}

static {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
CSS_FLOAT_FORMAT.setDecimalFormatSymbols(symbols);
}

@Override
public boolean containsArithmeticalOperator() {
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/vaadin-themes/css/valo/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -10416,8 +10416,8 @@ div.v-layout.v-horizontal.v-widget {
}

.valo .v-splitpanel-horizontal.large > div > .v-splitpanel-hsplitter:after {
left: 0px;
right: 0px;
left: -0px;
right: -0px;
}

.valo .v-splitpanel-horizontal.large > div > .v-splitpanel-hsplitter div:before {
Expand Down Expand Up @@ -10490,8 +10490,8 @@ div.v-layout.v-horizontal.v-widget {
}

.valo .v-splitpanel-vertical.large > div > .v-splitpanel-vsplitter:after {
top: 0px;
bottom: 0px;
top: -0px;
bottom: -0px;
}

.valo .v-splitpanel-vertical.large > div > .v-splitpanel-vsplitter div:before {
Expand Down

0 comments on commit e46d8fc

Please sign in to comment.