Skip to content
Permalink
Browse files

Map based NodeNumberer

  • Loading branch information...
szymonm committed Jul 19, 2014
1 parent 0e7b4dc commit 966071bb072e82e39f85b5eb0b4f2824ad4b8eeb
@@ -0,0 +1,61 @@
/*
* Copyright 2014 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.twitter.cassovary.util

import scala.collection.mutable
import scala.io.Source

class MapNodeNumberer[T](externalToInternalMap: collection.Map[T, Int],
internalToExternalMap: collection.Map[Int, T])
extends NodeNumberer[T] {
override def externalToInternal(externalNodeId: T): Int = externalToInternalMap(externalNodeId)

override def internalToExternal(internalNodeId: Int): T = internalToExternalMap(internalNodeId)
}

object MapNodeNumberer {

/**
* Reads map numberer from file in the following format:
* {{{
* name1 id1
* name2 id2
* ...
* }}}
* for example:
* {{{
* banana 12
* apple 2
* spoon 8
* }}}
*/
def forStringsFromFile(filename: String): MapNodeNumberer[String] = {
val internalToExternal = mutable.HashMap[Int, String]()
val externalToInternal = mutable.HashMap[String, Int]()

Source.fromFile(filename).getLines().foreach {
line =>
val lineAsArray = line.split(" ")
val name = lineAsArray(0)
val id = lineAsArray(1).toInt
assert(!internalToExternal.contains(id), "Duplicate id found: " + id)
internalToExternal += ((id, name))
assert(!externalToInternal.contains(name), "Duplicate name found: " + name)
externalToInternal += ((name, id))
}

new MapNodeNumberer[String](externalToInternal, internalToExternal)
}
}
@@ -0,0 +1,3 @@
orange 12
spoon 2
table 12
@@ -0,0 +1,4 @@
orange 12
banana 3
apple 7
orange 2
@@ -0,0 +1,4 @@
banana 2
orange 7
spoon 29
bike 12
@@ -0,0 +1,54 @@
/*
* Copyright 2014 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.twitter.cassovary.util

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.WordSpec
import org.scalatest.matchers.{ClassicMatchers, ShouldMatchers}

@RunWith(classOf[JUnitRunner])
class MapNodeNumbererSpec extends WordSpec with ShouldMatchers with ClassicMatchers {
val directory = "cassovary-core/src/test/resources/nodeNumberers/"

"Map node numberer" should {
"load correctly from the file" in {
val numberer = MapNodeNumberer.forStringsFromFile(directory + "numbering1.txt")
numberer.externalToInternal("banana") should be (2)
numberer.externalToInternal("orange") should be (7)
numberer.externalToInternal("spoon") should be (29)
numberer.externalToInternal("bike") should be (12)

numberer.internalToExternal(2) should be ("banana")
numberer.internalToExternal(7) should be ("orange")
numberer.internalToExternal(29) should be ("spoon")
numberer.internalToExternal(12) should be ("bike")

evaluating (numberer.externalToInternal("apple")) should produce [NoSuchElementException]

evaluating (numberer.internalToExternal(3)) should produce [NoSuchElementException]
}

"find duplicated names" in {
evaluating (MapNodeNumberer.forStringsFromFile(directory + "numbering-duplicatedId.txt")) should
produce [AssertionError]
}

"find duplicated ids" in {
evaluating (MapNodeNumberer.forStringsFromFile(directory + "numbering-duplicatedName.txt")) should
produce [AssertionError]
}
}
}

0 comments on commit 966071b

Please sign in to comment.
You can’t perform that action at this time.