[ERA-CVS] src/org/jdaemon/util/data MapEntryType.java,NONE,1.1 MapType.java,NONE,1.1 StringMapType.j
Brought to you by:
jessex
Update of /cvsroot/era/src/org/jdaemon/util/data In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14794 Modified Files: DataRepresentation.java ListType.java Types.java Added Files: MapEntryType.java MapType.java StringMapType.java Log Message: Map types - untested as yet --- NEW FILE: MapEntryType.java --- /* * ArrayType.java * * Copyright (C) 2002 Jonathan Essex * This program is distributed under the terms of the Lesser GNU General Public * License (v2 or later) per the included COPYING.txt file, or see www.fsf.org. * * Created on September 21, 2002, 2:35 PM */ package org.jdaemon.util.data; import java.util.Iterator; import java.util.TreeMap; import java.util.Map; /** Type object for Saving/Loading map entry objects from a DataRepresentation. * * @author Jonathan Essex */ class MapEntryType extends Type { private Type value_type; private Type key_type; /** Quick and dirty map entry implementation. */ private class MyMapEntry implements Map.Entry { /** Key attribute */ private final Object key; /** Value attribute */ private Object value; /** Get key attribute. * * @return value of key attribute */ public Object getKey() { return key; } /** Get value attribute. * * @return the value of the value attribute (!) */ public Object getValue() { return value; } /** Set the value attribute. * * @param obj new value for value attribute * @return old value of the value attribute */ public Object setValue(Object obj) { Object old = value; value = obj; return old; } /** Construct a new map entry. * * @param key value for key attribute * @param value value for value attribute */ public MyMapEntry(Object key, Object value) { this.key = key; this.value = value; } }; /** Creates a new instance of MapEntryType */ public MapEntryType(Type value_type, Type key_type) { super ("Entry", Map.Entry.class); this.value_type = value_type; this.key_type = key_type; } /** Reads a map entry from some DataRepresentation. * * @param representation Data representation from which we will read attributes and elements to create a new map entry * @throws ReadError Thrown when data cannot be read from <I>representation</I> * @throws ObjectInstantiationError Thrown when there is a problem creating a new instance of the required class * @return A new map entry containing key and value of the type given in the constructor of this object */ public Object read(DataRepresentation representation) throws ReadError, ObjectInstantiationError { return new MyMapEntry(key_type.get(representation, "key"), value_type.get(representation, "value")); } /** Writes a map entry to some DataRepresentation. * * @param representation Data representation to which we will write a new map entry * @param object map entry to be written to <I>representation</I> * @throws WriteError Thrown when data cannot be written to <I>representation</I> */ public void write(DataRepresentation representation, Object object) throws WriteError { key_type.put(representation, "key", ((Map.Entry)object).getKey()); value_type.put(representation, "value", ((Map.Entry)object).getValue()); } } --- NEW FILE: MapType.java --- /* * ArrayType.java * * Copyright (C) 2002 Jonathan Essex * This program is distributed under the terms of the Lesser GNU General Public * License (v2 or later) per the included COPYING.txt file, or see www.fsf.org. * * Created on September 21, 2002, 2:35 PM */ package org.jdaemon.util.data; import java.util.Iterator; import java.util.TreeMap; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; /** Type object for Saving/Loading maps from a DataRepresentation * * @author Jonathan Essex */ class MapType extends Type { private Type entry_type; /** Creates a new instance of MapType. * * @param key_type Type for key objects * @param value_type Type for value objects */ public MapType(Type key_type, Type element_type) { super ("Map", Map.class); this.entry_type = new MapEntryType(key_type, element_type); } /** Reads a map of objects from some DataRepresentation. * * @param representation Data representation from which we will read attributes and elements to create a new map * @throws ReadError Thrown when data cannot be read from <I>representation</I> * @throws ObjectInstantiationError Thrown when there is a problem creating a new instance of the required class * @return A new map containing key/value pairs of the type given in the constructor of this object */ public Object read(DataRepresentation representation) throws ReadError, ObjectInstantiationError { Iterator elements = representation.getElementKeys(); TreeMap map = new TreeMap(); while (elements.hasNext()) { Object key = elements.next(); Map.Entry elem = (Map.Entry)entry_type.get(representation, key); map.put(elem.getKey(), elem.getValue()); } return map; } /** Writes a map of objects to some DataRepresentation. * * @param representation Data representation to which we will write elements of the given map to create a new element * @param object array to be written to <I>representation</I> * @throws WriteError Thrown when data cannot be written to <I>representation</I> */ public void write(DataRepresentation representation, Object object) throws WriteError { Iterator i = ((Map)object).entrySet().iterator(); int count = 0; while (i.hasNext()) { entry_type.put(representation, new Integer(count), i.next()); count++; } } } --- NEW FILE: StringMapType.java --- /* * ArrayType.java * * Copyright (C) 2002 Jonathan Essex * This program is distributed under the terms of the Lesser GNU General Public * License (v2 or later) per the included COPYING.txt file, or see www.fsf.org. * * Created on September 21, 2002, 2:35 PM */ package org.jdaemon.util.data; import java.util.Iterator; import java.util.TreeMap; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; /** Type object for Saving/Loading maps from a DataRepresentation. * * This class deals with the special case where the map key is a string. Otherwise use MapType. * * @author Jonathan Essex */ class StringMapType extends Type { private Type value_type; /** Creates a new instance of StringMapType. * * @param value_type type of value contained by map (key is always a string). */ public StringMapType(Type value_type) { super ("StringMap", Map.class); this.value_type = value_type; } /** Reads a map of objects from some DataRepresentation. * * @param representation Data representation from which we will read attributes and elements to create a new map * @throws ReadError Thrown when data cannot be read from <I>representation</I> * @throws ObjectInstantiationError Thrown when there is a problem creating a new instance of the required class * @return A new map containing key/value pairs of the type given in the constructor of this object */ public Object read(DataRepresentation representation) throws ReadError, ObjectInstantiationError { Iterator elements = representation.getElementKeys(); TreeMap map = new TreeMap(); while (elements.hasNext()) { Object key = elements.next(); Object value = value_type.get(representation, key); map.put(key, value); } return map; } /** Writes a map of objects to some DataRepresentation. * * @param representation Data representation to which we will write elements of the given map to create a new element * @param object array to be written to <I>representation</I> * @throws WriteError Thrown when data cannot be written to <I>representation</I> */ public void write(DataRepresentation representation, Object object) throws WriteError { Iterator i = ((Map)object).entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry)i.next(); value_type.put(representation, entry.getKey(), entry.getValue()); } } } Index: DataRepresentation.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/data/DataRepresentation.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** DataRepresentation.java 24 Dec 2002 15:36:57 -0000 1.6 --- DataRepresentation.java 7 Jul 2004 14:41:29 -0000 1.7 *************** *** 33,37 **** * from attributes of the directory, and Elements are derived from sub-directories. * </P> ! * @author jonathan * @version */ --- 33,38 ---- * from attributes of the directory, and Elements are derived from sub-directories. * </P> ! * ! * @author Jonathan Essex * @version */ Index: ListType.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/data/ListType.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ListType.java 21 Jun 2004 15:15:20 -0000 1.1 --- ListType.java 7 Jul 2004 14:41:29 -0000 1.2 *************** *** 16,20 **** import java.util.ArrayList; ! /** Type object for Saving/Loading arrays from a DataRepresentation * * @author Jonathan Essex --- 16,20 ---- import java.util.ArrayList; ! /** Type object for Saving/Loading lists from a DataRepresentation. * * @author Jonathan Essex Index: Types.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/data/Types.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Types.java 23 Jun 2004 12:30:39 -0000 1.5 --- Types.java 7 Jul 2004 14:41:29 -0000 1.6 *************** *** 43,45 **** --- 43,65 ---- return new ListType(element_type); } + + /** Get a Type object for a map with keys and values of the types. + * + * @param key_type Type object for map keys + * @param value_type Type object for map values + * @return A type object representing a Map with the given key and value types + */ + public static Type mapOf(Type key_type, Type value_type) { + return new MapType(key_type, value_type); + } + + /** Get a Type object for a map of Strings to values of the given type + * + * @param value_type Type object for values in the map + * @return A Type object representing a map of strings to values of the given type + */ + public static Type mapOf(Type value_type) { + return new StringMapType(value_type); + } + } |