Permalink
Browse files

make better use of RichFields when available (avoids some code duplic…

…ation)
  • Loading branch information...
aaron-siegel committed Sep 12, 2012
1 parent d9f5bc0 commit a2d1ec199a41c7e548a9b707a7c08072d59f0937
View
@@ -9,3 +9,4 @@ project/plugins/lib_managed/
project/plugins/src_managed/
/.idea/
/.idea_modules/
+*.iml
@@ -131,11 +131,7 @@ trait FieldConversions extends LowPriorityFieldConversions {
new Fields(x.name)
}
}
- implicit def fieldToFields(f : Field[_]) = {
- val fields = new Fields(f.id)
- fields.setComparators(f.ord)
- fields
- }
+ implicit def fieldToFields(f : Field[_]) = RichFields(f)
@tailrec
final def newSymbol(avoid : Set[Symbol], guess : Symbol, trial : Int = 0) : Symbol = {
@@ -186,11 +182,8 @@ trait FieldConversions extends LowPriorityFieldConversions {
implicit def intFields[T <: TraversableOnce[Int]](f : T) = {
new Fields(f.toSeq.map { new java.lang.Integer(_) } : _*)
}
- implicit def fieldFields[T <: TraversableOnce[Field[_]]](f : T) = {
- val fields = new Fields(f.toSeq.map(_.id) : _*)
- f.foreach { field => fields.setComparator(field.id, field.ord) }
- fields
- }
+ implicit def fieldFields[T <: TraversableOnce[Field[_]]](f : T) = RichFields[Any](f.toSeq)
+
/**
* Useful to convert f : Any* to Fields. This handles mixed cases ("hey", 'you).
* Not sure we should be this flexible, but given that Cascading will throw an
@@ -35,7 +35,10 @@ class FieldImpsTest extends Specification with FieldConversions {
checkFieldsWithComparators(vF, fields)
}
def checkFieldsWithComparators(actual: Fields, expected: Fields) {
- actual.equals(expected) must beTrue
+ // sometimes one or the other is actually a RichFields, so rather than test for
+ // actual.equals(expected), we just check that all the field names and comparators line up
+ actual.size must_== expected.size
+ (0 until actual.size).foreach { i => actual.get(i).equals(expected.get(i)) must beTrue }
actual.getComparators.toSeq.equals(expected.getComparators.toSeq) must beTrue
}
"Field" should {
@@ -88,7 +91,7 @@ class FieldImpsTest extends Specification with FieldConversions {
// BigInteger is just a convenient non-primitive ordered type
setAndCheckField(Field[java.math.BigInteger]("foo"))
setAndCheckField(Field[java.math.BigInteger]('bar))
- setAndCheckField(Field[java.math.BigInteger](3))
+ setAndCheckField(Field[java.math.BigInteger](0))
// Try a custom ordering
val ord = implicitly[Ordering[java.math.BigInteger]].reverse
setAndCheckField(Field[java.math.BigInteger]("bell")(ord, implicitly[Manifest[java.math.BigInteger]]))
@@ -167,7 +170,7 @@ class FieldImpsTest extends Specification with FieldConversions {
f2 must be_==((fields, new Fields("bell")))
f2 = (foo -> ('bar,'bell))
- fields = new Fields("foo")
+ fields = RichFields(foo)
fields.setComparators(foo.ord)
f2 must be_==((fields, new Fields("bar", "bell")))

0 comments on commit a2d1ec1

Please sign in to comment.