Use plugin-generator-api in yang-maven-plugin
[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. Two most common kinds are captured in {@link #RESOURCE}, {@link #SOURCE}, but others may be
21  * 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     private static final ImmutableMap<String, GeneratedFileType> WELL_KNOWN = ImmutableMap.of(
45         RESOURCE.name(), RESOURCE, SOURCE.name(), SOURCE);
46
47     private final String name;
48
49     private GeneratedFileType(final String name) {
50         this.name = requireNonNull(name);
51     }
52
53     public static GeneratedFileType of(final String name) {
54         checkArgument(!name.isEmpty());
55         final @Nullable GeneratedFileType wellKnown = WELL_KNOWN.get(name);
56         return wellKnown != null ? wellKnown : new GeneratedFileType(name);
57     }
58
59     public String name() {
60         return name;
61     }
62
63     @Override
64     public int hashCode() {
65         return name.hashCode();
66     }
67
68     @Override
69     public boolean equals(final @Nullable Object obj) {
70         return this == obj || obj instanceof GeneratedFileType && name.equals(((GeneratedFileType) obj).name);
71     }
72
73     @Override
74     public String toString() {
75         return MoreObjects.toStringHelper(this).add("name", name).toString();
76     }
77 }