Skip to content
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

Better null handling for isSameContig utility #453

Merged
merged 1 commit into from Nov 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions adam-core/src/main/scala/org/bdgenomics/adam/util/Util.scala
Expand Up @@ -21,8 +21,11 @@ import org.bdgenomics.formats.avro.Contig

object Util {
def isSameContig(left: Contig, right: Contig): Boolean = {
left.getContigName == right.getContigName && (
left.getContigMD5 == null || right.getContigMD5 == null || left.getContigMD5 == right.getContigMD5)
val leftName = Option(left).map(_.getContigName)
val leftMD5 = Option(left).map(_.getContigMD5)
val rightName = Option(right).map(_.getContigName)
val rightMD5 = Option(right).map(_.getContigMD5)
leftName == rightName && (leftMD5.isEmpty || rightMD5.isEmpty || leftMD5 == rightMD5)
}

def hashCombine(parts: Int*): Int =
Expand Down
43 changes: 43 additions & 0 deletions adam-core/src/test/scala/org/bdgenomics/adam/util/UtilSuite.scala
@@ -0,0 +1,43 @@
/**
* Licensed to Big Data Genomics (BDG) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The BDG 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.bdgenomics.adam.util

import org.bdgenomics.formats.avro.Contig

class UtilSuite extends SparkFunSuite {

test("isSameConfig") {
val a = Contig.newBuilder().setContigName("foo")
val b = Contig.newBuilder().setContigName("bar")
assert(!Util.isSameContig(a.build(), b.build()))
b.setContigName("foo")
assert(Util.isSameContig(a.build(), b.build()))

// proper null handling
assert(Util.isSameContig(null, null))
assert(!Util.isSameContig(null, b.build()))
assert(!Util.isSameContig(a.build(), null))

a.setContigMD5("md5")
// both md5s need to be set to change equality
assert(!Util.isSameContig(a.build(), b.build()))
b.setContigMD5("md5")
assert(Util.isSameContig(a.build(), b.build()))
}

}