Add generalized FileGenerator interface
[yangtools.git] / plugin / plugin-generator-api / src / main / java / org / opendaylight / yangtools / plugin / generator / api / GeneratedFilePath.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.CharMatcher;
15 import com.google.common.base.MoreObjects;
16 import java.io.File;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21  * A relative path to a generated file.
22  *
23  * @author Robert Varga
24  */
25 @Beta
26 @NonNullByDefault
27 public final class GeneratedFilePath {
28     public static final char SEPARATOR = '/';
29     public static final String SEPARATOR_STR = "/";
30
31     private static final CharMatcher FS_MATCHER = CharMatcher.is(File.separatorChar);
32
33     private final String path;
34
35     private GeneratedFilePath(final String path) {
36         this.path = requireNonNull(path);
37         checkArgument(!path.isEmpty());
38     }
39
40     public static GeneratedFilePath ofPath(final String path) {
41         return new GeneratedFilePath(path);
42     }
43
44     public static GeneratedFilePath ofFile(final File file) {
45         return ofFilePath(file.getPath());
46     }
47
48     public static GeneratedFilePath ofFilePath(final String filePath) {
49         return ofPath(FS_MATCHER.replaceFrom(filePath, SEPARATOR));
50     }
51
52     public String getPath() {
53         return path;
54     }
55
56     @Override
57     public int hashCode() {
58         return path.hashCode();
59     }
60
61     @Override
62     public boolean equals(final @Nullable Object obj) {
63         return this == obj || obj instanceof GeneratedFilePath && path.equals(((GeneratedFilePath) obj).path);
64     }
65
66     @Override
67     public String toString() {
68         return MoreObjects.toStringHelper(this).add("path", path).toString();
69     }
70 }