001/*
002 * MIT License
003 *
004 * Copyright (c) 2023 Michael Cowan
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024
025package io.blt.util;
026
027import java.util.function.Consumer;
028import java.util.function.Supplier;
029
030/**
031 * Static utility methods for operating on {@code Object}.
032 */
033public final class Obj {
034
035    private Obj() {
036        throw new IllegalAccessError("Utility class should be accessed statically and never constructed");
037    }
038
039    /**
040     * Passes the {@code instance} to the {@code consumer}, then returns the {@code instance}.
041     * e.g.
042     * <pre>{@code
043     * var user = Obj.poke(new User(), u -> {
044     *     u.setName("Greg");
045     *     u.setAge(15);
046     * });
047     * }</pre>
048     *
049     * @param instance instance to consume and return
050     * @param consumer operation to perform on {@code instance}
051     * @param <T>      type of {@code instance}
052     * @return {@code instance} after accepting side effects via {@code consumer}.
053     */
054    public static <T> T poke(T instance, Consumer<T> consumer) {
055        consumer.accept(instance);
056        return instance;
057    }
058
059    /**
060     * Calls the {@code supplier} to retrieve an instance which is mutated by the {@code consumer} then returned.
061     * e.g.
062     * <pre>{@code
063     * var user = Obj.tap(User::new, u -> {
064     *     u.setName("Greg");
065     *     u.setAge(15);
066     * });
067     * }</pre>
068     *
069     * @param supplier Supplies an instance to consume and return
070     * @param consumer Operation to perform on supplied instance
071     * @param <T>      type of instance
072     * @return Supplied instance after applying side effects via {@code consumer}.
073     */
074    public static <T> T tap(Supplier<T> supplier, Consumer<T> consumer) {
075        return poke(supplier.get(), consumer);
076    }
077
078}