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

sviolet.slate.common.util.common.LambdaBuildable Maven / Gradle / Ivy

There is a newer version: 22.1.0
Show newest version
/*
 * Copyright (C) 2015-2019 S.Violet
 *
 * 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.
 *
 * Project GitHub: https://github.com/shepherdviolet/slate
 * Email: [email protected]
 */

package sviolet.slate.common.util.common;

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

/**
 * [JAVA8+]用Lambda表达式创建Map/Set/Object/List
 *
 * @since 1.8
 */
public interface LambdaBuildable {

    /**
     * 创建HashMap
     *
     * 
     *         Map map = buildHashMap(i -> {
     *             i.put("a", "b");
     *             i.put("c", "d");
     *         });
     * 
* * @since 1.8 */ default Map buildHashMap(Consumer> supplier) { Map map = new HashMap<>(16); if (supplier == null) { return map; } supplier.accept(map); return map; } /** * 创建LinkedHashMap * *
     *         Map map = buildLinkedHashMap(i -> {
     *             i.put("a", "b");
     *             i.put("c", "d");
     *         });
     * 
* * @since 1.8 */ default Map buildLinkedHashMap(Consumer> supplier) { Map map = new LinkedHashMap<>(16); if (supplier == null) { return map; } supplier.accept(map); return map; } /** * 创建HashSet * *
     *         Set set = buildHashSet(i -> {
     *             i.add("a");
     *             i.add("c");
     *         });
     * 
* * @since 1.8 */ default Set buildHashSet(Consumer> supplier) { Set set = new HashSet<>(16); if (supplier == null) { return set; } supplier.accept(set); return set; } /** * 创建Object * *
     *         Bean bean = buildObject(() -> {
     *             Bean obj = new Bean();
     *             obj.setName("123");
     *             obj.setId("456");
     *             return obj;
     *         });
     * 
* * @since 1.8 */ default T buildObject(Supplier supplier) { return supplier.get(); } /** * 创建ArrayList, 一般情况下用Arrays.asList * *
     *     Arrays.asList(
     *          item1,
     *          item2
     *     );
     * 
* *
     *         List list = buildArrayList(i -> {
     *            i.add("a");
     *            i.add("b");
     *         });
     * 
* * @since 1.8 */ default List buildArrayList(Consumer> supplier) { List list = new ArrayList<>(); if (supplier == null) { return list; } supplier.accept(list); return list; } /** * 创建LinkedList, 一般情况下用Arrays.asList * *
     *         List list = buildLinkedList(i -> {
     *            i.add("a");
     *            i.add("b");
     *         });
     * 
* * @since 1.8 */ default List buildLinkedList(Consumer> supplier) { List list = new LinkedList<>(); if (supplier == null) { return list; } supplier.accept(list); return list; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy