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

com.hazelcast.jet.datamodel.ThreeBags Maven / Gradle / Ivy

There is a newer version: 4.5.4
Show newest version
/*
 * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 com.hazelcast.jet.datamodel;

import java.util.Objects;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;

import static java.util.Collections.emptyList;

/**
 * A container of three bags (collections), each with its own element
 * type. Bags are identified by their index: 0, 1, and 2. Useful as
 * a container of co-grouped data.
 *
 * @param  type of items in bag-0
 * @param  type of items in bag-1
 * @param  type of items in bag-2
 */
public final class ThreeBags {
    private final Collection bag0;
    private final Collection bag1;
    private final Collection bag2;

    private ThreeBags(@Nonnull Collection bag0, @Nonnull Collection bag1, @Nonnull Collection bag2) {
        this.bag0 = new ArrayList<>(bag0);
        this.bag1 = new ArrayList<>(bag1);
        this.bag2 = new ArrayList<>(bag2);
    }

    /**
     * Returns a new, empty {@code ThreeBags} container.
     */
    @Nonnull
    public static  ThreeBags threeBags() {
        return new ThreeBags<>(emptyList(), emptyList(), emptyList());
    }

    /**
     * Returns a new {@code ThreeBags} container populated with the supplied
     * collections. Doesn't retain the supplied collections, but
     * copies them into newly created ones.
     */
    @Nonnull
    public static  ThreeBags threeBags(
            @Nonnull Collection bag0, @Nonnull Collection bag1, @Nonnull Collection bag2
    ) {
        return new ThreeBags<>(bag0, bag1, bag2);
    }

    /**
     * Retrieves the bag at index 0.
     */
    @Nonnull
    public Collection bag0() {
        return bag0;
    }

    /**
     * Retrieves the bag at index 1.
     */
    @Nonnull
    public Collection bag1() {
        return bag1;
    }

    /**
     * Retrieves the bag at index 2.
     */
    @Nonnull
    public Collection bag2() {
        return bag2;
    }

    /**
     * Combines this and the supplied container by merging all the supplied
     * container's data into this one. Leaves the supplied container unchanged.
     */
    public void combineWith(@Nonnull ThreeBags that) {
        bag0.addAll(that.bag0());
        bag1.addAll(that.bag1());
        bag2.addAll(that.bag2());
    }

    /**
     * Deducts the supplied container from this one by removing all the items
     * that the supplied one contains. Leaves the supplied container unchanged.
     */
    public void deduct(@Nonnull ThreeBags that) {
        bag0.removeAll(that.bag0());
        bag1.removeAll(that.bag1());
        bag2.removeAll(that.bag2());
    }

    /**
     * Returns a safe copy of this container.
     */
    public ThreeBags finish() {
        return new ThreeBags<>(new ArrayList<>(bag0), new ArrayList<>(bag1), new ArrayList<>(bag2));
    }

    @Override
    public boolean equals(Object o) {
        ThreeBags that;
        return this == o ||
                o instanceof ThreeBags
                        && Objects.equals(this.bag0, (that = (ThreeBags) o).bag0)
                        && Objects.equals(this.bag1, that.bag1)
                        && Objects.equals(this.bag2, that.bag2);
    }

    @Override
    public int hashCode() {
        int hc = Objects.hashCode(bag0);
        hc = 73 * hc + Objects.hashCode(bag1);
        hc = 73 * hc + Objects.hashCode(bag2);
        return hc;
    }

    @Override
    public String toString() {
        return "ThreeBags{bag0=" + bag0 + ", bag1=" + bag1 + ", bag2=" + bag2 + '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy