1 ////////////////////////////////////////////////////////////////////////////////
2 // MillScript: an Open Spice interpreter and batch website creation tool
3 // Copyright (C) 2001-2004 Open World Ltd
4 //
5 // This file is part of MillScript.
6 //
7 // MillScript is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 of the License, or (at your option)
10 // any later version.
11 //
12 // MillScript is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License along with
18 // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 ////////////////////////////////////////////////////////////////////////////////
21 package org.millscript.millscript.datatypes;
22
23 import org.millscript.commons.util.IMap;
24 import org.millscript.commons.util.list.IStringList;
25 import org.millscript.commons.util.map.IJavaUtilMap;
26
27 import java.util.Map;
28
29 /**
30 * This is a utility class for constructing maps from objects.
31 */
32 public abstract class MapFactory {
33
34 /**
35 * Returns a map made from the specified object.
36 *
37 * @param obj the object to make a map from
38 * @return a Map made from the specified object
39 */
40 @SuppressWarnings( "unchecked" )
41 public static final IMap make( final Object obj ) {
42 if ( obj instanceof IMap ) {
43 // This handles MillScript-Util Maps and Lists(as a list is a map
44 // we can just carry on without having to do anything special)
45 return (IMap) obj;
46 } else if ( obj instanceof Map ) {
47 // This handles java.util.Maps for non MillScript compatibility
48 return new IJavaUtilMap< Object, Object >( (Map) obj );
49 } else if ( obj instanceof String ) {
50 // This handles Strings as Maps(char pos to Character, also a List)
51 return new IStringList( (String)obj );
52 } else if ( obj instanceof MapAware ) {
53 return ((MapAware)obj).asMap();
54 } else {
55 // Try to make a list for the specified object. As a list is a map
56 // we can just return it without having to do anything special.
57 return ListFactory.make( obj );
58 }
59 }
60
61 /**
62 * Hidden constructor.
63 */
64 private MapFactory() {
65 }
66
67 }