2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.v3po.translate.util.read;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
23 import io.fd.honeycomb.v3po.translate.util.ReflectionUtils;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.Collections;
27 import java.util.List;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yangtools.concepts.Builder;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.Identifiable;
32 import org.opendaylight.yangtools.yang.binding.Identifier;
37 public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>>
38 extends ReflexiveReaderCustomizer<C, B>
39 implements ListReaderCustomizer<C, K, B> {
42 public ReflexiveListReaderCustomizer(final Class<C> typeClass, final Class<B> builderClass) {
43 super(typeClass, builderClass);
47 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) {
48 merge(parentBuilder, Collections.singletonList(readValue));
52 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final List<C> readData) {
53 final Optional<Method> method =
54 ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "set" + getTypeClass().getSimpleName(),
55 Collections.singletonList(List.class), parentBuilder.getClass());
57 checkArgument(method.isPresent(), "Unable to set %s to %s", readData, parentBuilder);
60 method.get().invoke(parentBuilder, readData);
61 } catch (IllegalAccessException | InvocationTargetException e) {
62 throw new IllegalArgumentException("Unable to set " + readData + " to " + parentBuilder, e);