Home / Class/ DefaultAttributeMap Class — netty Architecture

DefaultAttributeMap Class — netty Architecture

Architecture documentation for the DefaultAttributeMap class in DefaultAttributeMap.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f["DefaultAttributeMap"]
  a739a013_f627_e3c4_7939_617f92c79d55["DefaultAttributeMap.java"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|defined in| a739a013_f627_e3c4_7939_617f92c79d55
  29dc4ae3_4501_cdff_abe5_6eb56b31bc3b["searchAttributeByKey()"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|method| 29dc4ae3_4501_cdff_abe5_6eb56b31bc3b
  06e2906f_6470_6af3_c0b4_8b62aa0c0e87["orderedCopyOnInsert()"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|method| 06e2906f_6470_6af3_c0b4_8b62aa0c0e87
  c7821402_c1b3_d927_bcbd_43fa921be527["attr()"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|method| c7821402_c1b3_d927_bcbd_43fa921be527
  687147c8_8840_2da9_5463_2c0c3edffbdf["hasAttr()"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|method| 687147c8_8840_2da9_5463_2c0c3edffbdf
  36be273e_4a26_05a2_5f0e_7cf9398c207b["removeAttributeIfMatch()"]
  bbb6a8bc_5354_cb1a_513e_c44f549e2d8f -->|method| 36be273e_4a26_05a2_5f0e_7cf9398c207b

Relationship Graph

Source Code

common/src/main/java/io/netty/util/DefaultAttributeMap.java lines 29–213

public class DefaultAttributeMap implements AttributeMap {

    private static final AtomicReferenceFieldUpdater<DefaultAttributeMap, DefaultAttribute[]> ATTRIBUTES_UPDATER =
            AtomicReferenceFieldUpdater.newUpdater(DefaultAttributeMap.class, DefaultAttribute[].class, "attributes");
    private static final DefaultAttribute[] EMPTY_ATTRIBUTES = new DefaultAttribute[0];

    /**
     * Similarly to {@code Arrays::binarySearch} it perform a binary search optimized for this use case, in order to
     * save polymorphic calls (on comparator side) and unnecessary class checks.
     */
    private static int searchAttributeByKey(DefaultAttribute[] sortedAttributes, AttributeKey<?> key) {
        int low = 0;
        int high = sortedAttributes.length - 1;

        while (low <= high) {
            int mid = low + high >>> 1;
            DefaultAttribute midVal = sortedAttributes[mid];
            AttributeKey midValKey = midVal.key;
            if (midValKey == key) {
                return mid;
            }
            int midValKeyId = midValKey.id();
            int keyId = key.id();
            assert midValKeyId != keyId;
            boolean searchRight = midValKeyId < keyId;
            if (searchRight) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return -(low + 1);
    }

    private static void orderedCopyOnInsert(DefaultAttribute[] sortedSrc, int srcLength, DefaultAttribute[] copy,
                                            DefaultAttribute toInsert) {
        // let's walk backward, because as a rule of thumb, toInsert.key.id() tends to be higher for new keys
        final int id = toInsert.key.id();
        int i;
        for (i = srcLength - 1; i >= 0; i--) {
            DefaultAttribute attribute = sortedSrc[i];
            assert attribute.key.id() != id;
            if (attribute.key.id() < id) {
                break;
            }
            copy[i + 1] = sortedSrc[i];
        }
        copy[i + 1] = toInsert;
        final int toCopy = i + 1;
        if (toCopy > 0) {
            System.arraycopy(sortedSrc, 0, copy, 0, toCopy);
        }
    }

    private volatile DefaultAttribute[] attributes = EMPTY_ATTRIBUTES;

    @SuppressWarnings("unchecked")
    @Override
    public <T> Attribute<T> attr(AttributeKey<T> key) {
        ObjectUtil.checkNotNull(key, "key");
        DefaultAttribute newAttribute = null;
        for (;;) {
            final DefaultAttribute[] attributes = this.attributes;
            final int index = searchAttributeByKey(attributes, key);
            final DefaultAttribute[] newAttributes;
            if (index >= 0) {
                final DefaultAttribute attribute = attributes[index];
                assert attribute.key() == key;
                if (!attribute.isRemoved()) {
                    return attribute;
                }
                // let's try replace the removed attribute with a new one
                if (newAttribute == null) {
                    newAttribute = new DefaultAttribute<T>(this, key);
                }
                final int count = attributes.length;
                newAttributes = Arrays.copyOf(attributes, count);
                newAttributes[index] = newAttribute;
            } else {
                if (newAttribute == null) {

Frequently Asked Questions

What is the DefaultAttributeMap class?
DefaultAttributeMap is a class in the netty codebase, defined in common/src/main/java/io/netty/util/DefaultAttributeMap.java.
Where is DefaultAttributeMap defined?
DefaultAttributeMap is defined in common/src/main/java/io/netty/util/DefaultAttributeMap.java at line 29.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free