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

org.btrplace.model.SynchronizedElementBuilder Maven / Gradle / Ivy

/*
 * Copyright (c) 2016 University Nice Sophia Antipolis
 *
 * This file is part of btrplace.
 * This library 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, or (at your option) any later version.
 *
 * This library 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 org.btrplace.model;

/**
 * A wrapper for an {@link ElementBuilder} that makes it thread-safe.
 *
 * @author Fabien Hermenier
 */
public class SynchronizedElementBuilder implements ElementBuilder {

    private ElementBuilder base;

    private final Object vmLock;
    private final Object nodeLock;

    /**
     * Make a new builder.
     *
     * @param b the builder to wrap
     */
    public SynchronizedElementBuilder(ElementBuilder b) {
        this.base = b;
        vmLock = new Object();
        nodeLock = new Object();
    }

    @Override
    public VM newVM() {
        synchronized (vmLock) {
            return base.newVM();
        }
    }

    @Override
    public VM newVM(int id) {
        synchronized (vmLock) {
            return base.newVM(id);
        }
    }

    @Override
    public Node newNode() {
        synchronized (nodeLock) {
            return base.newNode();
        }
    }

    @Override
    public Node newNode(int id) {
        synchronized (nodeLock) {
            return base.newNode(id);
        }

    }

    @Override
    public boolean contains(VM v) {
        synchronized (vmLock) {
            return base.contains(v);
        }
    }

    @Override
    public boolean contains(Node n) {
        synchronized (nodeLock) {
            return base.contains(n);
        }
    }

    @Override
    public ElementBuilder copy() {
        return new SynchronizedElementBuilder(base);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy