001 /*--------------------------------------------------------------------------+ 002 $Id: ParameterizedElement.java 26285 2010-02-18 11:22:54Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.simulink.model; 019 020 import java.util.HashMap; 021 import java.util.HashSet; 022 import java.util.Set; 023 024 import edu.tum.cs.commons.collections.CollectionUtils; 025 import edu.tum.cs.commons.collections.UnmodifiableSet; 026 027 /** 028 * This class usually serves as base class for all classes that have a 029 * key-value-mechanism for parameters. It supports a default parameter mechanism 030 * that is often found withing the Simulink library. 031 * 032 * @author deissenb 033 * @author $Author: juergens $ 034 * @version $Rev: 26285 $ 035 * @levd.rating GREEN Hash: 42160170C7EA0692A401E240FBD54FE5 036 */ 037 public class ParameterizedElement { 038 039 /** The parameters map. */ 040 private final HashMap<String, String> parameters = new HashMap<String, String>(); 041 042 /** Create new element. */ 043 protected ParameterizedElement() { 044 // nothing to do 045 } 046 047 /** 048 * Creates new element from another parameterized element. This copies all 049 * parameters. 050 */ 051 protected ParameterizedElement(ParameterizedElement other) { 052 parameters.putAll(other.parameters); 053 } 054 055 /** 056 * Get parameter specified by name. This does <em>not</em> take default 057 * parameters into account. 058 */ 059 public String getDeclaredParameter(String name) { 060 return parameters.get(name); 061 } 062 063 /** 064 * Get parameter names. This does <em>not</em> take default parameters 065 * into account. 066 */ 067 public UnmodifiableSet<String> getDeclaredParameterNames() { 068 return CollectionUtils.asUnmodifiable(parameters.keySet()); 069 } 070 071 /** 072 * Get parameter specified by name. This takes default parameters into 073 * account. 074 */ 075 public String getParameter(String name) { 076 String value = parameters.get(name); 077 if (value != null) { 078 return value; 079 } 080 return getDefaultParameter(name); 081 } 082 083 /** 084 * Get the names of all parameters. This takes default parameters into 085 * account. 086 */ 087 public UnmodifiableSet<String> getParameterNames() { 088 if (getDefaultParameterNames().isEmpty()) { 089 return CollectionUtils.asUnmodifiable(parameters.keySet()); 090 } 091 HashSet<String> parametersNames = new HashSet<String>( 092 getDefaultParameterNames()); 093 parametersNames.addAll(parameters.keySet()); 094 return CollectionUtils.asUnmodifiable(parametersNames); 095 } 096 097 /** 098 * Add a parameter. 099 */ 100 public void setParameter(String name, String value) { 101 parameters.put(name.intern(), value.intern()); 102 } 103 104 /** 105 * Get default parameter. This implementation always returns 106 * <code>null</code>. 107 */ 108 /* package */String getDefaultParameter( 109 @SuppressWarnings("unused") String name) { 110 return null; 111 } 112 113 /** 114 * Get names of default parameters. This implementation always returns an 115 * empty set. 116 */ 117 /* package */Set<String> getDefaultParameterNames() { 118 return CollectionUtils.emptySet(); 119 } 120 }