-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix some more collection errors #1431
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.daffodil.lib.util | ||
|
||
import scala.collection.mutable | ||
|
||
/** | ||
* Compatibility class for 2.12 and 2.13 since MultiMap and inheritance | ||
* from class mutable.HashMap have been deprecated in 2.13. | ||
*/ | ||
class MultiMapWrapper[K, V] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is a wrapper, but I think this should just be called MultiMap. |
||
private val underlying = mutable.Map.empty[K, mutable.Set[V]] | ||
|
||
def addBinding(key: K, value: V): Unit = | ||
underlying.getOrElseUpdate(key, mutable.Set.empty) += value | ||
|
||
def addBinding(key: K, values: mutable.Set[V]): Unit = { | ||
values.foreach(addBinding(key, _)) | ||
} | ||
|
||
def removeBinding(key: K, value: V): Unit = | ||
underlying.get(key).foreach { values => | ||
values -= value | ||
if (values.isEmpty) underlying -= key | ||
Check warning on line 39 in daffodil-lib/src/main/scala/org/apache/daffodil/lib/util/MultiMapWrapper.scala
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to suggest we just ignore these codecov as part of this scala 2.13/scala 3 conversion process. We can always run a complete code-cov analysis later. |
||
} | ||
|
||
def get(key: K): Option[mutable.Set[V]] = underlying.get(key) | ||
|
||
def keys: Iterable[K] = underlying.keys | ||
|
||
def iterator: Iterator[(K, mutable.Set[V])] = underlying.iterator | ||
|
||
def filter(func: (K, mutable.Set[V]) => Boolean): MultiMapWrapper[K, V] = { | ||
val filtered = new MultiMapWrapper[K, V] | ||
for ((key, values) <- underlying) { | ||
if (func(key, values)) { | ||
filtered.addBinding(key, values) | ||
} | ||
} | ||
filtered | ||
} | ||
|
||
def map[T](func: (K, mutable.Set[V]) => T): collection.Seq[T] = { | ||
val ret = mutable.ListBuffer.empty[T] | ||
for ((key, values) <- underlying) { | ||
ret.append(func(key, values)) | ||
} | ||
ret | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever we do this sort of scala 2.12 compatibility thing, I think we should anticipate that we will drop scala 2.12 fairly quickly once we've moved to scala 2.13 or scala 3. So I would add a
// TODO: scala 2.12 phase out
or other easily identified TODO item.