From e5f88b5ba997e9b4327c0188c5ebf094ff62e869 Mon Sep 17 00:00:00 2001 From: Matt Massie Date: Sun, 2 Nov 2014 07:13:18 -0800 Subject: [PATCH] Better null handling for isSameContig utility --- .../scala/org/bdgenomics/adam/util/Util.scala | 7 ++- .../org/bdgenomics/adam/util/UtilSuite.scala | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 adam-core/src/test/scala/org/bdgenomics/adam/util/UtilSuite.scala diff --git a/adam-core/src/main/scala/org/bdgenomics/adam/util/Util.scala b/adam-core/src/main/scala/org/bdgenomics/adam/util/Util.scala index 7eff4e2a51..2010ebc760 100644 --- a/adam-core/src/main/scala/org/bdgenomics/adam/util/Util.scala +++ b/adam-core/src/main/scala/org/bdgenomics/adam/util/Util.scala @@ -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 = diff --git a/adam-core/src/test/scala/org/bdgenomics/adam/util/UtilSuite.scala b/adam-core/src/test/scala/org/bdgenomics/adam/util/UtilSuite.scala new file mode 100644 index 0000000000..9a44ae8cad --- /dev/null +++ b/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())) + } + +}