Merge "Bug 509: Improve logging in InMemoryDataStore."
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / java / GeneratedObject.java
1 package org.opendaylight.controller.config.yangjmxgenerator.plugin.java;
2
3 import com.google.common.base.Optional;
4 import com.google.common.collect.Maps;
5 import org.apache.commons.io.FileUtils;
6 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.Map.Entry;
11
12 import static com.google.common.base.Preconditions.checkNotNull;
13
14 public class GeneratedObject {
15
16     private final FullyQualifiedName fqn;
17     private final String content;
18
19     public GeneratedObject(FullyQualifiedName fqn, String content) {
20         this.fqn = checkNotNull(fqn);
21         this.content = StringUtil.formatJavaSource(checkNotNull(content));
22     }
23
24     public FullyQualifiedName getFQN(){
25         return fqn;
26     }
27
28     public String getContent() {
29         return content;
30     }
31
32     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory, boolean overwrite) throws IOException {
33         File dstFile = fqn.toFile(srcDirectory);
34         if (overwrite == true || dstFile.exists() == false) {
35             FileUtils.write(dstFile, content);
36             return Optional.of(Maps.immutableEntry(fqn, dstFile));
37         } else {
38             return Optional.absent();
39         }
40     }
41
42     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory) throws IOException {
43         return persist(srcDirectory, true);
44     }
45
46     @Override
47     public String toString() {
48         return getClass().getSimpleName() + "{" +
49                 "fqn=" + fqn +
50                 '}';
51     }
52
53     @Override
54     public boolean equals(Object o) {
55         if (this == o) {
56             return true;
57         }
58         if (o == null || getClass() != o.getClass()) {
59             return false;
60         }
61
62         GeneratedObject that = (GeneratedObject) o;
63
64         if (!fqn.equals(that.fqn)) {
65             return false;
66         }
67
68         return true;
69     }
70
71     @Override
72     public int hashCode() {
73         return fqn.hashCode();
74     }
75 }