001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.util.mappings; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023/** 024 * Class that wraps a {@link Mapping} around a {@link java.util.Map}. 025 * 026 * @author Malte Isberner 027 * 028 * @param <D> domain type. 029 * @param <R> range type. 030 */ 031public class MapMapping<D, R> implements MutableMapping<D, R> { 032 033 public static <D,R> MapMapping<D,R> create(Map<D,R> map) { 034 return new MapMapping<D,R>(map); 035 } 036 037 038 private final Map<? super D, R> map; 039 040 public MapMapping(Map<D,R> map, boolean copy) { 041 if(!copy) 042 this.map = map; 043 else 044 this.map = new HashMap<D,R>(); 045 } 046 047 /** 048 * Constructor. 049 * @param map the underlying {@link java.util.Map} object. 050 */ 051 public MapMapping(Map<? super D,R> map) { 052 this.map = map; 053 } 054 055 public MapMapping() { 056 this(new HashMap<D,R>()); 057 } 058 059 /* 060 * (non-Javadoc) 061 * @see edu.udo.ls5.util.Mapping#get(java.lang.Object) 062 */ 063 @Override 064 public R get(D elem) { 065 return map.get(elem); 066 } 067 068 /** 069 * Delegates to the underlying {@link java.util.Map}. 070 * @see java.util.Map#put(Object, Object) 071 */ 072 @Override 073 public R put(D key, R value) { 074 return map.put(key, value); 075 } 076 077 /** 078 * Delegates to the underlying {@link java.util.Map}. 079 * @see java.util.Map#entrySet() 080 */ 081 public Set<? extends Map.Entry<? super D,R>> entrySet() { 082 return map.entrySet(); 083 } 084}