From 6f0b2ecf8441d060dcf382adf0492a47d8e7d0a2 Mon Sep 17 00:00:00 2001 From: John Blum Date: Sun, 26 Apr 2020 15:30:42 -0700 Subject: [PATCH] Introduce InvocationArguments type to capture the arguments passed to a method invocation's parameters. --- .../util/function/InvocationArguments.java | 84 ++++++++++++++++ .../InvocationArgumentsUnitTests.java | 97 +++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 apache-geode-extensions/src/main/java/org/springframework/geode/util/function/InvocationArguments.java create mode 100644 apache-geode-extensions/src/test/java/org/springframework/geode/util/function/InvocationArgumentsUnitTests.java diff --git a/apache-geode-extensions/src/main/java/org/springframework/geode/util/function/InvocationArguments.java b/apache-geode-extensions/src/main/java/org/springframework/geode/util/function/InvocationArguments.java new file mode 100644 index 00000000..e5fdbec9 --- /dev/null +++ b/apache-geode-extensions/src/main/java/org/springframework/geode/util/function/InvocationArguments.java @@ -0,0 +1,84 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://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.springframework.geode.util.function; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Iterator; + +/** + * {@link Iterable} of {@link Method} invocation {@link Object arguments}. + * + * @author John Blum + * @see java.lang.Iterable + * @since 1.3.0 + */ +public class InvocationArguments implements Iterable { + + public static InvocationArguments from(Object... arguments) { + return new InvocationArguments(arguments); + } + + private final Object[] arguments; + + /** + * Constructs a new instance of {@link InvocationArguments} initialized with the given array of + * {@link Object arguments}. + * + * @param arguments array of {@link Object arguments} indicating the values passed to the {@link Method} invocation + * parameters; may be {@literal null}. + */ + public InvocationArguments(Object[] arguments) { + this.arguments = arguments != null ? arguments : new Object[0]; + } + + protected Object[] getArguments() { + return this.arguments; + } + + @SuppressWarnings("unchecked") + protected T getArgumentAt(int index) { + return (T) getArguments()[index]; + } + + @Override + public Iterator iterator() { + + return new Iterator() { + + int index = 0; + + @Override + public boolean hasNext() { + return this.index < InvocationArguments.this.getArguments().length; + } + + @Override + public Object next() { + return InvocationArguments.this.getArguments()[this.index++]; + } + }; + } + + public int size() { + return this.arguments.length; + } + + @Override + public String toString() { + return Arrays.toString(getArguments()); + } +} diff --git a/apache-geode-extensions/src/test/java/org/springframework/geode/util/function/InvocationArgumentsUnitTests.java b/apache-geode-extensions/src/test/java/org/springframework/geode/util/function/InvocationArgumentsUnitTests.java new file mode 100644 index 00000000..7e93e3f7 --- /dev/null +++ b/apache-geode-extensions/src/test/java/org/springframework/geode/util/function/InvocationArgumentsUnitTests.java @@ -0,0 +1,97 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://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.springframework.geode.util.function; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; + +import org.junit.Test; + +/** + * Unit Tests for {@link InvocationArguments). + * + * @author John Blum + * @see org.junit.Test + * @see org.springframework.geode.util.function.InvocationArguments + * @since 1.3.0 + */ +public class InvocationArgumentsUnitTests { + + @Test + public void constructsInvocationArgumentsWithArguments() { + + Object[] arguments = { true, 'c', 1, Math.PI, "test" }; + + InvocationArguments invocationArguments = new InvocationArguments(arguments); + + assertThat(invocationArguments).isNotNull(); + assertThat(invocationArguments.size()).isEqualTo(arguments.length); + assertThat(Arrays.equals(invocationArguments.getArguments(), arguments)).isTrue(); + assertThat(invocationArguments.getArgumentAt(0)).isEqualTo(true); + assertThat(invocationArguments.getArgumentAt(1)).isEqualTo('c'); + assertThat(invocationArguments.getArgumentAt(2)).isEqualTo(1); + assertThat(invocationArguments.getArgumentAt(3)).isEqualTo(Math.PI); + assertThat(invocationArguments.getArgumentAt(4)).isEqualTo("test"); + } + + @Test + public void fromConstructsNewInvocationArguments() { + + InvocationArguments arguments = InvocationArguments.from("test", 1, false); + + assertThat(arguments).isNotNull(); + assertThat(arguments).hasSize(3); + assertThat(arguments.size()).isEqualTo(3); + assertThat(arguments).containsExactly("test", 1, false); + assertThat(arguments.getArgumentAt(0)).isEqualTo("test"); + assertThat(arguments.getArgumentAt(1)).isEqualTo(1); + assertThat(arguments.getArgumentAt(2)).isEqualTo(false); + } + + @Test + public void constructInvocationArgumentsWithNull() { + + InvocationArguments arguments = new InvocationArguments(null); + + assertThat(arguments).isNotNull(); + assertThat(arguments).hasSize(0); + assertThat(arguments.getArguments()).isNotNull(); + } + + @Test + public void iteratesArguments() { + + Object[] arguments = { true, 1, "test" }; + + InvocationArguments invocationArguments = new InvocationArguments(arguments); + + int index = 0; + + for (Object argument : invocationArguments) { + assertThat(argument).isEqualTo(arguments[index++]); + } + } + + @Test + public void toStringIsCorrect() { + + InvocationArguments arguments = InvocationArguments.from("one", "two", "three"); + + assertThat(arguments).isNotNull(); + assertThat(arguments.toString()).isEqualTo("[one, two, three]"); + } +}