// Copyright 2014 takahashikzn
//
// 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 jp.root42.indolently.ref;

import java.util.function.Consumer;
import java.util.function.Supplier;


/**
 * @param <T> value type
 * @author takahashikzn
 */
public interface ValueReference<T>
    extends Supplier<T>, Consumer<T> {

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static BoolRef of(final boolean val) {
        return new BoolRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static IntRef of(final int val) {
        return new IntRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static LongRef of(final long val) {
        return new LongRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static DoubleRef of(final double val) {
        return new DoubleRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static FloatRef of(final float val) {
        return new FloatRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static ShortRef of(final short val) {
        return new ShortRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static ByteRef of(final byte val) {
        return new ByteRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static CharRef of(final char val) {
        return new CharRef(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static <T> Ref<T> of(final T val) {
        return new Ref<>(val);
    }

    /**
     * create a reference of value.
     *
     * @param val initial value
     * @return reference of value
     */
    static <T extends Comparable<T>> CmpRef<T> of(final T val) {
        return new CmpRef<>(val);
    }
}

@@ -613,16 +613,43 @@
 y.ref;%0A%0A
+import java.util.Optional;%0A
 import j
@@ -713,16 +713,57 @@
 plier;%0A%0A
+import jp.root42.indolently.Indolently;%0A%0A
 %0A/**%0A * 
@@ -887,16 +887,604 @@
 r%3CT%3E %7B%0A%0A
+    /**%0A     * get value as optional representation.%0A     *%0A     * @return optional representation of the value%0A     */%0A    default Optional%3CT%3E opt() %7B%0A        return Indolently.optional(this.get());%0A    %7D%0A%0A    /**%0A     * get value if exists, otherwise use the value from supplier as this reference's value.%0A     *%0A     * @param f value supplier%0A     * @return the value%0A     */%0A    default T orAccept(final Supplier%3C? extends T%3E f) %7B%0A%0A        return this.opt().orElseGet(() -%3E %7B%0A            final T val = f.get();%0A            this.accept(val);%0A            return val;%0A        %7D);%0A    %7D%0A%0A
     /**%0A

add opt/orAccept