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

org.coos.messaging.routing.LinkCost Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * 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 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 .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.routing;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.Serializable;

import org.coos.messaging.Link;

/**
 * @author Knut Eilif Husa, Tellu AS This class holds the link information used
 *         by the routing algorithm
 */
public class LinkCost implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5695860173037233991L;
	private String fromUuid;
	private String toUuid;
	private List aliases = new LinkedList();
	private Map costMap = new HashMap();
	private boolean link = false;
	private LinkCost nextLink;
	private String linkId;
	private transient long timeStamp;
	public static final int MAX_VALUE = 10000000;

	public LinkCost(String fromUri, String toUri, String linkId, int cost) {
		this.fromUuid = fromUri;
		this.toUuid = toUri;
		this.linkId = linkId;
		if (cost >= MAX_VALUE) {
			link = false;
		} else {
			link = true;

		}
		this.costMap.put(Link.DEFAULT_QOS_CLASS, cost);
		this.timeStamp = System.currentTimeMillis();
		
	}

    public LinkCost(String fromUri, String toUri, String linkId, Map costMap, List aliases) {
		this.fromUuid = fromUri;
		this.toUuid = toUri;
		this.linkId = linkId;
		this.costMap.putAll(costMap);
		if (costMap.containsValue(MAX_VALUE)) {
			link = false;
		} else {
			link = true;
		}
		if (aliases != null && !aliases.isEmpty()) {
			this.aliases = aliases;
		}
		this.timeStamp = System.currentTimeMillis();
	}

	public String getKey() {
		StringBuffer buf = new StringBuffer();
		buf.append(fromUuid);
		buf.append(",");
		buf.append(toUuid);
		return buf.toString();
	}

	public LinkCost(LinkCost linkCost) {
		fromUuid = linkCost.getFromUuid();
		toUuid = linkCost.getToUuid();
		costMap = Collections.synchronizedMap(new HashMap(linkCost.costMap));
		link = linkCost.link;
		nextLink = linkCost.getNextLink();
		linkId = linkCost.getLinkId();
		aliases = new LinkedList(linkCost.getAliases());
		this.timeStamp = System.currentTimeMillis();
	}

	public List getAliases() {
		return aliases;
	}

	public long getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(long timeStamp) {
		this.timeStamp = timeStamp;
	}

	public Set getQoSClasses() {
		return costMap.keySet();
	}

	public String getFromUuid() {
		return fromUuid;
	}

	public void setFromUuid(String fromUuid) {
		this.fromUuid = fromUuid;
	}

	public String getToUuid() {
		return toUuid;
	}

	public void setToUuid(String toUuid) {
		this.toUuid = toUuid;
	}

	public int getCost(String qos) {
		if (!link) {
			return MAX_VALUE;
		}
		Integer cost = costMap.get(qos);
		if (cost == null) {
			return 0;
		} else {
			return cost.intValue();
		}
	}

	public void setCost(String qos, int cost) {
		if (cost < MAX_VALUE) {
			link = true;
		}
		costMap.put(qos, cost);
	}

	public LinkCost getNextLink() {
		return nextLink;
	}

	public void setNextLinkCost(LinkCost nextLink) {
		this.nextLink = nextLink;
	}

	public String getLinkId() {
		return linkId;
	}

	public void setLinkId(String linkId) {
		this.linkId = linkId;
	}

	public void setLink(boolean link) {
		this.link = link;
	}

	public boolean isLink() {
		return link;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		LinkCost linkCost = (LinkCost) o;

		if (!fromUuid.equals(linkCost.fromUuid))
			return false;
		if (!linkId.equals(linkCost.linkId))
			return false;
		if (!toUuid.equals(linkCost.toUuid))
			return false;

		return true;
	}

	public int hashCode() {
		int result;
		result = fromUuid.hashCode();
		result = 31 * result + toUuid.hashCode();
		result = 31 * result + linkId.hashCode();
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy