Use java.nio.file.Path in YangTextFileSchemaSource
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextFileSchemaSource.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.yang.model.repo.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.concepts.Delegator;
20
21 /**
22  * A {@link YangTextSchemaSource} backed by a file.
23  *
24  * @author Robert Varga
25  */
26 final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<Path> {
27     private final @NonNull Path path;
28
29     YangTextFileSchemaSource(final SourceIdentifier identifier, final Path path) {
30         super(identifier);
31         this.path = requireNonNull(path);
32     }
33
34     @Override
35     public Path getDelegate() {
36         return path;
37     }
38
39     @Override
40     public InputStream openStream() throws IOException {
41         return Files.newInputStream(path);
42     }
43
44     @Override
45     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46         return toStringHelper.add("path", path);
47     }
48
49     @Override
50     public Optional<String> getSymbolicName() {
51         // FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
52         //        is essentially getDelegate() anyway. Perhaps expose it as PathAware?
53         return Optional.of(path.toString());
54     }
55 }