
net.sf.opendse.model.Mappings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendse-model Show documentation
Show all versions of opendse-model Show documentation
The algorithms module of OpenDSE
/**
* OpenDSE 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.
*
* OpenDSE 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 OpenDSE. If not, see http://www.gnu.org/licenses/.
*/
package net.sf.opendse.model;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections15.Bag;
import org.apache.commons.collections15.bag.HashBag;
import org.apache.commons.collections15.functors.InstantiateFactory;
import org.apache.commons.collections15.map.LazyMap;
/**
* The {@code Mappings} represent a set of {@link Mapping} elements.
*
* @author Martin Lukasiewycz
*
* @param
* the type of tasks
* @param
* the type of resources
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Mappings implements Iterable> {
protected Set> mappings = new HashSet>();
protected Map>> taskMappings = LazyMap.decorate(new HashMap>>(),
new InstantiateFactory(HashSet.class));
protected Map>> resourceMappings = LazyMap.decorate(new HashMap>>(),
new InstantiateFactory(HashSet.class));
protected Map> targets = LazyMap
.decorate(new HashMap>(), new InstantiateFactory(HashBag.class));
protected Map> sources = LazyMap
.decorate(new HashMap>(), new InstantiateFactory(HashBag.class));
/**
* Adds a mapping.
*
* @param mapping
* the mapping to be added
*/
public void add(Mapping mapping) {
mappings.add(mapping);
T source = mapping.getSource();
R target = mapping.getTarget();
Set> s0 = taskMappings.get(source);
Set> s1 = resourceMappings.get(target);
s0.add(mapping);
s1.add(mapping);
Bag ts = targets.get(source);
Bag ss = sources.get(target);
ts.add(target);
ss.add(source);
}
/**
* Removes a mapping
*
* @param mapping
* the mapping to be removed
* @return {@code true} if the mapping was removed
*/
public boolean remove(Mapping mapping) {
boolean exist = mappings.remove(mapping);
if (exist) {
T source = mapping.getSource();
R target = mapping.getTarget();
Set> s0 = taskMappings.get(source);
Set> s1 = resourceMappings.get(target);
s0.remove(mapping);
s1.remove(mapping);
Bag ts = targets.get(source);
Bag ss = sources.get(target);
ts.remove(target);
ss.remove(source);
}
return exist;
}
public boolean removeAll(Collection> mappings){
boolean b = false;
for(Mapping mapping: mappings){
b |= remove(mapping);
}
return b;
}
/*
* (non-Javadoc)
*
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator> iterator() {
return mappings.iterator();
}
/**
* Returns the number of mappings.
*
* @return the number of mappings
*/
public int size() {
return mappings.size();
}
/**
* Returns all mappings with the specified source.
*
* @param source
* the source
* @return all mappings with the specified source
*/
public Set> get(T source) {
return Collections.unmodifiableSet(taskMappings.get(source));
}
/**
* Returns all targets for a source.
*
* @param source
* the source
* @return all targets
*/
public Set getTargets(T source) {
return targets.get(source).uniqueSet();
}
/**
* Returns all mappings with the specified target.
*
* @param target
* the target
* @return all mappings with the specified target
*/
public Set> get(R target) {
return Collections.unmodifiableSet(resourceMappings.get(target));
}
/**
* Returns all sources for a target.
*
* @param target
* the target
* @return all sources
*/
public Set getSources(R target) {
return sources.get(target).uniqueSet();
}
/**
* Returns all mappings with the specified source and target.
*
* @param source
* the source
* @param target
* the target
* @return the mappings with the specified source and target
*/
public Set> get(T source, R target) {
Set> set = new HashSet>(get(source));
set.retainAll(get(target));
return Collections.unmodifiableSet(set);
}
/**
* Returns all mappings as a set.
*
* @return all mappings as a set
*/
public Set> getAll() {
return Collections.unmodifiableSet(mappings);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy