/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.org.springframework.core.io.support;

import permafrost.tundra.org.springframework.core.io.Resource;
import permafrost.tundra.org.springframework.util.Assert;

/**
 * Region of a {@link Resource} implementation, materialized by a {@code position}
 * within the {@link Resource} and a byte {@code count} for the length of that region.
 *
 * @author Arjen Poutsma
 * @since 4.3
 */
public class ResourceRegion {

    private final Resource resource;

    private final long position;

    private final long count;


    /**
     * Create a new {@code ResourceRegion} from a given {@link Resource}.
     * This region of a resource is represented by a start {@code position}
     * and a byte {@code count} within the given {@code Resource}.
     * @param resource a Resource
     * @param position the start position of the region in that resource
     * @param count the byte count of the region in that resource
     */
    public ResourceRegion(Resource resource, long position, long count) {
        Assert.notNull(resource, "Resource must not be null");
        Assert.isTrue(position >= 0, "'position' must be larger than or equal to 0");
        Assert.isTrue(count >= 0, "'count' must be larger than or equal to 0");
        this.resource = resource;
        this.position = position;
        this.count = count;
    }


    /**
     * Return the underlying {@link Resource} for this {@code ResourceRegion}
     */
    public Resource getResource() {
        return this.resource;
    }

    /**
     * Return the start position of this region in the underlying {@link Resource}
     */
    public long getPosition() {
        return this.position;
    }

    /**
     * Return the byte count of this region in the underlying {@link Resource}
     */
    public long getCount() {
        return this.count;
    }

}
