import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME;
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_GET_IMPLEMENTED_INTERFACE_NAME;
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.dom.codec.util.AugmentationReader;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.Augmentable;
private static final String EQUALS = "equals";
private static final String HASHCODE = "hashCode";
private static final String AUGMENTATIONS = "augmentations";
- private static final Object NULL_VALUE = new Object();
+ private static final @NonNull Object NULL_VALUE = new Object();
- private final ConcurrentHashMap<Method, Object> cachedData = new ConcurrentHashMap<>();
+ // Method.getName() is guaranteed to be interned and all getter methods have zero arguments, name is sufficient to
+ // identify the data, skipping Method.hashCode() computation.
+ private final ConcurrentHashMap<String, Object> cachedData = new ConcurrentHashMap<>();
private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data;
private final DataObjectCodecContext<D,?> context;
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) {
- if (method.getParameterTypes().length == 0) {
- final String name = method.getName();
- if (DATA_CONTAINER_GET_IMPLEMENTED_INTERFACE_NAME.equals(name)) {
- return context.getBindingClass();
- } else if (TO_STRING.equals(name)) {
- return bindingToString();
- } else if (HASHCODE.equals(name)) {
- return bindingHashCode();
- } else if (AUGMENTATIONS.equals(name)) {
- return getAugmentationsImpl();
- }
- return getBindingData(method);
- } else if (AUGMENTABLE_AUGMENTATION_NAME.equals(method.getName())) {
- return getAugmentationImpl((Class<?>) args[0]);
- } else if (EQUALS.equals(method.getName())) {
- return bindingEquals(args[0]);
+ switch (method.getParameterCount()) {
+ case 0:
+ final String methodName = method.getName();
+ switch (methodName) {
+ case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME:
+ return context.getBindingClass();
+ case TO_STRING:
+ return bindingToString();
+ case HASHCODE:
+ return bindingHashCode();
+ case AUGMENTATIONS:
+ return getAugmentationsImpl();
+ default:
+ return method.isDefault() ? nonnullBindingData(methodName) : getBindingData(methodName);
+ }
+ case 1:
+ switch (method.getName()) {
+ case AUGMENTABLE_AUGMENTATION_NAME:
+ return getAugmentationImpl((Class<?>) args[0]);
+ case EQUALS:
+ return bindingEquals(args[0]);
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
}
+
throw new UnsupportedOperationException("Unsupported method " + method);
}
return false;
}
try {
- for (final Method m : context.getHashCodeAndEqualsMethods()) {
- final Object thisValue = getBindingData(m);
+ for (final Method m : context.propertyMethods()) {
+ final Object thisValue = getBindingData(m.getName());
final Object otherValue = m.invoke(other);
/*
- * added for valid byte array comparison, when list key type is binary
- * deepEquals is not used since it does excessive amount of instanceof calls.
- */
+ * added for valid byte array comparison, when list key type is binary
+ * deepEquals is not used since it does excessive amount of instanceof calls.
+ */
if (thisValue instanceof byte[] && otherValue instanceof byte[]) {
if (!Arrays.equals((byte[]) thisValue, (byte[]) otherValue)) {
return false;
}
private Integer bindingHashCode() {
- final Integer ret = cachedHashcode;
- if (ret != null) {
- return ret;
+ final Integer cached = cachedHashcode;
+ if (cached != null) {
+ return cached;
}
final int prime = 31;
int result = 1;
- for (final Method m : context.getHashCodeAndEqualsMethods()) {
- final Object value = getBindingData(m);
+ for (final Method m : context.propertyMethods()) {
+ final Object value = getBindingData(m.getName());
result = prime * result + Objects.hashCode(value);
}
if (Augmentable.class.isAssignableFrom(context.getBindingClass())) {
result = prime * result + getAugmentationsImpl().hashCode();
}
- cachedHashcode = result;
- return result;
+ final Integer ret = result;
+ cachedHashcode = ret;
+ return ret;
}
- private Object getBindingData(final Method method) {
- Object cached = cachedData.get(method);
- if (cached == null) {
- final Object readedValue = context.getBindingChildValue(method, data);
- cached = readedValue == null ? NULL_VALUE : readedValue;
+ private Object nonnullBindingData(final String methodName) {
+ final Object value = getBindingData(context.getterNameForNonnullName(methodName));
+ return value != null ? value : ImmutableList.of();
+ }
- final Object raced = cachedData.putIfAbsent(method, cached);
- if (raced != null) {
- // Load/store raced, we should return the stored value
- cached = raced;
- }
+ // Internal invocation, can only target getFoo() methods
+ private Object getBindingData(final String methodName) {
+ final Object cached = cachedData.get(methodName);
+ if (cached != null) {
+ return unmaskNull(cached);
}
- return cached == NULL_VALUE ? null : cached;
+ final Object value = context.getBindingChildValue(methodName, data);
+ final Object raced = cachedData.putIfAbsent(methodName, value == null ? NULL_VALUE : value);
+ // If we raced we need to return previously-stored value
+ return raced != null ? unmaskNull(raced) : value;
+ }
+
+ private static Object unmaskNull(final @NonNull Object masked) {
+ return masked == NULL_VALUE ? null : masked;
}
private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentationsImpl() {
final Class<D> bindingClass = context.getBindingClass();
final ToStringHelper helper = MoreObjects.toStringHelper(bindingClass).omitNullValues();
- for (final Method m : context.getHashCodeAndEqualsMethods()) {
- helper.add(m.getName(), getBindingData(m));
+ for (final Method m : context.propertyMethods()) {
+ final String methodName = m.getName();
+ helper.add(methodName, getBindingData(methodName));
}
if (Augmentable.class.isAssignableFrom(bindingClass)) {
helper.add("augmentations", getAugmentationsImpl());