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

net.automatalib.commons.util.mappings.Mappings Maven / Gradle / Ivy

Go to download

Basic utility library containing several useful classes (e.g., a Pair class) to ease everyday programming.

There is a newer version: 0.11.0
Show newest version
/* Copyright (C) 2013 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 * 
 * AutomataLib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 3.0 as published by the Free Software Foundation.
 * 
 * AutomataLib 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 AutomataLib; if not, see
 * http://www.gnu.de/documents/lgpl.en.html.
 */
package net.automatalib.commons.util.mappings;

import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Collection of various methods dealing with {@link Mapping}s.
 * 
 * @author Malte Isberner 
 *
 */
public abstract class Mappings {
	
	private static final Mapping NULL_MAPPING = new Mapping() {
		@Override
		public Object get(Object elem) {
			return null;
		}
	};
	
	private static final Mapping IDENTITY_MAPPING = new Mapping() {
		@Override
		public Object get(Object elem) {
			return elem;
		}
	};
	
	private static final Mapping TOSTRING_MAPPING = new Mapping() {
		@Override
		public String get(Object elem) {
			return String.valueOf(elem);
		}
	};
	
	
	/**
	 * Retrieves the null mapping, which maps each domain value
	 * to null.
	 * @param  domain class.
	 * @param  range class.
	 * @return the null mapping.
	 */
	@SuppressWarnings("unchecked")
	public static  Mapping nullMapping() {
		return (Mapping)NULL_MAPPING;
	}
	
	/**
	 * Retrieves the identity mapping, which maps each domain value
	 * to itself.
	 * @param  domain/range class.
	 * @return the identity mapping.
	 */
	@SuppressWarnings("unchecked")
	public static  Mapping identity() {
		return (Mapping)IDENTITY_MAPPING;
	}
	
	/**
	 * Returns a mapping that maps objects to their {@link String} representation,
	 * as obtained by {@link String#valueOf(Object)}.
	 * @return the "toString()" mapping
	 */
	@SuppressWarnings("unchecked")
	public static  Mapping toStringMapping() {
		return (Mapping)TOSTRING_MAPPING;
	}
	
	/**
	 * Returns a mapping that maps objects to a supertype representation.
	 * @return the "upcast" mapping
	 */
	@SuppressWarnings("unchecked")
	public static  Mapping upcast() {
		return (Mapping)IDENTITY_MAPPING;
	}
	
	/**
	 * Retrieves the composition of two mappings, i.e., that mapping that
	 * results from applying the {@link Mapping#get(Object)} method
	 * consecutively.
	 * @param  domain class of the first (and resulting) mapping.
	 * @param  intermediate object class, range class of the first and domain
	 * class of the second mapping.
	 * @param  range class of the second (and resulting) mapping.
	 * @param first first mapping.
	 * @param second second mapping.
	 * @return the composed mapping.
	 */
	public static  Mapping compose(Mapping first, Mapping second) {
		return new MappingComposition(first, second);
	}
	

	/**
	 * Applies a mapping to a collection, resulting in a collection containing
	 * the result of applying the specified mapping to each element in the
	 * collection.
	 * 
	 * Note that more specific properties of the specified collection won't
	 * be preserved: If the given collection is e.g. a set, and the provided
	 * mapping is not bijective, then the resulting collections may contain
	 * some values multiple times.
	 * 
	 * @param  domain class.
	 * @param  range class.
	 * @param mapping the mapping to apply.
	 * @param coll the collection.
	 * @return the mapped collection.
	 */
	public static  Collection apply(final Mapping mapping, final Collection coll) {
		return new AbstractCollection() {
			@Override
			public Iterator iterator() {
				return apply(mapping, coll.iterator());
			}

			@Override
			public int size() {
				return coll.size();
			}
		};
	}
	
	/**
	 * Applies a mapping to a list, resulting in a list containing the result
	 * of applying the specified mapping to each element in the list.
	 * 
	 * @param mapping the mapping to apply.
	 * @param list the list.
	 * @return the mapped list.
	 */
	public static  List apply(final Mapping mapping, final List list) {
		return new AbstractList() {
			/*
			 * (non-Javadoc)
			 * @see java.util.AbstractList#get(int)
			 */
			@Override
			public R get(int index) {
				return mapping.get(list.get(index));
			}

			/*
			 * (non-Javadoc)
			 * @see java.util.AbstractCollection#size()
			 */
			@Override
			public int size() {
				return list.size();
			}			
		};
	}
	
	/**
	 * Applies a mapping to an iterable. The result is an iterable whose
	 * iterator returns the results of applying the specified mapping
	 * to each of the elements returned by the original iterable.
	 * @param  domain class.
	 * @param  range clas.
	 * @param mapping the mapping to apply.
	 * @param it the underlying iterable.
	 * @return the mapped iterable.
	 */
	public static  Iterable apply(final Mapping mapping, final Iterable it) {
		return new Iterable() {
			@Override
			public Iterator iterator() {
				return apply(mapping, it.iterator());
			}
		};
	}
	
	/**
	 * Applies a mapping to an iterator. For the behavior, see
	 * {@link #apply(Mapping, Iterable)}. The resulting iterator supports
	 * each operation which the underlying supports.
	 * 
	 * @param  domain class.
	 * @param  range class.
	 * @param mapping the mapping to apply.
	 * @param baseIt the underlying iterator.
	 * @return the mapped iterator.
	 */
	public static  Iterator apply(Mapping mapping, Iterator baseIt) {
		return new MappedIterator(mapping, baseIt);
	}
	
	
	/**
	 * Safely retrieves a value from a mapping. If the mapping is
	 * null or returns a null value, the given
	 * fallback value is returned.  
	 * @param mapping the mapping.
	 * @param key the key.
	 * @param fallback the fallback value to return if either the mapping or the
	 * originally returned value are null.
	 * @return the value returned by the specified mapping, or the fallback value.
	 */
	public static  R safeGet(Mapping mapping, D key, R fallback) {
		if(mapping == null)
			return fallback;
		R val = mapping.get(key);
		if(val == null)
			return fallback;
		return val;
	}
	
	public static  D idGet(Mapping mapping, D key) {
		return safeGet(mapping, key, key);
	}
	
	public static  R nullGet(Mapping mapping, D key) {
		return safeGet(mapping, key, null);
	}
	
	public static  Mapping fromMap(Map map) {
		return new MapMapping(map);
	}

	
	
	/*
	 * Prevent inheritance.
	 */
	private Mappings() {
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy