All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jinahya.simple.file.back.FileContext Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Jin Kwon <jinahya_at_gmail.com>.
 *
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.github.jinahya.simple.file.back;


import com.github.jinahya.simple.file.back.FileBack.FileOperation;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Optional;
import static java.util.Optional.ofNullable;
import java.util.function.Consumer;
import java.util.function.Supplier;


/**
 * A context between clients and file backs.
 *
 * @author Jin Kwon <jinahya_at_gmail.com>
 */
public interface FileContext {


    /**
     * Properties can be configured within file contexts.
     */
    public static enum PropertyKey {


        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Supplier} which supplies the target operation.
         */
        FILE_OPERATION_SUPPLIER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes the path name.
         */
        PATH_NAME_CONSUMER,
        /**
         * A constant for the key of a property whose values si an instance of
         * {@code Supplier} which supplies the path name.
         */
        PATH_NAME_SUPPLIER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes source file
         * channel.
         */
        SOURCE_CHANNEL_CONSUMER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes the target file
         * channel.
         */
        TARGET_CHANNEL_CONSUMER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Supplier} which supplies the source file
         * channel.
         */
        SOURCE_CHANNEL_SUPPLIER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Supplier} which supplies a target file
         * channel.
         */
        TARGET_CHANNEL_SUPPLIER,
        /**
         * A constants for the key of a property whose value is an instance of
         * {@code Consumer} which consumes the number of bytes copied from
         * the source file part.
         */
        SOURCE_COPIED_CONSUMER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes the number of bytes copied to
         * target file part.
         */
        TARGET_COPIED_CONSUMER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Supplier} which supplies the key bytes for
         * locating the source file part.
         */
        SOURCE_KEY_SUPPLIER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Supplier} which supplies the key bytes the
         * locating the target file part.
         */
        TARGET_KEY_SUPPLIER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes an implementation specific
         * type of source file reference.
         */
        SOURCE_OBJECT_CONSUMER,
        /**
         * A constant for the key of a property whose value is an instance of
         * {@code Consumer} which consumes an implementation specific
         * type of target file reference.
         */
        TARGET_OBJECT_CONSUMER


    }


    /**
     * Returns an optional property value mapped to specified
     * {@code propertyKey}.
     *
     * @param propertyKey the property key
     *
     * @return an optional value.
     */
    Optional property(PropertyKey propertyKey);


    /**
     * Returns an optional property value mapped to specified
     * {@code propertyKey}.
     *
     * @param  value type parameter
     * @param proprtyKey property key
     * @param valueType value type.
     *
     * @return an optional value of property.
     */
    default  Optional property(final PropertyKey proprtyKey,
                                     final Class valueType) {

        if (valueType == null) {
            throw new NullPointerException("null type");
        }

        return ofNullable(valueType.cast(property(proprtyKey).orElse(null)));
    }


    /**
     * Sets a new property value mapped to specified {@code propertyKey}. Note
     * that providing {@code null} for {@code propertyValue} will remove
     * previous mapping.
     *
     * @param propertyKey the property key
     * @param propertyValue the property value; {@code null} for removal of
     * mapping.
     *
     * @return an optional property value previously mapped to specified
     * {@code propertyKey}.
     */
    Optional property(PropertyKey propertyKey, Object propertyValue);


    /**
     * Sets a new property value mapped to specified {@code propertyKey}.
     *
     * @param  value type parameter
     * @param propertyKey property key
     * @param propertyValue the new property value.
     * @param valueType value type.
     *
     * @return an optional property value previously mapped to specified
     * {@code propertyKey}.
     */
    default  Optional property(final PropertyKey propertyKey,
                                     final T propertyValue,
                                     final Class valueType) {

        if (valueType == null) {
            throw new NullPointerException("null type");
        }

        return ofNullable(valueType.cast(property(propertyKey, propertyValue)
            .orElse(null)));
    }


    /**
     * Return the current property value mapped to
     * {@link PropertyKey#FILE_OPERATION_SUPPLIER}.
     *
     * @return the current value mapped to
     * {@link PropertyKey#FILE_OPERATION_SUPPLIER} or {@code null} if no
     * mappings found.
     */
    @SuppressWarnings("unchecked")
    default Supplier fileOperationSupplier() {

        return (Supplier) property(
            PropertyKey.FILE_OPERATION_SUPPLIER)
            .orElse(null);
    }


    /**
     * Sets a new property value mapped to
     * {@link PropertyKey#FILE_OPERATION_SUPPLIER}.
     *
     * @param fileOperationSupplier the new value.
     *
     * @return the previous value mapped to
     * {@link PropertyKey#FILE_OPERATION_SUPPLIER} or {@code null} if there is
     * no mappings.
     */
    @SuppressWarnings("unchecked")
    default Supplier fileOperationSupplier(
        final Supplier fileOperationSupplier) {

        return (Supplier) property(
            PropertyKey.FILE_OPERATION_SUPPLIER, fileOperationSupplier)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer pathNameConsumer() {

        return (Consumer) property(PropertyKey.PATH_NAME_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer pathNameConsumer(
        final Consumer pathNameConsumer) {

        return (Consumer) property(PropertyKey.PATH_NAME_CONSUMER,
                                           pathNameConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier pathNameSupplier() {

        return (Supplier) property(PropertyKey.PATH_NAME_SUPPLIER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier pathNameSupplier(
        final Supplier pathNameSupplier) {

        return (Supplier) property(PropertyKey.PATH_NAME_SUPPLIER,
                                           pathNameSupplier)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer sourceChannelConsumer() {

        return (Consumer) property(
            PropertyKey.SOURCE_CHANNEL_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer sourceChannelConsumer(
        final Consumer sourceChannelConsumer) {

        return (Consumer) property(
            PropertyKey.SOURCE_CHANNEL_CONSUMER, sourceChannelConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier sourceChannelSupplier() {

        return (Supplier) property(
            PropertyKey.SOURCE_CHANNEL_SUPPLIER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier sourceChannelSupplier(
        final Supplier sourceChannelSuppleir) {

        return (Supplier) property(
            PropertyKey.SOURCE_CHANNEL_SUPPLIER, sourceChannelSuppleir)
            .orElse(null);
    }


    /**
     * Returns the current property value mapped to
     * {@link PropertyKey#SOURCE_COPIED_CONSUMER}.
     *
     * @return the current property value mapped to
     * {@link PropertyKey#SOURCE_COPIED_CONSUMER} or {@code null} if no mappings
     * found.
     */
    @SuppressWarnings("unchecked")
    default Consumer sourceCopiedConsumer() {

        return (Consumer) property(PropertyKey.SOURCE_COPIED_CONSUMER)
            .orElse(null);
    }


    /**
     * Sets the new value for {@link PropertyKey#SOURCE_COPIED_CONSUMER}.
     *
     * @param sourceCopiedConsumer the new value; {@code null} for removal of
     * entry.
     *
     * @return previous value mapped; possibly {@code null}.
     */
    @SuppressWarnings("unchecked")
    default Consumer sourceCopiedConsumer(
        final Consumer sourceCopiedConsumer) {

        return (Consumer) property(PropertyKey.SOURCE_COPIED_CONSUMER,
                                         sourceCopiedConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier sourceKeySupplier() {

        return (Supplier) property(PropertyKey.SOURCE_KEY_SUPPLIER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier sourceKeySupplier(
        final Supplier sourceKeySupplier) {

        return (Supplier) property(PropertyKey.SOURCE_KEY_SUPPLIER,
                                               sourceKeySupplier)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer sourceObjectConsumer() {

        return (Consumer) property(PropertyKey.SOURCE_OBJECT_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer sourceObjectConsumer(
        final Consumer sourceObjectConsumer) {

        return (Consumer) property(PropertyKey.SOURCE_OBJECT_CONSUMER,
                                           sourceObjectConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetCopiedConsumer() {

        return (Consumer) property(PropertyKey.TARGET_COPIED_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetCopiedConsumer(
        final Consumer targetCopiedConsumer) {

        return (Consumer) property(PropertyKey.TARGET_COPIED_CONSUMER,
                                         targetCopiedConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier targetKeySupplier() {

        return (Supplier) property(PropertyKey.TARGET_KEY_SUPPLIER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier targetKeySupplier(
        final Supplier targetKeySupplier) {

        return (Supplier) property(PropertyKey.TARGET_KEY_SUPPLIER,
                                               targetKeySupplier)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetChannelConsumer() {

        return (Consumer) property(
            PropertyKey.TARGET_CHANNEL_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetChannelConsumer(
        final Consumer targetChannelConsumer) {

        return (Consumer) property(
            PropertyKey.TARGET_CHANNEL_CONSUMER, targetChannelConsumer)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier targetChannelSupplier() {

        return (Supplier) property(
            PropertyKey.TARGET_CHANNEL_SUPPLIER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Supplier targetChannelSupplier(
        final Supplier targetChannelSupplier) {

        return (Supplier) property(
            PropertyKey.TARGET_CHANNEL_SUPPLIER, targetChannelSupplier)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetObjectConsumer() {

        return (Consumer) property(PropertyKey.TARGET_OBJECT_CONSUMER)
            .orElse(null);
    }


    @SuppressWarnings("unchecked")
    default Consumer targetObjectConsumer(
        final Consumer targetObjectConsumer) {

        return (Consumer) property(PropertyKey.TARGET_OBJECT_CONSUMER,
                                           targetObjectConsumer)
            .orElse(null);
    }


}