37a7e98f719e9b4d2cf232b34efc6ebd2574f5cf
[yangtools.git] / yang / yang-model-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 com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.base.Preconditions;
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.Optional;
17 import org.opendaylight.yangtools.concepts.Delegator;
18
19 /**
20  * A {@link YangTextSchemaSource} backed by a file.
21  *
22  * @author Robert Varga
23  */
24 final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<File> {
25     private final File file;
26
27     YangTextFileSchemaSource(final SourceIdentifier identifier, final File file) {
28         super(identifier);
29         this.file = Preconditions.checkNotNull(file);
30     }
31
32     @Override
33     public File getDelegate() {
34         return file;
35     }
36
37     @Override
38     public InputStream openStream() throws IOException {
39         return new FileInputStream(file);
40     }
41
42     @Override
43     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
44         return toStringHelper.add("file", file);
45     }
46
47     @Override
48     public Optional<String> getSymbolicName() {
49         return Optional.of(file.toString());
50     }
51 }