This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit b859078cfbd20a4fc76c897c5280834584307cdb Author: Matt Sicker <[email protected]> AuthorDate: Sun May 22 13:46:43 2022 -0500 Add lazy containers for int and boolean This avoids boxing and null sentinels for lazy primitives. Signed-off-by: Matt Sicker <[email protected]> --- .../org/apache/logging/log4j/util/LazyBoolean.java | 56 ++++++++++++++++++++++ .../org/apache/logging/log4j/util/LazyInt.java | 52 ++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyBoolean.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyBoolean.java new file mode 100644 index 0000000000..f41f5cfd5e --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyBoolean.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.logging.log4j.util; + +import java.util.function.BooleanSupplier; + +public class LazyBoolean implements BooleanSupplier { + private final BooleanSupplier supplier; + private volatile boolean initialized; + private volatile boolean value; + + public LazyBoolean(final BooleanSupplier supplier) { + this.supplier = supplier; + } + + @Override + public boolean getAsBoolean() { + boolean uninitialized = !initialized; + boolean value = this.value; + if (uninitialized) { + synchronized (this) { + uninitialized = !initialized; + if (uninitialized) { + this.value = value = supplier.getAsBoolean(); + initialized = true; + } + } + } + return value; + } + + public synchronized void setAsBoolean(final boolean b) { + initialized = false; + value = b; + initialized = true; + } + + public void reset() { + initialized = false; + } +} diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyInt.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyInt.java new file mode 100644 index 0000000000..5b4d2fe6ca --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LazyInt.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.logging.log4j.util; + +import java.util.function.IntSupplier; + +public class LazyInt implements IntSupplier { + private final IntSupplier supplier; + private volatile boolean initialized; + private volatile int value; + + public LazyInt(final IntSupplier supplier) { + this.supplier = supplier; + } + + @Override + public int getAsInt() { + boolean uninitialized = !initialized; + int value = this.value; + if (uninitialized) { + synchronized (this) { + uninitialized = !initialized; + if (uninitialized) { + this.value = value = supplier.getAsInt(); + initialized = true; + } + } + } + return value; + } + + public synchronized void setAsInt(final int i) { + initialized = false; + value = i; + initialized = true; + } +}
