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

org.openrdf.repository.object.managers.helpers.SimpleRoleMapper Maven / Gradle / Ivy

Go to download

The Object Composition library merges multiple Java objects into a single multi-subject object.

There is a newer version: 2.4
Show newest version
/*
 * Copyright (c) 2007, James Leigh All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution. 
 * - Neither the name of the openrdf.org nor the names of its contributors may
 *   be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package org.openrdf.repository.object.managers.helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Tracks types and maps them to their assigned roles.
 * 
 * @author James Leigh
 * 
 */
public class SimpleRoleMapper implements Cloneable {

	/** http://www.w3.org/2000/01/rdf-schema#Resource */
	private static final String BASE_TYPE = "http://www.w3.org/2000/01/rdf-schema#Resource";

	private final Logger logger = LoggerFactory.getLogger(SimpleRoleMapper.class);

	private URI baseType;

	private boolean empty = true;

	private Map>> roles; // javancss cannot parse Map[]>

	private Map unregisteredTypes = new ConcurrentHashMap();

	public SimpleRoleMapper() {
		roles = new ConcurrentHashMap>>(256);
	}

	public SimpleRoleMapper clone() {
		try {
			SimpleRoleMapper cloned = (SimpleRoleMapper) super.clone();
			cloned.roles = clone(roles);
			return cloned;
		} catch (CloneNotSupportedException e) {
			throw new AssertionError(e);
		}
	}

	private  Map> clone(Map> map) {
		Map> cloned = new HashMap>(map);
		for (Map.Entry> e : cloned.entrySet()) {
			e.setValue(new ArrayList(e.getValue()));
		}
		return cloned;
	}

	public void setURIFactory(ValueFactory vf) {
		baseType = vf.createURI(BASE_TYPE);
		List> list = Collections.emptyList();
		roles.put(baseType, list);
	}

	public URI getBaseType() {
		return baseType;
	}

	public Collection> findAllRoles() {
		List> list = new ArrayList>(roles.size());
		for (List> v : roles.values()) {
			list.addAll(v);
		}
		return list;
	}

	public Collection> findRoles(URI type) {
		List> classes = roles.get(type);
		if (classes == null) {
			unregistered(type);
			return findBaseRoles();
		}
		return classes;
	}

	public Collection> findRoles(Collection types,
			Collection> classes) {
		boolean found = false;
		for (URI type : types) {
			List> javaClass = roles.get(type);
			if (javaClass == null) {
				unregistered(type);
			} else {
				found = true;
				classes.addAll(javaClass);
			}
		}
		if (!found) {
			classes.addAll(findBaseRoles());
			return classes;
		}
		return classes;
	}

	public boolean isNamedTypePresent() {
		return !empty;
	}

	public boolean isTypeRecorded(URI type) {
		return roles.containsKey(type);
	}

	public synchronized Set> recordRoles(Set> role, URI uri) {
		List> set = roles.get(uri);
		Set> changed = new HashSet>();
		if (set == null) {
			List> bar = roles.get(baseType);
			if (bar == null) {
				changed = role;
			} else {
				changed.addAll(bar);
				changed.addAll(role);
			}
		} else {
			changed.addAll(set);
			changed.addAll(role);
		}
		if (set == null || changed.size() != set.size()) {
			empty &= uri.equals(baseType);
			roles.put(uri, Arrays.asList(changed.toArray(new Class[changed.size()])));
		}
		return changed;

	}

	public synchronized void recordBaseRole(Class role) {
		for (Map.Entry>> e : roles.entrySet()) {
			List> set = e.getValue();
			boolean contains = false;
			for (Class c : set) {
				if (role.equals(c)) {
					contains = true;
					break;
				}
			}
			if (contains)
				continue;
			List> ar = new ArrayList>(set.size() + 1);
			ar.addAll(set);
			ar.add(role);
			e.setValue(ar);
		}
	}

	private Collection> findBaseRoles() {
		return roles.get(baseType);
	}

	private void unregistered(URI type) {
		if (!unregisteredTypes.containsKey(type)) {
			unregisteredTypes.put(type, Boolean.TRUE);
			logger.info("Unregistered type {}", type);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy