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.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 class LazyDataObject implements InvocationHandler, AugmentationReader {
33 private static final Logger LOG = LoggerFactory.getLogger(LazyDataObject.class);
34 private static final String GET_IMPLEMENTED_INTERFACE = "getImplementedInterface";
35 private static final String TO_STRING = "toString";
36 private static final String EQUALS = "equals";
37 private static final String GET_AUGMENTATION = "getAugmentation";
38 private static final String HASHCODE = "hashCode";
39 private static final Object NULL_VALUE = new Object();
41 private final ConcurrentHashMap<Method, Object> cachedData = new ConcurrentHashMap<>();
42 private final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data;
43 private final DataObjectCodecContext<?> context;
45 private volatile ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> cachedAugmentations = null;
46 private volatile Integer cachedHashcode = null;
48 @SuppressWarnings({ "rawtypes", "unchecked" })
49 LazyDataObject(final DataObjectCodecContext<?> ctx, final NormalizedNodeContainer data) {
50 this.context = Preconditions.checkNotNull(ctx, "Context must not be null");
51 this.data = Preconditions.checkNotNull(data, "Data must not be null");
55 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
56 if (method.getParameterTypes().length == 0) {
57 final String name = method.getName();
58 if (GET_IMPLEMENTED_INTERFACE.equals(name)) {
59 return context.bindingClass();
60 } else if (TO_STRING.equals(name)) {
61 return bindingToString();
62 } else if (HASHCODE.equals(name)) {
63 return bindingHashCode();
65 return getBindingData(method);
66 } else if (GET_AUGMENTATION.equals(method.getName())) {
67 return getAugmentationImpl((Class<?>) args[0]);
68 } else if (EQUALS.equals(method.getName())) {
69 return bindingEquals(args[0]);
71 throw new UnsupportedOperationException("Unsupported method " + method);
74 private boolean bindingEquals(final Object other) {
78 if (!context.bindingClass().isAssignableFrom(other.getClass())) {
82 for (final Method m : context.getHashCodeAndEqualsMethods()) {
83 final Object thisValue = getBindingData(m);
84 final Object otherValue = m.invoke(other);
85 if(!Objects.equals(thisValue, otherValue)) {
89 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
90 LOG.warn("Can not determine equality of {} and {}", this, other, e);
96 private Integer bindingHashCode() {
97 final Integer ret = cachedHashcode;
102 final int prime = 31;
104 for (final Method m : context.getHashCodeAndEqualsMethods()) {
105 final Object value = getBindingData(m);
106 result += prime * result + ((value == null) ? 0 : value.hashCode());
108 if (Augmentation.class.isAssignableFrom(context.bindingClass())) {
109 result += prime * result + (getAugmentations(this).hashCode());
111 cachedHashcode = result;
115 private Object getBindingData(final Method method) {
116 Object cached = cachedData.get(method);
117 if (cached == null) {
118 final Object readedValue = context.getBindingChildValue(method, data);
119 if (readedValue == null) {
122 cached = readedValue;
124 cachedData.putIfAbsent(method, cached);
127 return cached == NULL_VALUE ? null : cached;
130 private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentationsImpl() {
131 ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> ret = cachedAugmentations;
133 synchronized (this) {
134 ret = cachedAugmentations;
136 ret = ImmutableMap.copyOf(context.getAllAugmentationsFrom(data));
137 cachedAugmentations = ret;
146 public Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object obj) {
147 Preconditions.checkArgument(this == Proxy.getInvocationHandler(obj),
148 "Supplied object is not associated with this proxy handler");
150 return getAugmentationsImpl();
153 private Object getAugmentationImpl(final Class<?> cls) {
154 final ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> aug = cachedAugmentations;
158 Preconditions.checkNotNull(cls,"Supplied augmentation must not be null.");
159 final Optional<DataContainerCodecContext<?>> augCtx= context.getPossibleStreamChild(cls);
160 if(augCtx.isPresent()) {
161 final Optional<NormalizedNode<?, ?>> augData = data.getChild(augCtx.get().getDomPathArgument());
162 if (augData.isPresent()) {
163 return augCtx.get().dataFromNormalizedNode(augData.get());
169 public String bindingToString() {
170 final ToStringHelper helper = MoreObjects.toStringHelper(context.bindingClass()).omitNullValues();
172 for (final Method m :context.getHashCodeAndEqualsMethods()) {
173 helper.add(m.getName(), getBindingData(m));
175 if (Augmentable.class.isAssignableFrom(context.bindingClass())) {
176 helper.add("augmentations", getAugmentationsImpl());
178 return helper.toString();
182 public int hashCode() {
183 final int prime = 31;
185 result = prime * result + context.hashCode();
186 result = prime * result + data.hashCode();
191 public boolean equals(final Object obj) {
198 if (getClass() != obj.getClass()) {
201 final LazyDataObject other = (LazyDataObject) obj;
202 if (context == null) {
203 if (other.context != null) {
206 } else if (!context.equals(other.context)) {
210 if (other.data != null) {
213 } else if (!data.equals(other.data)) {