Skip to content

Commit ac44409

Browse files
committed
use singleton objects for label parsers
1 parent 3b1a7c6 commit ac44409

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

mllib/src/main/scala/org/apache/spark/mllib/util/LabelParsers.scala

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,23 @@ trait LabelParser extends Serializable {
2727
* Label parser for binary labels, which outputs 1.0 (positive) if the value is greater than 0.5,
2828
* or 0.0 (negative) otherwise. So it works with +1/-1 labeling and +1/0 labeling.
2929
*/
30-
class BinaryLabelParser extends LabelParser {
30+
object BinaryLabelParser extends LabelParser {
31+
/** Gets the default instance of BinaryLabelParser. */
32+
def getInstance(): LabelParser = this
33+
3134
/**
3235
* Parses the input label into positive (1.0) if the value is greater than 0.5,
3336
* or negative (0.0) otherwise.
3437
*/
3538
override def parse(labelString: String): Double = if (labelString.toDouble > 0.5) 1.0 else 0.0
3639
}
3740

38-
object BinaryLabelParser extends BinaryLabelParser {
39-
/** Gets the default instance of BinaryLabelParser. */
40-
def getInstance(): BinaryLabelParser = this
41-
}
42-
4341
/**
4442
* Label parser for multiclass labels, which converts the input label to double.
4543
*/
46-
class MulticlassLabelParser extends LabelParser {
47-
override def parse(labelString: String): Double = labelString.toDouble
48-
}
49-
50-
object MulticlassLabelParser extends MulticlassLabelParser {
44+
object MulticlassLabelParser extends LabelParser {
5145
/** Gets the default instance of MulticlassLabelParser. */
52-
def getInstance(): MulticlassLabelParser = this
46+
def getInstance(): LabelParser = this
47+
48+
override def parse(labelString: String): Double = labelString.toDouble
5349
}

0 commit comments

Comments
 (0)