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

org.btrplace.model.view.network.DefaultLinkBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright  2020 The BtrPlace Authors. All rights reserved.
 * Use of this source code is governed by a LGPL-style
 * license that can be found in the LICENSE.txt file.
 */

package org.btrplace.model.view.network;

import org.btrplace.model.PhysicalElement;

import java.util.BitSet;

/**
 * Default implementation of {@link LinkBuilder}.
 * Use a BitSet to generate identifiers and avoid duplication.
 *
 * @author Vincent Kherbache
 */
public class DefaultLinkBuilder implements LinkBuilder {

    private BitSet usedIds;
    private int nextId;

    /**
     * New Builder.
     */
    public DefaultLinkBuilder() {
        usedIds = new BitSet();
    }

    @Override
    public Link newLink(int id, int capacity, Switch sw, PhysicalElement pe) {
        if (!usedIds.get(id)) {
            usedIds.set(id);
            nextId = Math.max(nextId, id + 1);
            return new Link(id, capacity, sw, pe);
        }
        return null;
    }

    @Override
    public Link newLink(int capacity, Switch sw, PhysicalElement pe) {
        int id = nextId++;
        if (id < 0) {
            //We look for holes in the bitset
            id = usedIds.nextClearBit(0);
        }
        usedIds.set(id);
        return new Link(id, capacity, sw, pe);
    }

    @Override
    public Link newLink(Switch sw, PhysicalElement pe) {
        return newLink(-1, sw, pe); // Infinite bandwidth (testing purpose)
    }

    @Override
    public boolean contains(Link l) {
        return usedIds.get(l.id());
    }

    @Override
    public LinkBuilder copy() {
        DefaultLinkBuilder sb = new DefaultLinkBuilder();
        sb.nextId = nextId;
        sb.usedIds = (BitSet) usedIds.clone();
        return sb;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy