Skip to content

Commit c163f5e

Browse files
authored
loneElement and loneElementOption: extension methods for Iterable (#1827)
* loneElement and loneElementOption: extension methods for Iterable * remove 'hint' from loneElementOption * remove 'hint' from loneElementOption * moved to flatgraph. only change left: deprecation `onlyChecked` * latest fg
1 parent 16465a8 commit c163f5e

File tree

3 files changed

+8
-30
lines changed

3 files changed

+8
-30
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name := "codepropertygraph"
22

33
// parsed by project/Versions.scala, updated by updateDependencies.sh
4-
val flatgraphVersion = "0.1.22"
4+
val flatgraphVersion = "0.1.23"
55

66
inThisBuild(
77
List(

codepropertygraph/src/main/scala/io/shiftleft/Implicits.scala

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,23 @@ package io.shiftleft
33
import org.slf4j.{Logger, LoggerFactory}
44

55
object Implicits {
6-
76
private val logger: Logger = LoggerFactory.getLogger(Implicits.getClass)
87

98
implicit class IterableOnceDeco[T](val iterable: IterableOnce[T]) extends AnyVal {
9+
@deprecated(
10+
"please use `.loneElement` from flatgraph (mixed into the generated `language` packages) instead, which has a better name and will throw if the iterable has more than one element (rather than just log.warn)",
11+
since = "1.7.42 (July 2025)"
12+
)
1013
def onlyChecked: T = {
1114
if (iterable.iterator.hasNext) {
1215
val res = iterable.iterator.next()
1316
if (iterable.iterator.hasNext) {
1417
logger.warn("iterator was expected to have exactly one element, but it actually has more")
1518
}
1619
res
17-
} else { throw new NoSuchElementException() }
18-
}
19-
}
20-
21-
/** A wrapper around a Java iterator that throws a proper NoSuchElementException.
22-
*
23-
* Proper in this case means an exception with a stack trace. This is intended to be used as a replacement for next()
24-
* on the iterators returned from TinkerPop since those are missing stack traces.
25-
*/
26-
implicit class JavaIteratorDeco[T](val iterator: java.util.Iterator[T]) extends AnyVal {
27-
def nextChecked: T = {
28-
try {
29-
iterator.next
30-
} catch {
31-
case _: NoSuchElementException =>
32-
throw new NoSuchElementException()
20+
} else {
21+
throw new NoSuchElementException()
3322
}
3423
}
35-
36-
def onlyChecked: T = {
37-
if (iterator.hasNext) {
38-
val res = iterator.next
39-
if (iterator.hasNext) {
40-
logger.warn("iterator was expected to have exactly one element, but it actually has more")
41-
}
42-
res
43-
} else { throw new NoSuchElementException() }
44-
}
4524
}
46-
4725
}

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.10.11
1+
sbt.version=1.11.2

0 commit comments

Comments
 (0)