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

org.coos.messaging.util.UuidHelper Maven / Gradle / Ivy

/**
 * 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.util;


/**
 * @author Knut Eilif Husa, Tellu AS 
 * Helper class for parsing UUIDs
 */
public class UuidHelper {

	/*
	 * This method returns the segment part from a endpointName/alias or an endpointUuid
	 * Must only be used for qualified endpoint identificators
	 */
	public static String getSegmentFromEndpointNameOrEndpointUuid(String segmentedUuid) {
		int idx = segmentedUuid.lastIndexOf('.');
		if (idx > 0) {
			String seg = segmentedUuid.substring(0, idx);
			/*if(!seg.startsWith(".")){
				seg = "." + seg;
			}*/
			return seg;
		} else {
			return ".";
		}
	}
	
	/*
	 * This method returns the segment part from an endpointUuid or segment
	 * Must not be used on endpointNames/Aliases
	 */
	public static String getSegmentFromSegmentOrEndpointUuid(String segmentOrEndpointUuid) {
		if(isSegment(segmentOrEndpointUuid)) {
			return segmentOrEndpointUuid;
		} else {
			return getSegmentFromEndpointNameOrEndpointUuid(segmentOrEndpointUuid);
		}
	}
	
	
	/*
	 * Checks if the alias is valid for for an segmented endpointUuid
	 */
	public static boolean isValidAliasForUuid(String alias, String segmentedUuid){
		String aliasSegment = getSegmentFromEndpointNameOrEndpointUuid(alias);
		if(aliasSegment.equals("dico") || aliasSegment.equals("localcoos")){
			return true;
		}
		String segment = getSegmentFromEndpointNameOrEndpointUuid(segmentedUuid);
		if(aliasSegment.equals(segment)){
			return true;
		}
		return false;
	}

	public static String getParentSegment(String segmentedUuid) {
		String segment = getSegmentFromSegmentOrEndpointUuid(segmentedUuid);
		if (segment.equals(".")) {
			return ""; // segementedUuid on top level
		}
		int idx = segment.lastIndexOf('.');
		if (idx > 0) {
			return segmentedUuid.substring(0, idx);
		} else {
			return ".";
		}
	}
	
	public static String getQualifiedUuid(String uuid){
		if (uuid.indexOf("UUID-") == -1) {
			return uuid; //Not an Uuid, returning input parameter
		}
		if (uuid.startsWith("UUID-")){
			uuid = "."+uuid;
		}
		return uuid;
	}

	public static boolean isUuid(String segmentedUuid) {
		if (segmentedUuid == null) {
			return false;
		}
		if (segmentedUuid.indexOf("UUID-") != -1 ) {
			return true;
		} else {
			return false;
		}

	}

	public static boolean isRouterUuid(String segmentedUuid) {
		if (segmentedUuid == null) {
			return false;
		}
		if (segmentedUuid.indexOf("UUID-R-") != -1) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isSegment(String segmentedUuid) {
		if (segmentedUuid == null) {
			return false;
		}
		return segmentedUuid.indexOf("UUID-") == -1;
	}

	public static String replaceSegment(String uuid, String segmentName) {
		if (isSegment(uuid)) {
			return segmentName;
		} else {
			int idx = uuid.lastIndexOf('.');
			if (idx != -1) {
				if (segmentName.equals(".")) {
					return uuid.substring(idx);
				} else {
					return segmentName + uuid.substring(idx);
				}
			} else {
				return segmentName;
			}
		}
	}

	/*
	 * Checks whether segments is in a parent child relation 
	 */
	public static boolean isInParentChildRelation(String seg1, String seg2) {
		
		return (getParentSegment(seg1).equals(seg2)|| getParentSegment(seg2).equals(seg1));
				
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy