14619a24809c555c022017d697b060417040af09
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / java / GeneratedObject.java
1 /*
2  * Copyright (c) 2013, 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.yangjmxgenerator.plugin.java;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Maps;
15 import com.google.common.io.Files;
16 import java.io.File;
17 import java.io.IOException;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Map.Entry;
20 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
21
22 public class GeneratedObject {
23
24     private final FullyQualifiedName fqn;
25     private final String content;
26
27     public GeneratedObject(FullyQualifiedName fqn, String content) {
28         this.fqn = checkNotNull(fqn);
29         this.content = StringUtil.formatJavaSource(checkNotNull(content));
30     }
31
32     public FullyQualifiedName getFQN(){
33         return fqn;
34     }
35
36     public String getContent() {
37         return content;
38     }
39
40     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory, boolean overwrite) throws IOException {
41         File dstFile = fqn.toFile(srcDirectory);
42         if (overwrite || !dstFile.exists()) {
43             Files.createParentDirs(dstFile);
44             Files.touch(dstFile);
45             Files.write(content, dstFile, StandardCharsets.UTF_8);
46             return Optional.of(Maps.immutableEntry(fqn, dstFile));
47         } else {
48             return Optional.absent();
49         }
50     }
51
52     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory) throws IOException {
53         return persist(srcDirectory, true);
54     }
55
56     @Override
57     public String toString() {
58         return getClass().getSimpleName() + "{" +
59                 "fqn=" + fqn +
60                 '}';
61     }
62
63     @Override
64     public boolean equals(Object o) {
65         if (this == o) {
66             return true;
67         }
68         if (o == null || getClass() != o.getClass()) {
69             return false;
70         }
71
72         GeneratedObject that = (GeneratedObject) o;
73
74         return fqn.equals(that.fqn);
75
76     }
77
78     @Override
79     public int hashCode() {
80         return fqn.hashCode();
81     }
82 }