Skip to content

Commit

Permalink
Merge pull request eclipse-ee4j#2179 from kofemann/drop-NullaryFunction
Browse files Browse the repository at this point in the history
src: drop NullaryFunction in favor or java.util.Supplier
  • Loading branch information
arjantijms committed Sep 15, 2023
2 parents a80c366 + cb5fdde commit c795dda
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import java.util.function.Supplier;
import org.glassfish.grizzly.attributes.AttributeStorage;
import org.glassfish.grizzly.memory.MemoryManager;
import org.glassfish.grizzly.monitoring.MonitoringAware;
import org.glassfish.grizzly.monitoring.MonitoringConfig;
import org.glassfish.grizzly.utils.NullaryFunction;

/**
* Common interface, which represents any kind of connection.
Expand Down Expand Up @@ -152,7 +152,7 @@ public interface Connection<L> extends Readable<L>, Writeable<L>, Closeable, Att
*
* @return the {@link Processor} state associated with this <tt>Connection</tt>.
*/
<E> E obtainProcessorState(Processor processor, NullaryFunction<E> factory);
<E> E obtainProcessorState(Processor processor, Supplier<E> factory);

/**
* Executes the {@link Runnable} in the thread, responsible for running the given type of event on this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.glassfish.grizzly.attributes;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* Class used to define dynamic typed attributes on {@link AttributeHolder} instances. Storing attribute values in
Expand All @@ -38,7 +38,7 @@ public final class Attribute<T> {
/**
* Attribute initializer, which will be called, if attribute is not set.
*/
private final NullaryFunction<T> initializer;
private final Supplier<T> initializer;
/**
* Attribute index in AttributeBuilder
*/
Expand All @@ -50,16 +50,16 @@ public String toString() {
}

protected Attribute(final AttributeBuilder builder, final String name, final int index, final T defaultValue) {
this(builder, name, index, new NullaryFunction<T>() {
this(builder, name, index, new Supplier<T>() {

@Override
public T evaluate() {
public T get() {
return defaultValue;
}
});
}

protected Attribute(final AttributeBuilder builder, final String name, final int index, final NullaryFunction<T> initializer) {
protected Attribute(final AttributeBuilder builder, final String name, final int index, final Supplier<T> initializer) {
this.builder = builder;
this.name = name;
this.attributeIndex = index;
Expand All @@ -68,7 +68,7 @@ protected Attribute(final AttributeBuilder builder, final String name, final int

/**
* Get attribute value, stored on the {@link AttributeHolder}, the difference from
* {@link #get(org.glassfish.grizzly.attributes.AttributeHolder)} is that default value or {@link NullaryFunction} won't
* {@link #get(org.glassfish.grizzly.attributes.AttributeHolder)} is that default value or {@link Supplier} won't
* be invoked.
*
* @param attributeHolder {@link AttributeHolder}.
Expand All @@ -80,7 +80,7 @@ public T peek(final AttributeHolder attributeHolder) {

/**
* Get attribute value, stored on the {@link AttributeStorage}, the difference from {@link #get(AttributeStorage)} is
* that default value or {@link NullaryFunction} won't be invoked.
* that default value or {@link Supplier} won't be invoked.
*
* @param storage {@link AttributeStorage}.
* @return attribute value
Expand Down Expand Up @@ -212,7 +212,7 @@ public int index() {
}

@SuppressWarnings("unchecked")
private T get0(final AttributeHolder attributeHolder, final NullaryFunction<T> initializer) {
private T get0(final AttributeHolder attributeHolder, final Supplier<T> initializer) {
final IndexedAttributeAccessor indexedAccessor = attributeHolder.getIndexedAttributeAccessor();

return indexedAccessor != null ? (T) indexedAccessor.getAttribute(attributeIndex, initializer) : (T) attributeHolder.getAttribute(name, initializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.glassfish.grizzly.attributes;

import java.util.function.Supplier;

/**
* <tt>AttributeBuilder</tt> is responsible for creating and indexing {@link Attribute}s. For faster access to
* {@link Attribute} value, each {@link Attribute} has assigned index. <tt>AttributeBuilder</tt> is responsible to
Expand Down Expand Up @@ -61,29 +63,18 @@ public interface AttributeBuilder {
*/
<T> Attribute<T> createAttribute(String name, T defaultValue);

/**
* Create Attribute with name and initializer, which will be called, if Attribute's value is null on a AttributedObject
*
* @param <T> Type of attribute value
* @param name attribute name
* @param initializer NullaryFunction, which will be called, if Attribute's value is null on a AttributedObject
*
* @return Attribute<T>
*/
<T> Attribute<T> createAttribute(String name, org.glassfish.grizzly.utils.NullaryFunction<T> initializer);

/**
* Create Attribute with name and initializer, which will be called, if Attribute's value is null on a AttributedObject
*
* @param <T> Type of attribute value
* @param name attribute name
* @param initializer NullaryFunction, which will be called, if Attribute's value is null on a AttributedObject
* @param initializer {@link Supplier}, which will be called, if Attribute's value is null on a AttributedObject
*
* @return Attribute<T>
* @deprecated pls. use {@link #createAttribute(java.lang.String, org.glassfish.grizzly.utils.NullaryFunction)}.
*/
@Deprecated
<T> Attribute<T> createAttribute(String name, NullaryFunction<T> initializer);
<T> Attribute<T> createAttribute(String name, Supplier<T> initializer);

/**
* Creates and returns new thread-safe {@link AttributeHolder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Set;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* Interface declares common functionality for objects, which have associated {@link Attribute}s.
Expand Down Expand Up @@ -62,7 +62,7 @@ public interface AttributeHolder {
*
* @since 2.3.18
*/
Object getAttribute(String name, NullaryFunction initializer);
Object getAttribute(String name, Supplier initializer);

/**
* Return a {@link Set} of attribute names.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.List;
import java.util.Map;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* Default {@link AttributeBuilder} implementation.
Expand Down Expand Up @@ -63,7 +63,7 @@ public synchronized <T> Attribute<T> createAttribute(final String name, final T
*/
@Override
@SuppressWarnings("unchecked")
public synchronized <T> Attribute<T> createAttribute(final String name, final NullaryFunction<T> initializer) {
public synchronized <T> Attribute<T> createAttribute(final String name, final Supplier<T> initializer) {
Attribute<T> attribute = name2Attribute.get(name);
if (attribute == null) {
attribute = new Attribute<>(this, name, attributes.size(), initializer);
Expand All @@ -74,17 +74,6 @@ public synchronized <T> Attribute<T> createAttribute(final String name, final Nu
return attribute;
}

@Override
public <T> Attribute<T> createAttribute(final String name, final org.glassfish.grizzly.attributes.NullaryFunction<T> initializer) {
return createAttribute(name, initializer == null ? null : new NullaryFunction<T>() {

@Override
public T evaluate() {
return initializer.evaluate();
}
});
}

@Override
public AttributeHolder createSafeAttributeHolder() {
return new IndexedAttributeHolder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.glassfish.grizzly.attributes;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* The interface declares, that {@link AttributeHolder} supports indexed {@link Attribute} access.
Expand All @@ -38,11 +38,11 @@ public interface IndexedAttributeAccessor {
* index is not set, set it to the default value, using the <tt>initializer</tt>, and return the default.
*
* @param index the attribute index
* @param initializer the default value {@link NullaryFunction}
* @param initializer the default value {@link Supplier}
* @return the value of the attribute by index
* @since 2.3.18
*/
Object getAttribute(int index, NullaryFunction initializer);
Object getAttribute(int index, Supplier initializer);

/**
* Internal method for dynamic attribute support. Set the attribute with the index to value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.HashSet;
import java.util.Set;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* {@link AttributeHolder}, which supports indexed access to stored {@link Attribute}s. Access to such indexed
Expand Down Expand Up @@ -69,7 +69,7 @@ public Object getAttribute(final String name) {
* {@inheritDoc}
*/
@Override
public Object getAttribute(final String name, final NullaryFunction initializer) {
public Object getAttribute(final String name, final Supplier initializer) {
final Attribute attribute = attributeBuilder.getAttributeByName(name);
if (attribute != null) {
return indexedAttributeAccessor.getAttribute(attribute.index(), initializer);
Expand Down Expand Up @@ -293,17 +293,17 @@ public Object getAttribute(final int index) {
* {@inheritDoc}
*/
@Override
public Object getAttribute(final int index, final NullaryFunction initializer) {
public Object getAttribute(final int index, final Supplier initializer) {
Object value = weakGet(index);

if (value == null && initializer != null) {
synchronized (sync) {
// we want to make sure that parallel getAttribute(int, NullaryFunction)
// won't create multiple value instances (everyone will call NullaryFunction.evaluate())
// we want to make sure that parallel getAttribute(int, Suppler)
// won't create multiple value instances (everyone will call Supplier.get())
value = weakGet(index);

if (value == null) {
value = initializer.evaluate();
value = initializer.get();
setAttribute(index, value);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Map;
import java.util.Set;

import org.glassfish.grizzly.utils.NullaryFunction;
import java.util.function.Supplier;

/**
* A non thread-safe {@link AttributeHolder} implementation.
Expand Down Expand Up @@ -57,7 +57,7 @@ public Object getAttribute(final String name) {
}

@Override
public Object getAttribute(final String name, final NullaryFunction initializer) {
public Object getAttribute(final String name, final Supplier initializer) {

if (!isSet && initializer == null) {
return null;
Expand All @@ -68,7 +68,7 @@ public Object getAttribute(final String name, final NullaryFunction initializer)
return indexedAttributeAccessor.getAttribute(attribute, initializer);
}

return initializer != null ? initializer.evaluate() : null;
return initializer != null ? initializer.get() : null;
}

@Override
Expand Down Expand Up @@ -238,7 +238,7 @@ public Object getAttribute(final int index) {
}

@Override
public Object getAttribute(final int index, final NullaryFunction initializer) {
public Object getAttribute(final int index, final Supplier initializer) {
if (!isSet && initializer == null) {
return null;
}
Expand All @@ -256,14 +256,14 @@ public Object removeAttribute(final int index) {
return removeAttribute(attributeBuilder.getAttributeByIndex(index));
}

private Object getAttribute(final Attribute attribute, final NullaryFunction initializer) {
private Object getAttribute(final Attribute attribute, final Supplier initializer) {
final int idx = attribute.index();

final Holder h = holderByIdx(idx);

if (h != null) {
if (h.value == null && initializer != null) {
h.value = initializer.evaluate();
h.value = initializer.get();
}

return h.value;
Expand All @@ -272,7 +272,7 @@ private Object getAttribute(final Attribute attribute, final NullaryFunction ini
Object value = valueMap != null ? MapperAccessor.getValue(UnsafeAttributeHolder.this, idx) : null;

if (value == null && initializer != null) {
value = initializer.evaluate();
value = initializer.get();
setAttribute(attribute, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -44,7 +45,6 @@
import org.glassfish.grizzly.memory.Buffers;
import org.glassfish.grizzly.utils.Exceptions;
import org.glassfish.grizzly.utils.Futures;
import org.glassfish.grizzly.utils.NullaryFunction;

/**
* Default {@link FilterChain} implementation
Expand Down Expand Up @@ -637,10 +637,10 @@ private Object append(final Object currentMessage) {
}
}

private final class FiltersStateFactory implements NullaryFunction<FiltersState> {
private final class FiltersStateFactory implements Supplier<FiltersState> {

@Override
public FiltersState evaluate() {
public FiltersState get() {
return new FiltersState(size());
}
}
Expand Down
Loading

0 comments on commit c795dda

Please sign in to comment.