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

net.vectorpublish.desktop.vp.ui.Namespace Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016, Peter Rader. All rights reserved.
 *  ___ ___               __                 ______         __     __  __         __
 * |   |   |.-----..----.|  |_ .-----..----.|   __ \.--.--.|  |--.|  ||__|.-----.|  |--.
 * |   |   ||  -__||  __||   _||  _  ||   _||    __/|  |  ||  _  ||  ||  ||__ --||     |
 *  \_____/ |_____||____||____||_____||__|  |___|   |_____||_____||__||__||_____||__|__|
 *
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
package net.vectorpublish.desktop.vp.ui;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

public class Namespace {
	private static final Set NAMESPACES = new LinkedHashSet<>();

	public static final String REGEX_PATTERN = "[a-z0-9\\.\\-\\:]+";
	private final String foreseeable;
	private final String inconceivable;

	private Namespace() {
		throw new AssertionError("Shall not be constructed via constructor!");
	}

	public static Namespace getNamespace(String foreseeable, String inconceivable) {
		for (Namespace namespace : NAMESPACES) {
			if (namespace.foreseeable.equals(foreseeable) && namespace.inconceivable.equals(inconceivable)) {
				return namespace;
			}
		}
		synchronized (NAMESPACES) {
			for (Namespace namespace : NAMESPACES) {
				if (namespace.foreseeable.equals(foreseeable) && namespace.inconceivable.equals(inconceivable)) {
					return namespace;
				}
			}
			return new Namespace(foreseeable, inconceivable);
		}
	}

	private Namespace(final String foreseeable, final String inconceivable) {
		this.foreseeable = Objects.requireNonNull(foreseeable);
		this.inconceivable = Objects.requireNonNull(inconceivable);
		if (!toString().matches("^" + REGEX_PATTERN + "$")) {
			throw new IllegalArgumentException("The canonical name ('" + toString() + "') does not match the pattern '" + REGEX_PATTERN + "'.");
		}
		NAMESPACES.add(this);
	}

	public String getConfigNamespace() {
		return inconceivable;
	}

	@Override
	public String toString() {
		return foreseeable + ":" + inconceivable;
	}

	/**
	 * Please use == instead.
	 * 
	 * @see Object#equals(Object)
	 */
	@Override
	@Deprecated
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy