/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.io;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.util.Exceptions.newIllegalStateException;
import static java.lang.String.format;

abstract class ResourceObject {

    private final String path;
    private final ClassLoader classLoader;

    ResourceObject(String path, ClassLoader classLoader) {
        this.path = checkNotNull(path);
        this.classLoader = checkNotNull(classLoader);
    }

    private @Nullable URL findUrl() {
        @Nullable URL url = classLoader.getResource(path);
        return url;
    }

    /**
     * Checks if the resource with such a name exists in the classpath.
     *
     * @return {@code true} if the resource is present, {@code false} otherwise
     */
    public boolean exists() {
        @Nullable URL resource = findUrl();
        return resource != null;
    }

    /**
     * Obtains a {@link URL} of the resolved resource.
     *
     * <p>If the resource cannot be resolved (i.e. the file does not exist), throws
     * an {@code IllegalStateException}.
     *
     * @return the resource URL
     */
    public URL locate() {
        @Nullable URL url = findUrl();
        if (url == null) {
            throw cannotFind();
        }
        return url;
    }

    /** Obtains the resource path of this resource object as passed on creation. */
    final String path() {
        return path;
    }

    final IllegalStateException cannotFind() {
        return newIllegalStateException("Unable to find `%s`.", this);
    }

    final Enumeration<URL> resources() throws IOException {
        return classLoader.getResources(path);
    }

    @Override
    public int hashCode() {
        return path.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ResourceObject)) {
            return false;
        }
        ResourceObject other = (ResourceObject) o;
        return path.equals(other.path);
    }

    @Override
    public String toString() {
        return format("`%s` via ClassLoader `%s`", path, classLoader);
    }
}

@@ -1569,16 +1569,263 @@
 ormat;%0A%0A
+/**%0A * Abstract base for objects stored in program resources.%0A *%0A * %3Cp%3ESuch a resource is represented by a string path relative to the %7B@code %22resources%22%7D directory%0A * of a project, and is loaded by a specified %7B@link ClassLoader%7D on runtime.%0A */%0A
 abstract
@@ -1924,16 +1924,95 @@
 oader;%0A%0A
+    /**%0A     * Creates a new resource with given path and classloader.%0A     */%0A
     Reso
@@ -3109,32 +3109,117 @@
 rn path;%0A    %7D%0A%0A
+    /**%0A     * Crate an exception stating that the resource cannot be found.%0A     */%0A
     final Illega
@@ -3323,24 +3323,93 @@
 is);%0A    %7D%0A%0A
+    /**%0A     * Enumerates all resources with the given path.%0A     */%0A
     final En

Add missing javadocs