package org.opendaylight.yangtools.yang.model.repo.api;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
+import static org.opendaylight.yangtools.yang.common.YangNames.parseFilename;
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Map.Entry;
-import javax.annotation.Nonnull;
-import org.opendaylight.yangtools.yang.common.YangConstants;
-import org.opendaylight.yangtools.yang.common.YangNames;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.common.Revision;
/**
- * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation
- * as an {@link InputStream}.
+ * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
*/
@Beta
public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
- private final SourceIdentifier identifier;
+ private final @NonNull SourceIdentifier identifier;
protected YangTextSchemaSource(final SourceIdentifier identifier) {
- this.identifier = Preconditions.checkNotNull(identifier);
+ this.identifier = requireNonNull(identifier);
}
- public static SourceIdentifier identifierFromFilename(final String name) {
- checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION),
- "Filename %s does not have a .yang extension", name);
+ public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
+ checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename %s does not end with '%s'",
+ RFC6020_YANG_FILE_EXTENSION, name);
- final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
- final Entry<String, String> parsed = YangNames.parseFilename(baseName);
- return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
+ final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length());
+ final Entry<String, String> parsed = parseFilename(baseName);
+ return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue()));
}
/**
* @param delegate Backing ByteSource instance
* @return A new YangTextSchemaSource
*/
- public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
+ public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
final ByteSource delegate) {
return new DelegatedYangTextSchemaSource(identifier, delegate);
}
* @return A new YangTextSchemaSource
* @throws IllegalArgumentException if the file name has invalid format
*/
- public static YangTextSchemaSource delegateForByteSource(final String fileName, final ByteSource delegate) {
+ public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
+ final ByteSource delegate) {
return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
}
* @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
* @throws NullPointerException if file is null
*/
- public static YangTextSchemaSource forFile(final File file) {
- Preconditions.checkArgument(file.isFile(), "Supplied file %s is not a file", file);
+ public static @NonNull YangTextSchemaSource forFile(final File file) {
+ checkArgument(file.isFile(), "Supplied file %s is not a file", file);
return new YangTextFileSchemaSource(identifierFromFilename(file.getName()), file);
}
* @return A new instance.
* @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
*/
- public static ResourceYangTextSchemaSource forResource(final String resourceName) {
+ public static @NonNull YangTextSchemaSource forResource(final String resourceName) {
return forResource(YangTextSchemaSource.class, resourceName);
}
* @return A new instance.
* @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
*/
- public static ResourceYangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
+ public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
final SourceIdentifier identifier = identifierFromFilename(fileName);
final URL url = Resources.getResource(clazz, resourceName);
return identifier;
}
- @Nonnull
@Override
public Class<? extends YangTextSchemaSource> getType() {
return YangTextSchemaSource.class;