/*
 * Copyright (c) 2014, Oracle America, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *  * Neither the name of Oracle nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.modelmap.sample.model;

import static java.util.Arrays.asList;
import static org.modelmap.sample.model.FavoriteWebsite.webSite;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.modelmap.core.FieldModel;
import org.modelmap.core.FieldModels;
import org.modelmap.sample.field.SampleFieldIdInfo;
import org.openjdk.jmh.annotations.*;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(3)
public class CloneBenchmark {

    private SampleModel source;

    @Setup
    public void init() {
        source = SampleModels.sample();
    }

    @Benchmark
    public SampleModel clone_java_bean() {
        SampleModel clone = new SampleModel();

        User user = new User();
        user.setId(source.getUser().getId());
        user.setFirstName(source.getUser().getFirstName());
        user.setLastName(source.getUser().getLastName());
        user.setBirthDate(source.getUser().getBirthDate());
        clone.setUser(user);

        Account account = new Account();
        account.setId(source.getAccount().getId());
        account.setAcceptEmail(source.getAccount().getAcceptEmail());
        account.setEmail(source.getAccount().getEmail());
        account.setEmailTypes(source.getAccount().getEmailTypes());
        account.setLanguage(source.getAccount().getLanguage());
        account.setLogin(source.getAccount().getLogin());
        account.setPassword(source.getAccount().getPassword());
        account.setPhoneNumber(source.getAccount().getPhoneNumber());
        account.setTimezone(source.getAccount().getTimezone());

        account.setTop3WebSite(new ArrayList<>());
        source.getAccount().getTop3WebSite()
                        .forEach(site -> account.getTop3WebSite().add(webSite(site.getName(), site.getUrl())));
        clone.setAccount(account);
        return clone;
    }

    @Benchmark
    public FieldModel clone_field_model() {
        FieldModel fieldModel = new SampleModelWrapper(source);
        FieldModel clone = new SampleModelWrapper();

        asList(fieldModel.getFieldInfos()).forEach(e -> clone.set(e.id(), fieldModel.get(e.id())));
        return clone;
    }

    @Benchmark
    public FieldModel clone_stream_sequential() {
        return FieldModels.stream(new SampleModelWrapper(source), false)
                        .filter(e -> e.getValue() != null)
                        .collect(FieldModels.toFieldModel(SampleFieldIdInfo.values()));
    }

    @Benchmark
    public FieldModel clone_stream_parallel() {
        return FieldModels.stream(new SampleModelWrapper(source), true)
                        .filter(e -> e.getValue() != null)
                        .collect(FieldModels.toFieldModel(SampleFieldIdInfo.values()));
    }
}

@@ -1,1657 +1,41 @@
-/*%0A * Copyright (c) 2014, Oracle America, Inc.%0A * All rights reserved.%0A *%0A * Redistribution and use in source and binary forms, with or without%0A * modification, are permitted provided that the following conditions are met:%0A *%0A *  * Redistributions of source code must retain the above copyright notice,%0A *    this list of conditions and the following disclaimer.%0A *%0A *  * Redistributions in binary form must reproduce the above copyright%0A *    notice, this list of conditions and the following disclaimer in the%0A *    documentation and/or other materials provided with the distribution.%0A *%0A *  * Neither the name of Oracle nor the names of its contributors may be used%0A *    to endorse or promote products derived from this software without%0A *    specific prior written permission.%0A *%0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0A * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE%0A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE%0A * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE%0A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR%0A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF%0A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS%0A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN%0A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)%0A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF%0A * THE POSSIBILITY OF SUCH DAMAGE.%0A */%0A%0Apackage org.modelmap.sample.model;%0A%0Aimport static java.util.Arrays.asList;
+%0A%0Apackage org.modelmap.sample.model;%0A
 %0Aimp
@@ -125,16 +125,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -355,9 +355,417 @@
 ons.
-*
+Benchmark;%0Aimport org.openjdk.jmh.annotations.BenchmarkMode;%0Aimport org.openjdk.jmh.annotations.Fork;%0Aimport org.openjdk.jmh.annotations.Measurement;%0Aimport org.openjdk.jmh.annotations.Mode;%0Aimport org.openjdk.jmh.annotations.OutputTimeUnit;%0Aimport org.openjdk.jmh.annotations.Scope;%0Aimport org.openjdk.jmh.annotations.Setup;%0Aimport org.openjdk.jmh.annotations.State;%0Aimport org.openjdk.jmh.annotations.Warmup
 ;%0A%0A@
@@ -2517,25 +2517,24 @@
 pper();%0A
-%0A
         
 asList(f
@@ -2529,14 +2529,21 @@
     
-asList
+Arrays.stream
 (fie
@@ -2794,33 +2794,8 @@
 lse)
-%0A                        
 .fil
@@ -3058,33 +3058,8 @@
 rue)
-%0A                        
 .fil

use Arrays.stream instead of Arrays.asList