8302815: Use new Math.clamp method in core libraries

Reviewed-by: alanb
This commit is contained in:
Tagir F. Valeev 2023-02-22 09:51:14 +00:00
parent 5e1d1b7940
commit 3f3a1f534b
11 changed files with 37 additions and 45 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, 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
@ -1523,7 +1523,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
if (lf <= 0 || Float.isNaN(lf))
throw new InvalidObjectException("Illegal load factor: " + lf);
lf = Math.min(Math.max(0.25f, lf), 4.0f);
lf = Math.clamp(lf, 0.25f, 4.0f);
HashMap.UnsafeHolder.putLoadFactor(this, lf);
reinitialize();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, 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
@ -322,7 +322,7 @@ public class HashSet<E>
loadFactor);
}
// Clamp load factor to range of 0.25...4.0.
loadFactor = Math.min(Math.max(0.25f, loadFactor), 4.0f);
loadFactor = Math.clamp(loadFactor, 0.25f, 4.0f);
// Read size and verify non-negative.
int size = s.readInt();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
@ -1270,7 +1270,7 @@ public class Hashtable<K,V>
float lf = fields.get("loadFactor", 0.75f);
if (lf <= 0 || Float.isNaN(lf))
throw new StreamCorruptedException("Illegal load factor: " + lf);
lf = Math.min(Math.max(0.25f, lf), 4.0f);
lf = Math.clamp(lf, 0.25f, 4.0f);
// Read the original length of the array and number of elements
int origlength = s.readInt();

View file

@ -2714,9 +2714,9 @@ public class ForkJoinPool extends AbstractExecutorService {
this.saturate = saturate;
this.config = asyncMode ? FIFO : 0;
this.keepAlive = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
int corep = Math.min(Math.max(corePoolSize, p), MAX_CAP);
int maxSpares = Math.max(0, Math.min(maximumPoolSize - p, MAX_CAP));
int minAvail = Math.max(0, Math.min(minimumRunnable, MAX_CAP));
int corep = Math.clamp(corePoolSize, p, MAX_CAP);
int maxSpares = Math.clamp(maximumPoolSize - p, 0, MAX_CAP);
int minAvail = Math.clamp(minimumRunnable, 0, MAX_CAP);
this.bounds = (long)(minAvail & SMASK) | (long)(maxSpares << SWIDTH) |
((long)corep << 32);
int size = 1 << (33 - Integer.numberOfLeadingZeros(p - 1));
@ -2747,7 +2747,7 @@ public class ForkJoinPool extends AbstractExecutorService {
String ms = System.getProperty
("java.util.concurrent.ForkJoinPool.common.maximumSpares");
if (ms != null)
maxSpares = Math.max(0, Math.min(MAX_CAP, Integer.parseInt(ms)));
maxSpares = Math.clamp(Integer.parseInt(ms), 0, MAX_CAP);
String sf = System.getProperty
("java.util.concurrent.ForkJoinPool.common.threadFactory");
String sh = System.getProperty

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, 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
@ -50,7 +50,7 @@ final class SliceOps {
* @return the sliced size
*/
private static long calcSize(long size, long skip, long limit) {
return size >= 0 ? Math.max(0, Math.min(size - skip, limit)) : -1;
return size >= 0 ? Math.clamp(size - skip, 0, limit) : -1;
}
/**