changeset 51703:4ffb0a33f265

8210347: Combine subsequent calls to Set.contains() and Set.add() Reviewed-by: smarks, bpb
author igerasim
date Tue, 11 Sep 2018 14:51:45 -0700
parents ebd5b1ad971a
children 2368e8e9cec6
files src/java.base/share/classes/java/lang/ModuleLayer.java src/java.base/share/classes/java/lang/module/Configuration.java src/java.base/share/classes/java/util/ServiceLoader.java src/java.base/share/classes/java/util/stream/DistinctOps.java
diffstat 4 files changed, 6 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/ModuleLayer.java	Tue Sep 11 13:54:34 2018 -0700
+++ b/src/java.base/share/classes/java/lang/ModuleLayer.java	Tue Sep 11 14:51:45 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -790,8 +790,7 @@
             // push in reverse order
             for (int i = layer.parents.size() - 1; i >= 0; i--) {
                 ModuleLayer parent = layer.parents.get(i);
-                if (!visited.contains(parent)) {
-                    visited.add(parent);
+                if (visited.add(parent)) {
                     stack.push(parent);
                 }
             }
--- a/src/java.base/share/classes/java/lang/module/Configuration.java	Tue Sep 11 13:54:34 2018 -0700
+++ b/src/java.base/share/classes/java/lang/module/Configuration.java	Tue Sep 11 14:51:45 2018 -0700
@@ -601,8 +601,7 @@
                 // push in reverse order
                 for (int i = layer.parents.size() - 1; i >= 0; i--) {
                     Configuration parent = layer.parents.get(i);
-                    if (!visited.contains(parent)) {
-                        visited.add(parent);
+                    if (visited.add(parent)) {
                         stack.push(parent);
                     }
                 }
--- a/src/java.base/share/classes/java/util/ServiceLoader.java	Tue Sep 11 13:54:34 2018 -0700
+++ b/src/java.base/share/classes/java/util/ServiceLoader.java	Tue Sep 11 14:51:45 2018 -0700
@@ -938,8 +938,7 @@
                     List<ModuleLayer> parents = layer.parents();
                     for (int i = parents.size() - 1; i >= 0; i--) {
                         ModuleLayer parent = parents.get(i);
-                        if (!visited.contains(parent)) {
-                            visited.add(parent);
+                        if (visited.add(parent)) {
                             stack.push(parent);
                         }
                     }
--- a/src/java.base/share/classes/java/util/stream/DistinctOps.java	Tue Sep 11 13:54:34 2018 -0700
+++ b/src/java.base/share/classes/java/util/stream/DistinctOps.java	Tue Sep 11 14:51:45 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -170,8 +170,7 @@
 
                         @Override
                         public void accept(T t) {
-                            if (!seen.contains(t)) {
-                                seen.add(t);
+                            if (seen.add(t)) {
                                 downstream.accept(t);
                             }
                         }