// $Id$ // // Copyright 2007-2008 Cisco Systems Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. using System; using System.Collections.Generic; using Etch.Msg; namespace Etch.Support { public class Class2TypeMap { /// /// /// /// /// the Type for the specified class, or null public XType Get( Type c ) { try { return c2t[ c ]; } catch ( KeyNotFoundException ) { return null; } } /// /// Adds a map entry from c to t /// /// /// public void Add( Type c, XType t ) { if ( locked ) throw new Exception( "locked" ); XType x; try { x = c2t[ c ]; } catch ( KeyNotFoundException ) { x = null; } if ( x != null ) { if ( !x.Equals( t ) ) throw new ArgumentException( String.Format( "type {0} : class {1} is already mapped to type {2}", t, c, x ) ); return; } c2t.Add( c, t ); } /// /// Adds all the mappings from other to this /// /// public void AddAll( Class2TypeMap other ) { if ( locked ) throw new Exception( "locked" ); foreach ( KeyValuePair me in other.c2t ) { Add( me.Key, me.Value ); } } /// /// Locks the map, disallowing any more changes /// public void Lock() { locked = true; } private readonly Dictionary c2t = new Dictionary(); private Boolean locked; } }