54ffe08cfbd455f19e2d8996588e19ce7e706cc0
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ArrayAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping;
10
11 import java.lang.reflect.Array;
12 import java.util.List;
13
14 import javax.management.openmbean.ArrayType;
15 import javax.management.openmbean.OpenType;
16
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.Lists;
20
21 public class ArrayAttributeMappingStrategy extends AbstractAttributeMappingStrategy<List<Object>, ArrayType<?>> {
22
23     private final AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy;
24
25     public ArrayAttributeMappingStrategy(ArrayType<?> arrayType,
26             AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy) {
27         super(arrayType);
28         this.innerElementStrategy = innerElementStrategy;
29     }
30
31     @Override
32     public Optional<List<Object>> mapAttribute(Object value) {
33         if (value == null){
34             return Optional.absent();
35         }
36
37         Preconditions.checkArgument(value.getClass().isArray(), "Value has to be instanceof Array ");
38
39         List<Object> retVal = Lists.newArrayList();
40
41         for (int i = 0; i < Array.getLength(value); i++) {
42             Object innerValue = Array.get(value, i);
43             Optional<?> mapAttribute = innerElementStrategy.mapAttribute(innerValue);
44
45             if (mapAttribute.isPresent()){
46                 retVal.add(mapAttribute.get());
47             }
48         }
49
50         return Optional.of(retVal);
51     }
52
53 }