Skip to content

Commit 098911b

Browse files
authored
Merge pull request #678 from dubinsky/equality-inequality
Equality cleanup:
2 parents 41b0294 + 1febe86 commit 098911b

25 files changed

+98
-129
lines changed

Diff for: jvm/src/test/scala/scala/xml/XMLTest.scala

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package scala.xml
22

33
import org.junit.{Test => UnitTest}
44
import org.junit.Assert.{assertEquals, assertFalse, assertNull, assertThrows, assertTrue}
5-
import java.io.StringWriter
6-
import java.io.ByteArrayOutputStream
5+
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStreamReader, IOException, ObjectInputStream,
6+
ObjectOutputStream, OutputStreamWriter, PrintStream, StringWriter}
77
import java.net.URL
88
import scala.xml.dtd.{DocType, PublicID}
99
import scala.xml.parsing.ConstructingParser
@@ -177,26 +177,26 @@ class XMLTestJVM {
177177
</entry>""", f("a,b,c").toString)
178178

179179
object Serialize {
180-
@throws(classOf[java.io.IOException])
180+
@throws(classOf[IOException])
181181
def write[A](o: A): Array[Byte] = {
182182
val ba: ByteArrayOutputStream = new ByteArrayOutputStream(512)
183-
val out: java.io.ObjectOutputStream = new java.io.ObjectOutputStream(ba)
183+
val out: ObjectOutputStream = new ObjectOutputStream(ba)
184184
out.writeObject(o)
185185
out.close()
186186
ba.toByteArray
187187
}
188-
@throws(classOf[java.io.IOException])
188+
@throws(classOf[IOException])
189189
@throws(classOf[ClassNotFoundException])
190190
def read[A](buffer: Array[Byte]): A = {
191-
val in: java.io.ObjectInputStream =
192-
new java.io.ObjectInputStream(new java.io.ByteArrayInputStream(buffer))
191+
val in: ObjectInputStream =
192+
new ObjectInputStream(new ByteArrayInputStream(buffer))
193193
in.readObject().asInstanceOf[A]
194194
}
195195
def check[A, B](x: A, y: B): Unit = {
196196
// println("x = " + x)
197197
// println("y = " + y)
198-
// println("x equals y: " + (x equals y) + ", y equals x: " + (y equals x))
199-
assertTrue(x.equals(y) && y.equals(x))
198+
// println("x == y: " + (x == y) + ", y == x: " + (y == x))
199+
assertTrue(x == y && y == x)
200200
// println()
201201
}
202202
}
@@ -296,14 +296,14 @@ class XMLTestJVM {
296296
// scala.xml.XML.save("foo.xml", xml)
297297
// scala.xml.XML.loadFile("foo.xml").toString
298298

299-
val outputStream: java.io.ByteArrayOutputStream = new java.io.ByteArrayOutputStream
300-
val streamWriter: java.io.OutputStreamWriter = new java.io.OutputStreamWriter(outputStream, "UTF-8")
299+
val outputStream: ByteArrayOutputStream = new ByteArrayOutputStream
300+
val streamWriter: OutputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8")
301301

302302
XML.write(streamWriter, xml, XML.encoding, xmlDecl = false, null)
303303
streamWriter.flush()
304304

305-
val inputStream: java.io.ByteArrayInputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
306-
val streamReader: java.io.InputStreamReader = new java.io.InputStreamReader(inputStream, XML.encoding)
305+
val inputStream: ByteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray)
306+
val streamReader: InputStreamReader = new InputStreamReader(inputStream, XML.encoding)
307307

308308
assertEquals(xml.toString, XML.load(streamReader).toString)
309309
}
@@ -492,8 +492,6 @@ class XMLTestJVM {
492492

493493
@UnitTest
494494
def dontLoop(): Unit = {
495-
import java.io.{ Console => _, _ }
496-
497495
val xml: String = "<!DOCTYPE xmeml SYSTEM 'uri'> <xmeml> <sequence> </sequence> </xmeml> "
498496
val sink: PrintStream = new PrintStream(new ByteArrayOutputStream())
499497
Console.withOut(sink) {
@@ -1026,7 +1024,7 @@ class XMLTestJVM {
10261024

10271025
def toSource(s: String): scala.io.Source = new scala.io.Source {
10281026
override val iter: Iterator[Char] = s.iterator
1029-
override def reportError(pos: Int, msg: String, out: java.io.PrintStream = Console.err): Unit = ()
1027+
override def reportError(pos: Int, msg: String, out: PrintStream = Console.err): Unit = ()
10301028
}
10311029

10321030
@UnitTest

Diff for: shared/src/main/scala/scala/xml/Attribute.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ trait Attribute extends MetaData {
8181
}
8282

8383
/** Returns an iterator on attributes */
84-
override def iterator: Iterator[MetaData] = {
84+
override def iterator: Iterator[MetaData] =
8585
if (value == null) next.iterator
8686
else Iterator.single(this) ++ next.iterator
87-
}
8887

89-
override def size: Int = {
88+
override def size: Int =
9089
if (value == null) next.size
9190
else 1 + next.size
92-
}
9391

9492
/**
9593
* Appends string representation of only this attribute to stringbuffer.

Diff for: shared/src/main/scala/scala/xml/Comment.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ case class Comment(commentText: String) extends SpecialNode {
2929
final override def doCollectNamespaces: Boolean = false
3030
final override def doTransform: Boolean = false
3131

32-
if (commentText.contains("--")) {
32+
if (commentText.contains("--"))
3333
throw new IllegalArgumentException(s"""text contains "--"""")
34-
}
35-
if (commentText.nonEmpty && commentText.charAt(commentText.length - 1) == '-') {
34+
35+
if (commentText.nonEmpty && commentText.charAt(commentText.length - 1) == '-')
3636
throw new IllegalArgumentException("The final character of a XML comment may not be '-'. See https://www.w3.org/TR/xml11//#IDA5CES")
37-
}
3837

3938
/**
4039
* Appends &quot;<!-- text -->&quot; to this string buffer.

Diff for: shared/src/main/scala/scala/xml/Equality.scala

+4-9
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,13 @@ object Equality {
6161
case x: NodeSeq if x.length == 1 => x2 == x(0)
6262
case _ => false
6363
}
64-
def compareBlithely(x1: AnyRef, x2: AnyRef): Boolean = {
65-
if (x1 == null || x2 == null)
66-
return x1.eq(x2)
67-
68-
x2 match {
64+
def compareBlithely(x1: AnyRef, x2: AnyRef): Boolean =
65+
if (x1 == null || x2 == null) x1 == null && x2 == null else x2 match {
6966
case s: String => compareBlithely(x1, s)
7067
case n: Node => compareBlithely(x1, n)
7168
case _ => false
7269
}
73-
}
7470
}
75-
import Equality._
7671

7772
trait Equality extends scala.Equals {
7873
protected def basisForHashCode: Seq[Any]
@@ -109,10 +104,10 @@ trait Equality extends scala.Equals {
109104
private def doComparison(other: Any, blithe: Boolean): Boolean = {
110105
val strictlyEqual: Boolean = other match {
111106
case x: AnyRef if this.eq(x) => true
112-
case x: Equality => (x canEqual this) && (this strict_== x)
107+
case x: Equality => x.canEqual(this) && this.strict_==(x)
113108
case _ => false
114109
}
115110

116-
strictlyEqual || (blithe && compareBlithely(this, asRef(other)))
111+
strictlyEqual || (blithe && Equality.compareBlithely(this, Equality.asRef(other)))
117112
}
118113
}

Diff for: shared/src/main/scala/scala/xml/MetaData.scala

+11-10
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,27 @@ object MetaData {
2929
*/
3030
@tailrec
3131
def concatenate(attribs: MetaData, new_tail: MetaData): MetaData =
32-
if (attribs.eq(Null)) new_tail
32+
if (attribs.isNull) new_tail
3333
else concatenate(attribs.next, attribs.copy(new_tail))
3434

3535
/**
3636
* returns normalized MetaData, with all duplicates removed and namespace prefixes resolved to
3737
* namespace URIs via the given scope.
3838
*/
3939
def normalize(attribs: MetaData, scope: NamespaceBinding): MetaData = {
40-
def iterate(md: MetaData, normalized_attribs: MetaData, set: Set[String]): MetaData = {
41-
if (md.eq(Null)) {
40+
def iterate(md: MetaData, normalized_attribs: MetaData, set: Set[String]): MetaData =
41+
if (md.isNull) {
4242
normalized_attribs
43-
} else if (md.value.eq(null)) {
43+
} else if (md.value == null)
4444
iterate(md.next, normalized_attribs, set)
45-
} else {
45+
else {
4646
val key: String = getUniversalKey(md, scope)
47-
if (set(key)) {
47+
if (set(key))
4848
iterate(md.next, normalized_attribs, set)
49-
} else {
49+
else
5050
md.copy(iterate(md.next, normalized_attribs, set + key))
51-
}
5251
}
53-
}
52+
5453
iterate(attribs, Null, Set())
5554
}
5655

@@ -85,7 +84,9 @@ abstract class MetaData
8584
extends AbstractIterable[MetaData]
8685
with Iterable[MetaData]
8786
with Equality
88-
with Serializable {
87+
with Serializable
88+
{
89+
private[xml] def isNull: Boolean = this.eq(Null)
8990

9091
/**
9192
* Updates this MetaData with the MetaData given as argument. All attributes that occur in updates

Diff for: shared/src/main/scala/scala/xml/NamespaceBinding.scala

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ case class NamespaceBinding(prefix: String, uri: String, parent: NamespaceBindin
2929
if (prefix == "")
3030
throw new IllegalArgumentException("zero length prefix not allowed")
3131

32-
def getURI(_prefix: String): String =
33-
if (prefix == _prefix) uri else parent.getURI(_prefix)
32+
def getURI(prefix: String): String =
33+
if (this.prefix == prefix) uri else parent.getURI(prefix)
3434

3535
/**
3636
* Returns some prefix that is mapped to the URI.
@@ -39,8 +39,8 @@ case class NamespaceBinding(prefix: String, uri: String, parent: NamespaceBindin
3939
* @return the prefix that is mapped to the input URI, or null
4040
* if no prefix is mapped to the URI.
4141
*/
42-
def getPrefix(_uri: String): String =
43-
if (_uri == uri) prefix else parent.getPrefix(_uri)
42+
def getPrefix(uri: String): String =
43+
if (uri == this.uri) prefix else parent.getPrefix(uri)
4444

4545
override def toString: String = Utility.sbToString(buildString(_, TopScope))
4646

@@ -72,9 +72,8 @@ case class NamespaceBinding(prefix: String, uri: String, parent: NamespaceBindin
7272

7373
def buildString(stop: NamespaceBinding): String = Utility.sbToString(buildString(_, stop))
7474

75-
def buildString(sb: StringBuilder, stop: NamespaceBinding): Unit = {
75+
def buildString(sb: StringBuilder, stop: NamespaceBinding): Unit =
7676
shadowRedefined(stop).doBuildString(sb, stop)
77-
}
7877

7978
private def doBuildString(sb: StringBuilder, stop: NamespaceBinding): Unit = {
8079
if (List(null, stop, TopScope).contains(this)) return

Diff for: shared/src/main/scala/scala/xml/Node.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ abstract class Node extends NodeSeq {
8282
* @return the namespace if `scope != null` and prefix was
8383
* found, else `null`
8484
*/
85-
def getNamespace(pre: String): String = if (scope.eq(null)) null else scope.getURI(pre)
85+
def getNamespace(pre: String): String = if (scope == null) null else scope.getURI(pre)
8686

8787
/**
8888
* Convenience method, looks up an unprefixed attribute in attributes of this node.
@@ -125,7 +125,7 @@ abstract class Node extends NodeSeq {
125125
/**
126126
* Children which do not stringify to "" (needed for equality)
127127
*/
128-
def nonEmptyChildren: Seq[Node] = child.filterNot(_.toString == "")
128+
def nonEmptyChildren: Seq[Node] = child.filterNot(_.toString.isEmpty)
129129

130130
/**
131131
* Descendant axis (all descendants of this node, not including node itself)

Diff for: shared/src/main/scala/scala/xml/NodeSeq.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
103103
val i: Int = that.indexOf('}')
104104
if (i == -1) fail
105105
val (uri: String, key: String) = (that.substring(2, i), that.substring(i + 1, that.length))
106-
if (uri == "" || key == "") fail
106+
if (uri.isEmpty || key.isEmpty) fail
107107
else y.attribute(uri, key)
108108
} else y.attribute(that.drop(1))
109109

Diff for: shared/src/main/scala/scala/xml/PrefixedAttribute.scala

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class PrefixedAttribute(
2828
override val pre: String,
2929
override val key: String,
3030
override val value: Seq[Node],
31-
val next1: MetaData)
32-
extends Attribute {
33-
override val next: MetaData = if (value.ne(null)) next1 else next1.remove(key)
31+
val next1: MetaData
32+
)
33+
extends Attribute
34+
{
35+
override val next: MetaData = if (value != null) next1 else next1.remove(key)
3436

3537
/** same as this(pre, key, Text(value), next), or no attribute if value is null */
3638
def this(pre: String, key: String, value: String, next: MetaData) =
37-
this(pre, key, if (value.ne(null)) Text(value) else null: NodeSeq, next)
39+
this(pre, key, if (value != null) Text(value) else null: NodeSeq, next)
3840

3941
/** same as this(pre, key, value.get, next), or no attribute if value is None */
4042
def this(pre: String, key: String, value: Option[Seq[Node]], next: MetaData) =
@@ -56,12 +58,11 @@ class PrefixedAttribute(
5658
/**
5759
* gets attribute value of qualified (prefixed) attribute with given key
5860
*/
59-
override def apply(namespace: String, scope: NamespaceBinding, key: String): Seq[Node] = {
61+
override def apply(namespace: String, scope: NamespaceBinding, key: String): Seq[Node] =
6062
if (key == this.key && scope.getURI(pre) == namespace)
6163
value
6264
else
6365
next(namespace, scope, key)
64-
}
6566
}
6667

6768
object PrefixedAttribute {

Diff for: shared/src/main/scala/scala/xml/PrettyPrinter.scala

+5-8
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean) {
150150

151151
protected def traverse(node: Node, pscope: NamespaceBinding, ind: Int): Unit = node match {
152152

153-
case Text(s) if s.trim == "" =>
153+
case Text(s) if s.trim.isEmpty =>
154154

155155
case _: Atom[_] | _: Comment | _: EntityRef | _: ProcInstr =>
156156
makeBox(ind, node.toString.trim)
@@ -163,18 +163,17 @@ class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean) {
163163
if (doPreserve(node)) sb.toString
164164
else TextBuffer.fromString(sb.toString).toText(0).data
165165
}
166-
if (childrenAreLeaves(node) && fits(test)) {
166+
if (childrenAreLeaves(node) && fits(test))
167167
makeBox(ind, test)
168-
} else {
168+
else {
169169
val ((stg: String, len2: Int), etg: String) =
170170
if (node.child.isEmpty && minimizeEmpty) {
171171
// force the tag to be self-closing
172172
val firstAttribute: Int = test.indexOf(' ')
173173
val firstBreak: Int = if (firstAttribute != -1) firstAttribute else test.lastIndexOf('/')
174174
((test, firstBreak), "")
175-
} else {
175+
} else
176176
(startTag(node, pscope), endTag(node))
177-
}
178177

179178
if (stg.length < width - cur) { // start tag fits
180179
makeBox(ind, stg)
@@ -221,9 +220,7 @@ class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean) {
221220
* @param n the node to be serialized
222221
* @param sb the stringbuffer to append to
223222
*/
224-
def format(n: Node, sb: StringBuilder): Unit = { // entry point
225-
format(n, TopScope, sb)
226-
}
223+
def format(n: Node, sb: StringBuilder): Unit = format(n, TopScope, sb) // entry point
227224

228225
def format(n: Node, pscope: NamespaceBinding, sb: StringBuilder): Unit = { // entry point
229226
var lastwasbreak: Boolean = false

Diff for: shared/src/main/scala/scala/xml/ProcInstr.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ case class ProcInstr(target: String, proctext: String) extends SpecialNode {
3939
* appends &quot;&lt;?&quot; target (&quot; &quot;+text)?+&quot;?&gt;&quot;
4040
* to this stringbuffer.
4141
*/
42-
override def buildString(sb: StringBuilder): StringBuilder = {
43-
val textStr: String = if (proctext == "") "" else s" $proctext"
44-
sb.append(s"<?$target$textStr?>")
45-
}
42+
override def buildString(sb: StringBuilder): StringBuilder =
43+
sb.append(s"<?$target${if (proctext.isEmpty) "" else s" $proctext"}?>")
4644
}

Diff for: shared/src/main/scala/scala/xml/TopScope.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ package xml
2020
*/
2121
object TopScope extends NamespaceBinding(null, null, null) {
2222

23-
import XML.{xml, namespace}
24-
2523
override def getURI(prefix1: String): String =
26-
if (prefix1 == xml) namespace else null
24+
if (prefix1 == XML.xml) XML.namespace else null
2725

2826
override def getPrefix(uri1: String): String =
29-
if (uri1 == namespace) xml else null
27+
if (uri1 == XML.namespace) XML.xml else null
3028

3129
override def toString: String = ""
3230

Diff for: shared/src/main/scala/scala/xml/UnprefixedAttribute.scala

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ import scala.collection.Seq
2424
class UnprefixedAttribute(
2525
override val key: String,
2626
override val value: Seq[Node],
27-
next1: MetaData)
28-
extends Attribute {
27+
next1: MetaData
28+
)
29+
extends Attribute
30+
{
2931
final override val pre: scala.Null = null
30-
override val next: MetaData = if (value.ne(null)) next1 else next1.remove(key)
32+
override val next: MetaData = if (value != null) next1 else next1.remove(key)
3133

3234
/** same as this(key, Text(value), next), or no attribute if value is null */
3335
def this(key: String, value: String, next: MetaData) =
34-
this(key, if (value.ne(null)) Text(value) else null: NodeSeq, next)
36+
this(key, if (value != null) Text(value) else null: NodeSeq, next)
3537

3638
/** same as this(key, value.get, next), or no attribute if value is None */
3739
def this(key: String, value: Option[Seq[Node]], next: MetaData) =

0 commit comments

Comments
 (0)