Fix checkstyle warnings for config-netconf-connector
[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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import java.lang.reflect.Array;
15 import java.util.List;
16 import javax.management.openmbean.ArrayType;
17 import javax.management.openmbean.OpenType;
18
19 public class ArrayAttributeMappingStrategy extends AbstractAttributeMappingStrategy<List<Object>, ArrayType<?>> {
20
21     private final AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy;
22
23     public ArrayAttributeMappingStrategy(ArrayType<?> arrayType,
24             AttributeMappingStrategy<?, ? extends OpenType<?>> innerElementStrategy) {
25         super(arrayType);
26         this.innerElementStrategy = innerElementStrategy;
27     }
28
29     @Override
30     public Optional<List<Object>> mapAttribute(Object value) {
31         if (value == null){
32             return Optional.absent();
33         }
34
35         Preconditions.checkArgument(value.getClass().isArray(), "Value has to be instanceof Array ");
36
37         List<Object> retVal = Lists.newArrayList();
38
39         for (int i = 0; i < Array.getLength(value); i++) {
40             Object innerValue = Array.get(value, i);
41             Optional<?> mapAttribute = innerElementStrategy.mapAttribute(innerValue);
42
43             if (mapAttribute.isPresent()){
44                 retVal.add(mapAttribute.get());
45             }
46         }
47
48         return Optional.of(retVal);
49     }
50
51 }