001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.utils;
017
018import com.jfinal.plugin.activerecord.Model;
019import com.jfinal.plugin.activerecord.Page;
020import io.jboot.db.model.JbootModel;
021import io.jboot.exception.JbootException;
022
023import java.lang.reflect.Array;
024import java.util.ArrayList;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029/**
030 * @author michael yang (fuhai999@gmail.com)
031 * @Date: 2019/11/21
032 */
033public class ModelUtil {
034
035    /**
036     * copy model list
037     *
038     * @param modelList
039     * @param <M>
040     * @return
041     */
042    public static <M extends JbootModel<?>> List<M> copy(List<M> modelList) {
043        if (modelList == null || modelList.isEmpty()) {
044            return modelList;
045        }
046
047        List<M> list = modelList instanceof ArrayList
048                ? new ArrayList<>(modelList.size())
049                : newInstance(modelList.getClass());
050
051        for (M m : modelList) {
052            list.add(copy(m));
053        }
054
055        return list;
056    }
057
058
059    /**
060     * copy model set
061     *
062     * @param modelSet
063     * @param <M>
064     * @return
065     */
066    public static <M extends JbootModel<?>> Set<M> copy(Set<M> modelSet) {
067        if (modelSet == null || modelSet.isEmpty()) {
068            return modelSet;
069        }
070
071        Set<M> set = modelSet instanceof HashSet
072                ? new HashSet<>(modelSet.size())
073                : newInstance(modelSet.getClass());
074
075        for (M m : modelSet) {
076            set.add(copy(m));
077        }
078
079        return set;
080    }
081
082
083    /**
084     * copy model array
085     *
086     * @param models
087     * @param <M>
088     * @return
089     */
090    public static <M extends JbootModel<?>> M[] copy(M[] models) {
091        if (models == null || models.length == 0) {
092            return models;
093        }
094
095        M[] array = (M[]) Array.newInstance(models.getClass().getComponentType(), models.length);
096        int i = 0;
097        for (M m : models) {
098            array[i++] = copy(m);
099        }
100        return array;
101    }
102
103
104    /**
105     * copy model page
106     *
107     * @param modelPage
108     * @param <M>
109     * @return
110     */
111    public static <M extends JbootModel<?>> Page<M> copy(Page<M> modelPage) {
112        if (modelPage == null) {
113            return null;
114        }
115
116        List<M> modelList = modelPage.getList();
117        if (modelList == null || modelList.isEmpty()) {
118            return modelPage;
119        }
120
121        modelPage.setList(copy(modelList));
122        return modelPage;
123    }
124
125
126    /**
127     * copy model
128     *
129     * @param model
130     * @param <M>
131     * @return
132     */
133    public static <M extends JbootModel<?>> M copy(M model) {
134        return model == null ? null : (M) model.copy();
135    }
136
137
138    private static <T> T newInstance(Class<T> clazz) {
139        try {
140            return clazz.newInstance();
141        } catch (Exception e) {
142            throw new JbootException("Can not newInstance with class: " + clazz.getName() + "\n" + e, e);
143        }
144    }
145
146
147    public static void keep(List<? extends Model> models, String... attrs) {
148        if (models == null || models.isEmpty()) {
149            return;
150        }
151
152        models.forEach(model -> model.keep(attrs));
153    }
154
155
156}