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

org.cometd.oort.OortObjectMergers Maven / Gradle / Ivy

There is a newer version: 8.0.6
Show newest version
/*
 * Copyright (c) 2008-2018 the original author or authors.
 *
 * 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 org.cometd.oort;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class OortObjectMergers {
    private OortObjectMergers() {
    }

    public static OortObject.Merger longSum() {
        return new LongSumMerger();
    }

    public static  OortObject.Merger, Map> mapUnion() {
        return new MapUnionMerger<>();
    }

    public static  OortObject.Merger, ConcurrentMap> concurrentMapUnion() {
        return new ConcurrentMapUnionMerger<>();
    }

    public static  OortObject.Merger, List> listUnion() {
        return new ListUnionMerger<>();
    }

    private static class LongSumMerger implements OortObject.Merger {
        @Override
        public Long merge(Collection> infos) {
            long sum = 0;
            for (OortObject.Info info : infos) {
                sum += info.getObject();
            }
            return sum;
        }
    }

    private static class MapUnionMerger implements OortObject.Merger, Map> {
        @Override
        public Map merge(Collection>> infos) {
            Map result = new HashMap<>();
            for (OortObject.Info> value : infos) {
                result.putAll(value.getObject());
            }
            return result;
        }
    }

    private static class ConcurrentMapUnionMerger implements OortObject.Merger, ConcurrentMap> {
        @Override
        public ConcurrentMap merge(Collection>> infos) {
            ConcurrentMap result = new ConcurrentHashMap<>();
            for (OortObject.Info> value : infos) {
                result.putAll(value.getObject());
            }
            return result;
        }
    }

    public static class ListUnionMerger implements OortObject.Merger, List> {
        @Override
        public List merge(Collection>> infos) {
            List result = new ArrayList<>();
            for (OortObject.Info> value : infos) {
                result.addAll(value.getObject());
            }
            return result;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy