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

net.openhft.chronicle.map.MapInstanceBuilder Maven / Gradle / Ivy

There is a newer version: 3.27ea0
Show newest version
/*
 *     Copyright (C) 2015  higherfrequencytrading.com
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this program.  If not, see .
 */

package net.openhft.chronicle.map;

import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder;
import net.openhft.chronicle.hash.replication.ReplicationChannel;
import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicBoolean;

final class MapInstanceBuilder
        implements ChronicleHashInstanceBuilder>, Serializable {

    final ChronicleMapBuilder mapBuilder;
    transient SingleChronicleHashReplication singleHashReplication;
    transient ReplicationChannel channel;
    transient File file;
    String name;

    final AtomicBoolean used;

    MapInstanceBuilder(ChronicleMapBuilder mapBuilder,
                       SingleChronicleHashReplication singleHashReplication,
                       ReplicationChannel channel,
                       File file, String name, AtomicBoolean used) {
        this.mapBuilder = mapBuilder;
        this.singleHashReplication = singleHashReplication;
        this.channel = channel;
        this.file = file;
        this.name = name;
        this.used = used;
    }

    @Override
    public MapInstanceBuilder replicated(
            byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork) {
        return replicated(SingleChronicleHashReplication.builder()
                .tcpTransportAndNetwork(tcpTransportAndNetwork).createWithId(identifier));
    }

    @Override
    public MapInstanceBuilder replicated(SingleChronicleHashReplication replication) {
        singleHashReplication = replication;
        channel = null;
        return this;
    }

    @Override
    public MapInstanceBuilder replicatedViaChannel(ReplicationChannel channel) {
        singleHashReplication = null;
        this.channel = channel;
        return this;
    }

    @Override
    public MapInstanceBuilder persistedTo(File file) {
        this.file = file;
        return this;
    }

    @Override
    public MapInstanceBuilder name(String name) {
        this.name = name;
        return this;
    }

    @Override
    public synchronized ChronicleMap create() throws IOException {
        if (!used.getAndSet(true)) {
            return mapBuilder.create(this);
        } else {
            throw new IllegalStateException(
                    "A ChronicleMap has already been created using this instance config chain. " +
                            "Create a new instance config (builder.instance()) to create a new " +
                            "ChronicleMap instance");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy