2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.binding.data.codec.impl;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableMap;
15 import java.lang.reflect.InvocationHandler;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.lang.reflect.Proxy;
20 import java.util.Objects;
21 import java.util.concurrent.ConcurrentHashMap;
22 import org.opendaylight.yangtools.binding.data.codec.util.AugmentationReader;
23 import org.opendaylight.yangtools.yang.binding.Augmentable;
24 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 class LazyDataObject<D extends DataObject> implements InvocationHandler, AugmentationReader {
34 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObject.class);
35 private static final String GET_IMPLEMENTED_INTERFACE = "getImplementedInterface";
36 private static final String TO_STRING = "toString";
37 private static final String EQUALS = "equals";
38 private static final String GET_AUGMENTATION = "getAugmentation";
39 private static final String HASHCODE = "hashCode";
40 private static final Object NULL_VALUE = new Object();
42 private final ConcurrentHashMap<Method, Object> cachedData = new ConcurrentHashMap<>();
43 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data;
44 private final DataObjectCodecContext<D,?> context;
46 private volatile ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> cachedAugmentations = null;
47 private volatile Integer cachedHashcode = null;
49 @SuppressWarnings({ "rawtypes", "unchecked" })
50 LazyDataObject(final DataObjectCodecContext<D,?> ctx, final NormalizedNodeContainer data) {
51 this.context = Preconditions.checkNotNull(ctx, "Context must not be null");
52 this.data = Preconditions.checkNotNull(data, "Data must not be null");
56 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
57 if (method.getParameterTypes().length == 0) {
58 final String name = method.getName();
59 if (GET_IMPLEMENTED_INTERFACE.equals(name)) {
60 return context.getBindingClass();
61 } else if (TO_STRING.equals(name)) {
62 return bindingToString();
63 } else if (HASHCODE.equals(name)) {
64 return bindingHashCode();
66 return getBindingData(method);
67 } else if (GET_AUGMENTATION.equals(method.getName())) {
68 return getAugmentationImpl((Class<?>) args[0]);
69 } else if (EQUALS.equals(method.getName())) {
70 return bindingEquals(args[0]);
72 throw new UnsupportedOperationException("Unsupported method " + method);
75 private boolean bindingEquals(final Object other) {
79 if (!context.getBindingClass().isAssignableFrom(other.getClass())) {
83 for (final Method m : context.getHashCodeAndEqualsMethods()) {
84 final Object thisValue = getBindingData(m);
85 final Object otherValue = m.invoke(other);
86 if(!Objects.equals(thisValue, otherValue)) {
90 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
91 LOG.warn("Can not determine equality of {} and {}", this, other, e);
97 private Integer bindingHashCode() {
98 final Integer ret = cachedHashcode;
103 final int prime = 31;
105 for (final Method m : context.getHashCodeAndEqualsMethods()) {
106 final Object value = getBindingData(m);
107 result += prime * result + ((value == null) ? 0 : value.hashCode());
109 if (Augmentation.class.isAssignableFrom(context.getBindingClass())) {
110 result += prime * result + (getAugmentations(this).hashCode());
112 cachedHashcode = result;
116 private Object getBindingData(final Method method) {
117 Object cached = cachedData.get(method);
118 if (cached == null) {
119 final Object readedValue = context.getBindingChildValue(method, data);
120 if (readedValue == null) {
123 cached = readedValue;
125 cachedData.putIfAbsent(method, cached);
128 return cached == NULL_VALUE ? null : cached;
131 private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentationsImpl() {
132 ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> ret = cachedAugmentations;
134 synchronized (this) {
135 ret = cachedAugmentations;
137 ret = ImmutableMap.copyOf(context.getAllAugmentationsFrom(data));
138 cachedAugmentations = ret;
147 public Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object obj) {
148 Preconditions.checkArgument(this == Proxy.getInvocationHandler(obj),
149 "Supplied object is not associated with this proxy handler");
151 return getAugmentationsImpl();
154 private Object getAugmentationImpl(final Class<?> cls) {
155 final ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> aug = cachedAugmentations;
159 Preconditions.checkNotNull(cls,"Supplied augmentation must not be null.");
161 @SuppressWarnings({"unchecked","rawtypes"})
162 final Optional<DataContainerCodecContext<?,?>> augCtx= context.possibleStreamChild((Class) cls);
163 if(augCtx.isPresent()) {
164 final Optional<NormalizedNode<?, ?>> augData = data.getChild(augCtx.get().getDomPathArgument());
165 if (augData.isPresent()) {
166 return augCtx.get().deserialize(augData.get());
172 public String bindingToString() {
173 final ToStringHelper helper = MoreObjects.toStringHelper(context.getBindingClass()).omitNullValues();
175 for (final Method m :context.getHashCodeAndEqualsMethods()) {
176 helper.add(m.getName(), getBindingData(m));
178 if (Augmentable.class.isAssignableFrom(context.getBindingClass())) {
179 helper.add("augmentations", getAugmentationsImpl());
181 return helper.toString();
185 public int hashCode() {
186 final int prime = 31;
188 result = prime * result + context.hashCode();
189 result = prime * result + data.hashCode();
194 public boolean equals(final Object obj) {
201 if (getClass() != obj.getClass()) {
204 final LazyDataObject<?> other = (LazyDataObject<?>) obj;
205 if (context == null) {
206 if (other.context != null) {
209 } else if (!context.equals(other.context)) {
213 if (other.data != null) {
216 } else if (!data.equals(other.data)) {