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.Objects.ToStringHelper;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import java.lang.reflect.InvocationHandler;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.lang.reflect.Proxy;
19 import java.util.Objects;
20 import java.util.concurrent.ConcurrentHashMap;
21 import org.opendaylight.yangtools.binding.data.codec.util.AugmentationReader;
22 import org.opendaylight.yangtools.yang.binding.Augmentable;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
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 private 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");
54 @SuppressWarnings("rawtypes")
55 static DataObject create(final DataObjectCodecContext ctx, final NormalizedNodeContainer<?, ?, ?> data) {
56 Class<?> bindingClass = ctx.bindingClass();
57 return (DataObject) Proxy.newProxyInstance(bindingClass.getClassLoader(), new Class<?>[] { bindingClass },
58 new LazyDataObject(ctx, data));
62 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
63 if (method.getParameterTypes().length == 0) {
64 String name = method.getName();
65 if (GET_IMPLEMENTED_INTERFACE.equals(name)) {
66 return context.bindingClass();
67 } else if (TO_STRING.equals(name)) {
68 return bindingToString();
69 } else if (HASHCODE.equals(name)) {
70 return bindingHashCode();
72 return getBindingData(method);
73 } else if (GET_AUGMENTATION.equals(method.getName())) {
74 return getAugmentationImpl((Class<?>) args[0]);
75 } else if (EQUALS.equals(method.getName())) {
76 return bindingEquals(args[0]);
78 throw new UnsupportedOperationException("UNsupported method " + method);
81 private boolean bindingEquals(final Object other) {
85 if (!context.bindingClass().isAssignableFrom(other.getClass())) {
89 for (Method m : context.getHashCodeAndEqualsMethods()) {
90 Object thisValue = getBindingData(m);
91 Object otherValue = m.invoke(other);
92 if(!Objects.equals(thisValue, otherValue)) {
96 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
97 LOG.warn("Can not determine equality of {} and {}",this,other,e);
103 private Integer bindingHashCode() {
104 Integer ret = cachedHashcode;
109 final int prime = 31;
111 for (Method m : context.getHashCodeAndEqualsMethods()) {
112 Object value = getBindingData(m);
113 result += prime * result + ((value == null) ? 0 : value.hashCode());
115 if (Augmentation.class.isAssignableFrom(context.bindingClass())) {
116 result += prime * result + (getAugmentations(this).hashCode());
118 cachedHashcode = result;
122 private Object getBindingData(final Method method) {
123 Object cached = cachedData.get(method);
124 if (cached == null) {
125 Object readedValue = context.getBindingChildValue(method, data);
126 if (readedValue == null) {
129 cached = readedValue;
131 cachedData.putIfAbsent(method, cached);
134 return cached == NULL_VALUE ? null : cached;
137 private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentationsImpl() {
138 ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> ret = cachedAugmentations;
140 synchronized (this) {
141 ret = cachedAugmentations;
143 ret = ImmutableMap.copyOf(context.getAllAugmentationsFrom(data));
144 cachedAugmentations = ret;
153 public Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object obj) {
154 Preconditions.checkArgument(this == Proxy.getInvocationHandler(obj),
155 "Supplied object is not associated with this proxy handler");
157 return getAugmentationsImpl();
160 private Object getAugmentationImpl(final Class<?> cls) {
161 final ImmutableMap<Class<? extends Augmentation<?>>, Augmentation<?>> aug = cachedAugmentations;
165 Preconditions.checkNotNull(cls,"Supplied augmentation must not be null.");
166 final Optional<DataContainerCodecContext<?>> augCtx= context.getPossibleStreamChild(cls);
167 if(augCtx.isPresent()) {
168 final Optional<NormalizedNode<?, ?>> augData = data.getChild(augCtx.get().getDomPathArgument());
169 if (augData.isPresent()) {
170 return augCtx.get().dataFromNormalizedNode(augData.get());
176 public String bindingToString() {
177 ToStringHelper helper = com.google.common.base.Objects.toStringHelper(context.bindingClass());
179 for (Method m :context.getHashCodeAndEqualsMethods()) {
180 helper.add(m.getName(), getBindingData(m));
182 if (Augmentable.class.isAssignableFrom(context.bindingClass())) {
183 helper.add("augmentations", getAugmentationsImpl());
185 return helper.toString();
189 public int hashCode() {
190 final int prime = 31;
192 result = prime * result + context.hashCode();
193 result = prime * result + data.hashCode();
198 public boolean equals(final Object obj) {
205 if (getClass() != obj.getClass()) {
208 LazyDataObject other = (LazyDataObject) obj;
209 if (context == null) {
210 if (other.context != null) {
213 } else if (!context.equals(other.context)) {
217 if (other.data != null) {
220 } else if (!data.equals(other.data)) {