Skip to content

Commit 1953fcc

Browse files
committed
fun with lambdas
1 parent 39b7b48 commit 1953fcc

3 files changed

Lines changed: 12 additions & 21 deletions

File tree

src/main/java/org/htmlunit/cssparser/dom/AbstractColor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.htmlunit.cssparser.dom;
1616

1717
import java.io.Serializable;
18+
import java.util.function.Consumer;
1819

1920
import org.htmlunit.cssparser.parser.LexicalUnit;
2021
import org.htmlunit.cssparser.parser.LexicalUnit.LexicalUnitType;
@@ -29,14 +30,15 @@ public class AbstractColor implements Serializable {
2930

3031
private CSSValueImpl alpha_;
3132

32-
protected static CSSValueImpl getNumberPercentagePart(final LexicalUnit next) {
33+
protected void getNumberPercentagePart(final LexicalUnit next, final Consumer<CSSValueImpl> setter) {
3334
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
3435

3536
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
3637
|| LexicalUnitType.REAL == next.getLexicalUnitType()
3738

3839
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
39-
return new CSSValueImpl(next, true);
40+
setter.accept(new CSSValueImpl(next, true));
41+
return;
4042
}
4143

4244
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");

src/main/java/org/htmlunit/cssparser/dom/LABColorImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5454
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5555
}
5656

57-
lightness_ = getNumberPercentagePart(next);
57+
getNumberPercentagePart(next, this::setLightness);
5858

5959
next = next.getNextLexicalUnit();
6060
if (next == null) {
6161
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6262
}
6363

64-
aDistance_ = getNumberPercentagePart(next);
64+
getNumberPercentagePart(next, this::setA);
6565
next = next.getNextLexicalUnit();
6666
if (next == null) {
6767
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
6868
}
6969

70-
bDistance_ = getNumberPercentagePart(next);
70+
getNumberPercentagePart(next, this::setB);
7171
next = next.getNextLexicalUnit();
7272
if (next == null) {
7373
return;

src/main/java/org/htmlunit/cssparser/dom/RGBColorImpl.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5555
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5656
}
5757

58-
red_ = getPart(next);
58+
getNumberPercentagePart(next, this::setRed);
5959

6060
next = next.getNextLexicalUnit();
6161
if (next == null) {
@@ -78,7 +78,7 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
7878
throw new DOMException(DOMException.SYNTAX_ERR,
7979
"'" + function_ + "' has to use blank as separator if none is used.");
8080
}
81-
green_ = getPart(next);
81+
getNumberPercentagePart(next, this::setGreen);
8282

8383
next = next.getNextLexicalUnit();
8484
if (next == null) {
@@ -99,7 +99,7 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
9999
throw new DOMException(DOMException.SYNTAX_ERR,
100100
"'" + function_ + "' has to use blank as separator if none is used.");
101101
}
102-
blue_ = getPart(next);
102+
getNumberPercentagePart(next, this::setBlue);
103103

104104
next = next.getNextLexicalUnit();
105105
if (next == null) {
@@ -130,7 +130,7 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
130130
return;
131131
}
132132

133-
green_ = getPart(next);
133+
getNumberPercentagePart(next, this::setGreen);
134134
next = next.getNextLexicalUnit();
135135
if (next == null) {
136136
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
@@ -140,7 +140,7 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
140140
"'" + function_ + "' requires consitent separators (blank or comma).");
141141
}
142142

143-
blue_ = getPart(next);
143+
getNumberPercentagePart(next, this::setBlue);
144144
next = next.getNextLexicalUnit();
145145
if (next == null) {
146146
return;
@@ -163,17 +163,6 @@ public RGBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
163163
}
164164
}
165165

166-
private static CSSValueImpl getPart(final LexicalUnit next) {
167-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
168-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
169-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
170-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
171-
return new CSSValueImpl(next, true);
172-
}
173-
174-
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
175-
}
176-
177166
/**
178167
* <p>getRed.</p>
179168
*

0 commit comments

Comments
 (0)