Skip to content

Commit 7eaeb95

Browse files
author
volker
committed
reduce use of instanceof through using getItemType()
1 parent 944cb46 commit 7eaeb95

File tree

5 files changed

+102
-61
lines changed

5 files changed

+102
-61
lines changed

src/main/java/com/inet/sass/expression/BinaryOperator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ public LexicalUnitImpl evalInternal(LexicalUnitImpl leftValue,
173173
}
174174

175175
public static boolean isTrue(SassListItem item) {
176-
if (LexicalUnitImpl.checkLexicalUnitType(item,
177-
LexicalUnitImpl.SCSS_NULL)) {
176+
if( item.getItemType() == LexicalUnitImpl.SCSS_NULL ) {
178177
return false;
179178
}
180179
return !"false".equals(item.printState());

src/main/java/com/inet/sass/function/InspectFunctionGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ class InspectFunctionGenerator extends AbstractFunctionGenerator {
3030
protected SassListItem computeForArgumentList( ScssContext context, LexicalUnitImpl function, FormalArgumentList arglist ) {
3131
SassListItem param = getParam( arglist, 0 );
3232
String result;
33-
if (LexicalUnitImpl.checkLexicalUnitType(param, LexicalUnitImpl.SCSS_NULL)) {
33+
if( param.getItemType() == LexicalUnitImpl.SCSS_NULL ) {
3434
result = "null";
35-
// } else if( param instanceof SassList ) {
3635
} else {
3736
result = param.printState();
3837
}

src/main/java/com/inet/sass/function/TypeOfFunctionGenerator.java

Lines changed: 90 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,80 +16,119 @@
1616
*/
1717
package com.inet.sass.function;
1818

19+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_CENTIMETER;
20+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_DEGREE;
21+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_DIMENSION;
22+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_EM;
23+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_EX;
24+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_FUNCTION;
25+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_GRADIAN;
26+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_HERTZ;
27+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_IDENT;
28+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_INCH;
29+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_INTEGER;
30+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_KILOHERTZ;
31+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_LEM;
32+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_MILLIMETER;
33+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_MILLISECOND;
34+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PERCENTAGE;
35+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PICA;
36+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_PIXEL;
37+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_POINT;
38+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_RADIAN;
39+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_REAL;
40+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_REM;
41+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_RGBCOLOR;
42+
import static com.inet.sass.parser.SCSSLexicalUnit.SAC_SECOND;
43+
import static com.inet.sass.parser.SCSSLexicalUnit.SCSS_LIST;
44+
import static com.inet.sass.parser.SCSSLexicalUnit.SCSS_NULL;
45+
1946
import com.inet.sass.ScssContext;
2047
import com.inet.sass.parser.FormalArgumentList;
2148
import com.inet.sass.parser.LexicalUnitImpl;
2249
import com.inet.sass.parser.SassList;
23-
import com.inet.sass.parser.SassListItem;
2450
import com.inet.sass.parser.SassList.Separator;
51+
import com.inet.sass.parser.SassListItem;
2552
import com.inet.sass.util.ColorUtil;
2653

2754
class TypeOfFunctionGenerator extends AbstractFunctionGenerator {
2855

2956
private static String[] argumentNames = { "value" };
3057

3158
TypeOfFunctionGenerator() {
32-
super(createArgumentList(argumentNames, false), "type-of");
59+
super( createArgumentList( argumentNames, false ), "type-of" );
3360
}
3461

3562
@Override
36-
protected SassListItem computeForArgumentList(ScssContext context,
37-
LexicalUnitImpl function, FormalArgumentList actualArguments) {
38-
SassListItem param = getParam(actualArguments, 0);
63+
protected SassListItem computeForArgumentList( ScssContext context, LexicalUnitImpl function, FormalArgumentList actualArguments ) {
64+
SassListItem param = getParam( actualArguments, 0 );
3965
String type = "string";
4066

41-
if (param instanceof SassList) {
42-
type = "list";
43-
SassList list = (SassList)param;
44-
if( list.size() > 0 ) {
45-
// we need only to check the first entry, the other has the parser already checked
46-
SassListItem first = list.get( 0 );
47-
if( first.getClass() == SassList.class && ((SassList)first).getSeparator() == Separator.COLON ) {
48-
type = "map";
67+
switch( param.getItemType() ) {
68+
case SCSS_LIST:
69+
type = "list";
70+
SassList list = (SassList)param;
71+
if( list.size() > 0 ) {
72+
// we need only to check the first entry, the other has the parser already checked
73+
SassListItem first = list.get( 0 );
74+
if( first.getClass() == SassList.class && ((SassList)first).getSeparator() == Separator.COLON ) {
75+
type = "map";
76+
}
4977
}
50-
}
51-
} else if (param instanceof LexicalUnitImpl) {
52-
LexicalUnitImpl unit = (LexicalUnitImpl) param;
53-
if (unit.getItemType() == LexicalUnitImpl.SCSS_NULL) {
78+
break;
79+
case SCSS_NULL:
5480
type = "null";
55-
} else if (isNumber(unit)) {
81+
break;
82+
case SAC_INTEGER:
83+
case SAC_REAL:
84+
case SAC_EM:
85+
case SAC_LEM:
86+
case SAC_REM:
87+
case SAC_EX:
88+
case SAC_PIXEL:
89+
case SAC_INCH:
90+
case SAC_CENTIMETER:
91+
case SAC_MILLIMETER:
92+
case SAC_POINT:
93+
case SAC_PICA:
94+
case SAC_PERCENTAGE:
95+
case SAC_DEGREE:
96+
case SAC_GRADIAN:
97+
case SAC_RADIAN:
98+
case SAC_MILLISECOND:
99+
case SAC_SECOND:
100+
case SAC_HERTZ:
101+
case SAC_KILOHERTZ:
102+
case SAC_DIMENSION:
56103
type = "number";
57-
} else if (isBoolean(unit)) {
58-
type = "bool";
59-
} else if (unit.getItemType() == LexicalUnitImpl.SAC_RGBCOLOR) {
60-
type = "color";
61-
} else if( ColorUtil.isColorName( unit ) ) {
62-
type = "color";
63-
} else if( ColorUtil.isHexColor( unit ) ) {
64-
type = "color";
65-
} else if (unit.getItemType() == LexicalUnitImpl.SAC_FUNCTION) {
66-
if ("rgb".equals(unit.getFunctionName())
67-
|| "rgba".equals(unit.getFunctionName())
68-
|| "hsl".equals(unit.getFunctionName())
69-
|| "hsla".equals(unit.getFunctionName())) {
104+
break;
105+
case SAC_IDENT:
106+
String str = ((LexicalUnitImpl)param).getStringValue();
107+
if( "true".equals( str ) || "false".equals( str ) ) {
108+
type = "bool";
109+
break;
110+
}
111+
if( ColorUtil.isColorName( str ) || ColorUtil.isHexColor( str ) ) {
70112
type = "color";
113+
break;
71114
}
72-
}
73-
}
74-
75-
return createIdent(function, type);
76-
}
77-
78-
private boolean isBoolean(LexicalUnitImpl unit) {
79-
if (unit.getItemType() != LexicalUnitImpl.SAC_IDENT) {
80-
return false;
115+
break;
116+
case SAC_RGBCOLOR:
117+
type = "color";
118+
break;
119+
case SAC_FUNCTION:
120+
str = ((LexicalUnitImpl)param).getFunctionName();
121+
switch( str ) {
122+
case "rgb":
123+
case "rgba":
124+
case "hsl":
125+
case "hsla":
126+
type = "color";
127+
break;
128+
}
129+
break;
81130
}
82-
return "true".equals(unit.getStringValue())
83-
|| "false".equals(unit.getStringValue());
84-
}
85-
86-
private boolean isNumber(LexicalUnitImpl unit) {
87-
return unit.isNumber();
88-
}
89131

90-
private LexicalUnitImpl createIdent(LexicalUnitImpl function,
91-
String paramType) {
92-
return LexicalUnitImpl.createRawIdent( function.getUri(), function.getLineNumber(), function.getColumnNumber(), paramType );
132+
return LexicalUnitImpl.createRawIdent( function.getUri(), function.getLineNumber(), function.getColumnNumber(), type );
93133
}
94-
95134
}

src/main/java/com/inet/sass/util/ColorUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ public static boolean isHexColor(LexicalUnitImpl unit) {
266266
if( unit.getItemType() != SCSSLexicalUnit.SAC_IDENT ) {
267267
return false;
268268
}
269-
String str = unit.getStringValue();
269+
return isHexColor( unit.getStringValue() );
270+
}
271+
272+
public static boolean isHexColor( String str ) {
270273
int length = str.length();
271274
switch( length ) {
272275
case 4:
@@ -309,6 +312,10 @@ public static boolean isColorName(LexicalUnitImpl unit) {
309312
&& colorNameToHex.containsKey(unit.getStringValue());
310313
}
311314

315+
public static boolean isColorName( String str ) {
316+
return colorNameToHex.containsKey( str );
317+
}
318+
312319
/**
313320
* Returns true if the lexical unit represents a valid hsl() color function
314321
* call, false otherwise.

src/main/java/com/inet/sass/visitor/VariableNodeHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ public static void traverse( ScssContext context, VariableNode node ) {
3636
context.setVariable( node.getVariable() );
3737
} else { // Handle the case where a variable has the value SCSS_NULL
3838
SassListItem value = variable.getExpr();
39-
if( value instanceof LexicalUnitImpl ) {
40-
LexicalUnitImpl unit = (LexicalUnitImpl)value;
41-
if( unit.getItemType() == SCSSLexicalUnit.SCSS_NULL ) {
42-
context.setVariable( node.getVariable() );
43-
}
39+
if( value.getItemType() == SCSSLexicalUnit.SCSS_NULL ) {
40+
context.setVariable( node.getVariable() );
4441
}
4542
}
4643
}

0 commit comments

Comments
 (0)