Mechanical code cleanup (config)
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / mapping / SimpleAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.mapping.attributes.mapping;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import java.util.Date;
15 import java.util.Map;
16 import javax.management.openmbean.SimpleType;
17 import org.opendaylight.controller.config.facade.xml.util.Util;
18
19 public class SimpleAttributeMappingStrategy extends AbstractAttributeMappingStrategy<String, SimpleType<?>> {
20
21     public SimpleAttributeMappingStrategy(SimpleType<?> openType) {
22         super(openType);
23     }
24
25     @Override
26     public Optional<String> mapAttribute(Object value) {
27         if (value == null){
28             return Optional.absent();
29         }
30
31         String expectedClass = getOpenType().getClassName();
32         String realClass = value.getClass().getName();
33         Preconditions.checkArgument(realClass.equals(expectedClass), "Type mismatch, expected " + expectedClass
34                 + " but was " + realClass);
35
36         WriterPlugin prefferedPlugin = writerPlugins.get(value.getClass().getCanonicalName());
37         prefferedPlugin = prefferedPlugin == null ? writerPlugins.get(DEFAULT_WRITER_PLUGIN) : prefferedPlugin;
38         return Optional.of(prefferedPlugin.writeObject(value));
39     }
40
41     private static final String DEFAULT_WRITER_PLUGIN = "default";
42     private static final Map<String, WriterPlugin> writerPlugins = Maps.newHashMap();
43     static {
44         writerPlugins.put(DEFAULT_WRITER_PLUGIN, new DefaultWriterPlugin());
45         writerPlugins.put(Date.class.getCanonicalName(), new DatePlugin());
46     }
47
48     /**
49      * Custom writer plugins must implement this interface.
50      */
51     interface WriterPlugin {
52         String writeObject(Object value);
53     }
54
55     static class DefaultWriterPlugin implements WriterPlugin {
56
57         @Override
58         public String writeObject(Object value) {
59             return value.toString();
60         }
61     }
62
63     static class DatePlugin implements WriterPlugin {
64
65         @Override
66         public String writeObject(Object value) {
67             Preconditions.checkArgument(value instanceof Date, "Attribute must be Date");
68             return Util.writeDate((Date) value);
69         }
70     }
71
72 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.