5eb58d5a1e5e910c08ef96f97c06b1218b027a64
[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 static com.google.common.base.Preconditions.checkNotNull;
4
5 import com.google.common.base.Optional;
6 import com.google.common.collect.Maps;
7 import com.google.common.io.Files;
8 import java.io.File;
9 import java.io.IOException;
10 import java.nio.charset.StandardCharsets;
11 import java.util.Map.Entry;
12 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
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 || !dstFile.exists()) {
35             Files.createParentDirs(dstFile);
36             Files.touch(dstFile);
37             Files.write(content, dstFile, StandardCharsets.UTF_8);
38             return Optional.of(Maps.immutableEntry(fqn, dstFile));
39         } else {
40             return Optional.absent();
41         }
42     }
43
44     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory) throws IOException {
45         return persist(srcDirectory, true);
46     }
47
48     @Override
49     public String toString() {
50         return getClass().getSimpleName() + "{" +
51                 "fqn=" + fqn +
52                 '}';
53     }
54
55     @Override
56     public boolean equals(Object o) {
57         if (this == o) {
58             return true;
59         }
60         if (o == null || getClass() != o.getClass()) {
61             return false;
62         }
63
64         GeneratedObject that = (GeneratedObject) o;
65
66         return fqn.equals(that.fqn);
67
68     }
69
70     @Override
71     public int hashCode() {
72         return fqn.hashCode();
73     }
74 }