Skip to content

Commit

Permalink
8302815: Use new Math.clamp method in core libraries
Browse files Browse the repository at this point in the history
Reviewed-by: alanb
  • Loading branch information
amaembo committed Feb 22, 2023
1 parent 5e1d1b7 commit 3f3a1f5
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/io/ObjectInputStream.java
Expand Up @@ -308,8 +308,8 @@ protected Boolean computeValue(Class<?> type) {
* The maximum number of interfaces allowed for a proxy is limited to 65535 by
* {@link java.lang.reflect.Proxy#newProxyInstance(ClassLoader, Class[], InvocationHandler)}.
*/
static final int PROXY_INTERFACE_LIMIT = Math.max(0, Math.min(65535, GetIntegerAction
.privilegedGetProperty("jdk.serialProxyInterfaceLimit", 65535)));
static final int PROXY_INTERFACE_LIMIT = Math.clamp(GetIntegerAction
.privilegedGetProperty("jdk.serialProxyInterfaceLimit", 65535), 0, 65535);
}

/*
Expand Down
32 changes: 12 additions & 20 deletions src/java.base/share/classes/java/text/DecimalFormat.java
Expand Up @@ -3649,13 +3649,11 @@ else if (ch == percent) {
*/
@Override
public void setMaximumIntegerDigits(int newValue) {
maximumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
maximumIntegerDigits = Math.clamp(newValue, 0, MAXIMUM_INTEGER_DIGITS);
super.setMaximumIntegerDigits(Math.min(maximumIntegerDigits, DOUBLE_INTEGER_DIGITS));
if (minimumIntegerDigits > maximumIntegerDigits) {
minimumIntegerDigits = maximumIntegerDigits;
super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
super.setMinimumIntegerDigits(Math.min(minimumIntegerDigits, DOUBLE_INTEGER_DIGITS));
}
fastPathCheckNeeded = true;
}
Expand All @@ -3670,13 +3668,11 @@ public void setMaximumIntegerDigits(int newValue) {
*/
@Override
public void setMinimumIntegerDigits(int newValue) {
minimumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
minimumIntegerDigits = Math.clamp(newValue, 0, MAXIMUM_INTEGER_DIGITS);
super.setMinimumIntegerDigits(Math.min(minimumIntegerDigits, DOUBLE_INTEGER_DIGITS));
if (minimumIntegerDigits > maximumIntegerDigits) {
maximumIntegerDigits = minimumIntegerDigits;
super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
super.setMaximumIntegerDigits(Math.min(maximumIntegerDigits, DOUBLE_INTEGER_DIGITS));
}
fastPathCheckNeeded = true;
}
Expand All @@ -3691,13 +3687,11 @@ public void setMinimumIntegerDigits(int newValue) {
*/
@Override
public void setMaximumFractionDigits(int newValue) {
maximumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
maximumFractionDigits = Math.clamp(newValue, 0, MAXIMUM_FRACTION_DIGITS);
super.setMaximumFractionDigits(Math.min(maximumFractionDigits, DOUBLE_FRACTION_DIGITS));
if (minimumFractionDigits > maximumFractionDigits) {
minimumFractionDigits = maximumFractionDigits;
super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
super.setMinimumFractionDigits(Math.min(minimumFractionDigits, DOUBLE_FRACTION_DIGITS));
}
fastPathCheckNeeded = true;
}
Expand All @@ -3712,13 +3706,11 @@ public void setMaximumFractionDigits(int newValue) {
*/
@Override
public void setMinimumFractionDigits(int newValue) {
minimumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
minimumFractionDigits = Math.clamp(newValue, 0, MAXIMUM_FRACTION_DIGITS);
super.setMinimumFractionDigits(Math.min(minimumFractionDigits, DOUBLE_FRACTION_DIGITS));
if (minimumFractionDigits > maximumFractionDigits) {
maximumFractionDigits = minimumFractionDigits;
super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
super.setMaximumFractionDigits(Math.min(maximumFractionDigits, DOUBLE_FRACTION_DIGITS));
}
fastPathCheckNeeded = true;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, 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
Expand Down Expand Up @@ -3572,7 +3572,7 @@ public boolean format(DateTimePrintContext context, StringBuilder buf) {
}
}
} else {
int outputScale = Math.min(Math.max(fraction.scale(), minWidth), maxWidth);
int outputScale = Math.clamp(fraction.scale(), minWidth, maxWidth);
fraction = fraction.setScale(outputScale, RoundingMode.FLOOR);
if (decimalPoint) {
buf.append(decimalStyle.getDecimalSeparator());
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/HashMap.java
@@ -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
Expand Down Expand Up @@ -1523,7 +1523,7 @@ private void readObject(ObjectInputStream s)
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();
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/HashSet.java
@@ -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
Expand Down Expand Up @@ -322,7 +322,7 @@ private void readObject(java.io.ObjectInputStream s)
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();
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/Hashtable.java
@@ -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
Expand Down Expand Up @@ -1270,7 +1270,7 @@ void readHashtable(ObjectInputStream s)
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();
Expand Down
Expand Up @@ -2714,9 +2714,9 @@ public ForkJoinPool(int parallelism,
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));
Expand Down Expand Up @@ -2747,7 +2747,7 @@ private ForkJoinPool(byte forCommonPoolOnly) {
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
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/stream/SliceOps.java
@@ -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
Expand Down Expand Up @@ -50,7 +50,7 @@ private 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;
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, Institute of Software, Chinese Academy of Sciences.
* All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
Expand Down Expand Up @@ -154,7 +154,7 @@ public StorageCalculator(boolean forArguments) {
// Aggregates or scalars passed on the stack are aligned to the greater of
// the type alignment and XLEN bits, but never more than the stack alignment.
void alignStack(long alignment) {
alignment = Utils.alignUp(Math.min(Math.max(alignment, STACK_SLOT_SIZE), 16), STACK_SLOT_SIZE);
alignment = Utils.alignUp(Math.clamp(alignment, STACK_SLOT_SIZE, 16), STACK_SLOT_SIZE);
stackOffset = Utils.alignUp(stackOffset, alignment);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 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
Expand Down Expand Up @@ -1538,7 +1538,7 @@ public float floatValue() {
}
}
}
fValue = Math.max(Float.MIN_VALUE, Math.min(Float.MAX_VALUE, (float) dValue));
fValue = Math.clamp((float) dValue, Float.MIN_VALUE, Float.MAX_VALUE);

//
// fValue is now approximately the result.
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/unix/classes/sun/nio/fs/UnixUriUtils.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
Expand Down Expand Up @@ -162,8 +162,8 @@ private static long highMask(String chars) {
// between first and last, inclusive
private static long lowMask(char first, char last) {
long m = 0;
int f = Math.max(Math.min(first, 63), 0);
int l = Math.max(Math.min(last, 63), 0);
int f = Math.clamp(first, 0, 63);
int l = Math.clamp(last, 0, 63);
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
Expand All @@ -173,8 +173,8 @@ private static long lowMask(char first, char last) {
// between first and last, inclusive
private static long highMask(char first, char last) {
long m = 0;
int f = Math.max(Math.min(first, 127), 64) - 64;
int l = Math.max(Math.min(last, 127), 64) - 64;
int f = Math.clamp(first, 64, 127) - 64;
int l = Math.clamp(last, 64, 127) - 64;
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
Expand Down

1 comment on commit 3f3a1f5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.