Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Commit 4d99e4f

Browse files
Key type wrapper.
1 parent ec65954 commit 4d99e4f

5 files changed

Lines changed: 211 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.sansa_stack.inference.flink.utils
2+
3+
import org.apache.jena.graph.Node
4+
import org.apache.jena.sparql.util.NodeComparator
5+
6+
/**
7+
* Key type wrapper for Jena `Node` objects.
8+
* It basically makes Node comparable which is necessary to be handles as key in Flink.
9+
*
10+
* @author Lorenz Buehmann
11+
*/
12+
class NodeKey(val node: Node) extends Comparable[NodeKey] with Equals {
13+
14+
override def compareTo(o: NodeKey): Int = {
15+
val other = o.node
16+
if (node == null)
17+
if (other == null) 0 else -1
18+
else
19+
if (other == null) 1 else new NodeComparator().compare(node, other)
20+
}
21+
22+
override def canEqual(that: Any): Boolean = that.isInstanceOf[NodeKey]
23+
24+
override def hashCode(): Int = 31 * node.##
25+
26+
override def equals(that: Any): Boolean =
27+
that match {
28+
case key: NodeKey => (this eq key) || (key.canEqual(this) && hashCode == key.hashCode)
29+
case _ => false
30+
}
31+
}
32+
33+
object NodeKey {
34+
def apply(node: Node): NodeKey = new NodeKey(node)
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.sansa_stack.inference.flink.utils.key
2+
3+
/**
4+
* Base of a tuple-like generic key.
5+
*
6+
* @tparam T The type of the concrete key type.
7+
*/
8+
abstract class Key[T <: Key[T]] extends Comparable[T] {
9+
/**
10+
* Gets the i-th element of the tuple-like key.
11+
*
12+
* @param pos The position.
13+
* @return The element at that key position;
14+
*/
15+
def get(pos: Int): Any
16+
}
17+
18+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.sansa_stack.inference.flink.utils.key
2+
3+
/**
4+
* A key with one key field.
5+
*
6+
* @tparam T1 The type of the field.
7+
*/
8+
class Key1[T1 <: Comparable[T1]](val value1: T1) extends Key[Key1[T1]] with Equals {
9+
10+
def get(pos: Int): Any = pos match {
11+
case 0 =>
12+
value1
13+
case _ =>
14+
throw new IndexOutOfBoundsException
15+
}
16+
17+
override def hashCode: Int = if (value1 == null) 0 else value1.hashCode
18+
19+
override def canEqual(that: Any): Boolean = that.isInstanceOf[Key1[T1]]
20+
21+
override def equals(obj: Any): Boolean =
22+
obj match {
23+
case that: Key1[T1] => (this eq that) || (this.canEqual(that) && (value1 == that.value1))
24+
case _ => false
25+
}
26+
27+
override def toString: String = s"Key1 ($value1)"
28+
29+
def compareTo(o: Key1[T1]): Int = {
30+
val other = o.value1
31+
if (value1 == null)
32+
if (other == null) 0 else -1
33+
else
34+
if (other == null) 1 else value1.compareTo(other)
35+
}
36+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.sansa_stack.inference.flink.utils.key
2+
3+
/**
4+
* A key with two key fields.
5+
*
6+
* @tparam T1 The type of the first field.
7+
* @tparam T2 The type of the second field.
8+
*/
9+
class Key2[T1 <: Comparable[T1], T2 <: Comparable[T2]](val value1: T1, val value2: T2)
10+
extends Key[Key2[T1, T2]]
11+
with Equals {
12+
13+
def get(pos: Int): Any = pos match {
14+
case 0 =>
15+
value1
16+
case 1 =>
17+
value2
18+
case _ =>
19+
throw new IndexOutOfBoundsException
20+
}
21+
22+
override def hashCode: Int = {
23+
val c1: Int = if (value1 == null) 0 else value1.hashCode
24+
val c2: Int = if (value2 == null) 0 else value2.hashCode
25+
c1 * 17 + c2 * 31
26+
}
27+
28+
override def canEqual(that: Any): Boolean = that.isInstanceOf[Key2[T1, T2]]
29+
30+
override def equals(obj: Any): Boolean =
31+
obj match {
32+
case that: Key2[T1, T2] => (this eq that) || (this.canEqual(that) && (value1 == that.value1) && (value2 == that.value2))
33+
case _ => false
34+
}
35+
36+
override def toString: String = s"Key2 ($value1, $value2)"
37+
38+
def compareTo(o: Key2[T1, T2]): Int = {
39+
val other1 = o.value1
40+
val other2 = o.value2
41+
42+
val c1 = if (value1 == null)
43+
if (other1 == null) 0 else -1
44+
else
45+
if (other1 == null) 1 else value1.compareTo(other1)
46+
47+
if(c1 != 0) c1 else
48+
if (value2 == null)
49+
if (other2 == null) 0 else -1
50+
else
51+
if (other2 == null) 1 else value2.compareTo(other2)
52+
}
53+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package net.sansa_stack.inference.flink.utils.key
2+
3+
/**
4+
* A key with two key fields.
5+
*
6+
* @tparam T1 The type of the first field.
7+
* @tparam T2 The type of the second field.
8+
* @tparam T3 The type of the third field.
9+
*/
10+
class Key3[T1 <: Comparable[T1], T2 <: Comparable[T2], T3 <: Comparable[T3]](val value1: T1, val value2: T2, val value3: T3)
11+
extends Key[Key3[T1, T2, T3]]
12+
with Equals {
13+
14+
def get(pos: Int): Any = pos match {
15+
case 0 =>
16+
value1
17+
case 1 =>
18+
value2
19+
case 2 =>
20+
value3
21+
case _ =>
22+
throw new IndexOutOfBoundsException
23+
}
24+
25+
override def hashCode: Int = {
26+
val c1: Int = if (value1 == null) 0 else value1.hashCode
27+
val c2: Int = if (value2 == null) 0 else value2.hashCode
28+
val c3: Int = if (value3 == null) 0 else value3.hashCode
29+
c1 * 17 + c2 * 31 + c3 * 47
30+
}
31+
32+
override def canEqual(that: Any): Boolean = that.isInstanceOf[Key3[T1, T2, T3]]
33+
34+
override def equals(obj: Any): Boolean =
35+
obj match {
36+
case that: Key3[T1, T2, T3] =>
37+
(this eq that) || (this.canEqual(that) && (value1 == that.value1) && (value2 == that.value2) && (value3 == that.value3))
38+
case _ => false
39+
}
40+
41+
override def toString: String = s"Key3 ($value1, $value2, $value3)"
42+
43+
def compareTo(o: Key3[T1, T2, T3]): Int = {
44+
val other1 = o.value1
45+
val other2 = o.value2
46+
val other3 = o.value3
47+
48+
val c1 = if (value1 == null)
49+
if (other1 == null) 0 else -1
50+
else
51+
if (other1 == null) 1 else value1.compareTo(other1)
52+
53+
if(c1 != 0) c1 else {
54+
val c2 = if (value2 == null)
55+
if (other2 == null) 0 else -1
56+
else
57+
if (other2 == null) 1 else value2.compareTo(other2)
58+
59+
if(c2 != 0) c2 else {
60+
if (value3 == null)
61+
if (other3 == null) 0 else -1
62+
else
63+
if (other3 == null) 1 else value3.compareTo(other3)
64+
}
65+
66+
}
67+
68+
}
69+
}

0 commit comments

Comments
 (0)