Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Created May 12, 2014 21:49
Show Gist options
  • Select an option

  • Save solidsnack/2515fdb03793ec7aaccb to your computer and use it in GitHub Desktop.

Select an option

Save solidsnack/2515fdb03793ec7aaccb to your computer and use it in GitHub Desktop.
package mesosphere.servicenet.dsl
import scala.Ordered
import Ordering.Implicits._
/**
* Names for network entities.
*
* The sort order for names is the intuitive one for DNS names: sort on dotted
* subcomponents, starting from the back (might be revisited for network
* interface names).
*
* There are length and content limitations to impose at some point in the
* future. DNS names should contain only letters, digits and hyphens, and be
* under 255 octets in total. Linux device names should probably come from the
* same set of characters -- and must be much shorter.
*/
case class Name(components: Seq[String]) extends Ordered[Name] {
def compare(other: Name) = Orderings.NameOrdering.compare(this, other)
}
object Name {
def dotted(s: String): Seq[String] = s.split('.').reverse.toSeq
def apply(s: String): Name = Name(dotted(s))
implicit def String2Name(s: String): Name = Name(s)
implicit def Name2String(n: Name): String =
n.components.reverse.mkString(".")
}
/**
* Sorts for datatypes related to network entity names.
*/
object Orderings {
implicit val NameOrdering: Ordering[Name] =
Ordering.by[Name, Seq[String]](_.components)
implicit val StringOrdering: Ordering[String] =
Ordering.by[String, Name](Name(_))
implicit val ChangeOrdering: Ordering[Change] = Ordering.by {
case Add(item) => item.name()
case Remove(name) => name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment