Introduce computeAttribute() in AttributeAccessor
This commit introduces computeAttribute() as an interface default method in the AttributeAccessor API. This serves as a convenience analogous to the computeIfAbsent() method in java.util.Map. Closes gh-26281
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,39 +17,61 @@
|
||||
package org.springframework.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AttributeAccessorSupport}.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
class AttributeAccessorSupportTests {
|
||||
|
||||
private static final String NAME = "foo";
|
||||
private static final String NAME = "name";
|
||||
|
||||
private static final String VALUE = "bar";
|
||||
private static final String VALUE = "value";
|
||||
|
||||
private final AttributeAccessor attributeAccessor = new SimpleAttributeAccessorSupport();
|
||||
|
||||
private AttributeAccessor attributeAccessor = new SimpleAttributeAccessorSupport();
|
||||
|
||||
@Test
|
||||
void setAndGet() throws Exception {
|
||||
void setAndGet() {
|
||||
this.attributeAccessor.setAttribute(NAME, VALUE);
|
||||
assertThat(this.attributeAccessor.getAttribute(NAME)).isEqualTo(VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setAndHas() throws Exception {
|
||||
void setAndHas() {
|
||||
assertThat(this.attributeAccessor.hasAttribute(NAME)).isFalse();
|
||||
this.attributeAccessor.setAttribute(NAME, VALUE);
|
||||
assertThat(this.attributeAccessor.hasAttribute(NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() throws Exception {
|
||||
void computeAttribute() {
|
||||
AtomicInteger atomicInteger = new AtomicInteger();
|
||||
Function<String, String> computeFunction = name -> "computed-" + atomicInteger.incrementAndGet();
|
||||
|
||||
assertThat(this.attributeAccessor.hasAttribute(NAME)).isFalse();
|
||||
this.attributeAccessor.computeAttribute(NAME, computeFunction);
|
||||
assertThat(this.attributeAccessor.getAttribute(NAME)).isEqualTo("computed-1");
|
||||
this.attributeAccessor.computeAttribute(NAME, computeFunction);
|
||||
assertThat(this.attributeAccessor.getAttribute(NAME)).isEqualTo("computed-1");
|
||||
|
||||
this.attributeAccessor.removeAttribute(NAME);
|
||||
assertThat(this.attributeAccessor.hasAttribute(NAME)).isFalse();
|
||||
this.attributeAccessor.computeAttribute(NAME, computeFunction);
|
||||
assertThat(this.attributeAccessor.getAttribute(NAME)).isEqualTo("computed-2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
assertThat(this.attributeAccessor.hasAttribute(NAME)).isFalse();
|
||||
this.attributeAccessor.setAttribute(NAME, VALUE);
|
||||
assertThat(this.attributeAccessor.removeAttribute(NAME)).isEqualTo(VALUE);
|
||||
@@ -57,13 +79,13 @@ class AttributeAccessorSupportTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void attributeNames() throws Exception {
|
||||
void attributeNames() {
|
||||
this.attributeAccessor.setAttribute(NAME, VALUE);
|
||||
this.attributeAccessor.setAttribute("abc", "123");
|
||||
String[] attributeNames = this.attributeAccessor.attributeNames();
|
||||
Arrays.sort(attributeNames);
|
||||
assertThat(Arrays.binarySearch(attributeNames, NAME) > -1).isTrue();
|
||||
assertThat(Arrays.binarySearch(attributeNames, "abc") > -1).isTrue();
|
||||
assertThat(Arrays.binarySearch(attributeNames, "abc")).isEqualTo(0);
|
||||
assertThat(Arrays.binarySearch(attributeNames, NAME)).isEqualTo(1);
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
|
||||
Reference in New Issue
Block a user