Add generalized FileGenerator interface
[yangtools.git] / plugin / plugin-generator-api / src / main / java / org / opendaylight / yangtools / plugin / generator / api / GeneratedFileType.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 package org.opendaylight.yangtools.plugin.generator.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableMap;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18
19 /**
20  * Type of generated file. Four most common kinds are captured in {@link #RESOURCE}, {@link #SOURCE},
21  * {@link #TEST_RESOURCE} and {@link #TEST_SOURCE}, but others may be externally defined.
22  *
23  * <p>
24  * Users of {@link FileGenerator} are expected to provide sensible mapping of {@link GeneratedFileType} to their
25  * output structures. Notably they must handle pre-defined types, allow end users to specify mapping of custom types.
26  * They need to deal with runtime mismatches involving between FileGenerators and user's expectations, for example by
27  * issuing warnings when a mismatch is detected.
28  *
29  * @author Robert Varga
30  */
31 @Beta
32 @NonNullByDefault
33 public final class GeneratedFileType {
34     /**
35      * A generated resource file. This file should be part of artifact's resources.
36      */
37     public static final GeneratedFileType RESOURCE = new GeneratedFileType("resource");
38
39     /**
40      * A generated source file. This file should be part of main compilation unit.
41      */
42     public static final GeneratedFileType SOURCE = new GeneratedFileType("source");
43
44     /**
45      * A generated test resource file. This file should be part of test resources.
46      */
47     public static final GeneratedFileType TEST_RESOURCE = new GeneratedFileType("test-resource");
48
49     /**
50      * A generated test source file. This file should be part of test sources.
51      */
52     public static final GeneratedFileType TEST_SOURCE = new GeneratedFileType("test-source");
53
54     private static final ImmutableMap<String, GeneratedFileType> WELL_KNOWN = ImmutableMap.of(
55         RESOURCE.name(), RESOURCE, TEST_RESOURCE.name(), TEST_RESOURCE,
56         SOURCE.name(), SOURCE, TEST_SOURCE.name(), TEST_SOURCE);
57
58     private final String name;
59
60     private GeneratedFileType(final String name) {
61         this.name = requireNonNull(name);
62     }
63
64     public static GeneratedFileType of(final String name) {
65         checkArgument(!name.isEmpty());
66         final @Nullable GeneratedFileType wellKnown = WELL_KNOWN.get(name);
67         return wellKnown != null ? wellKnown : new GeneratedFileType(name);
68     }
69
70     public String name() {
71         return name;
72     }
73
74     @Override
75     public int hashCode() {
76         return name.hashCode();
77     }
78
79     @Override
80     public boolean equals(final @Nullable Object obj) {
81         return this == obj || obj instanceof GeneratedFileType && name.equals(((GeneratedFileType) obj).name);
82     }
83
84     @Override
85     public String toString() {
86         return MoreObjects.toStringHelper(this).add("name", name).toString();
87     }
88 }