COMP 310
|
Lec40: Mixed Data Dictionaries, WWJ Mapping Library and the map package |
![]() ![]() ![]() ![]() ![]() ![]() |
The problem with dictionaries is that they are typed to fixed <key, value> types. One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary. This causes two main problems with type-safety in the application:
What we'd really like a dictionary that enables us to store mixed types of data safely together. The problem is that Java's type erasure causes the type information to be lost at runtime.
The soluton is thus to encode the key with the type information of the data to which it refers. Then, the dictionary can a) restrict any data being stored to only the type or subtype of that the key specifies and b) because of a), can unequivocally retrieve the data with the specific type as defined by the key being used.
See the documentation for the mixedData package.
While a MixedDataKey is Serializable, it is often more practical to build the key when and where it is used by using the three values needed to specify the key instead of sending a serialized key instance.
Here's a breakdown of the information needed to generate a key. These three values completely and uniquely determine a key, that is, any MixedDataKey instance made with the same three values will be equal.
UUID id
= This value insures that
the key will be unique from all other keys with the same two other values.
In a typical scenario, all the keys in use share the same id value.
For instance, a single randomly-generated UUID value could be used
to uniquely identify the instance of the Game Server. The Game
Server would then give that common UUID value to all commands it
sends out so that they would be tied to that instance of the Game Server. String desc
= This is a
"human-friendly" name that you can use to identify the particular value in
the dictionary to which this key maps, e.g. "MapView adapter". This
helps you keep track of what each key is for. In practice, this is the
main part of the key that one uses to differentiate one value from another
in the dictionary.Class<?> type
= The is the Java
type of the value to which this key maps in the dictionary. Only values of
this type can be associated with this key. While it is technically possible
to use the same description above for different value types, it is not
recommended because of the semantic confusion it would cause. Technically,
this type specification duplicates the generic typing of the
MixedDataKey itself but is needed in Java due to type erasure; in
run-time generics languages such as C#, this parameter is not needed at all.Type-erasure Problems when Storing Generic Types in a Mixed-Data Dictionary (MDD)
It turns out that saving generic types to the MDD is problematic due to Java's type-erasure (boo, hiss!).
In particular, type erasure prevents on from getting the class object of a
generic type, e.g. suppose one has a generic class, MyClass<T>
, then Java won't
let you get the class object for it, e.g. MyClass<SomeClass>.class
will not compile. This is
because at run-time, only the raw MyClass
exists, not the fully generic type.
Thus, we cannot make the MDD key that we want because the key requires the
corresponding Class
object for the data being stored.
The type-safe work-around is to create a custom, non-generic subclass for the desired type, e.g:
class
MyCustomClass extends MyClass<SomeClass> {
}
Now, MyCustomClass.class
is a perfectly valid operation and
creates the Class
object we want for the MDD key.
Notes:
Class
object at all is
because of type-erasure!DataPackets
of generic types.
Please use a message-passing architecture for game server to game client communications -- one of the main points of the final project is to practice thinking in these terms.
Today we will discuss the new World Wind Java mapping library as well as any design issues that have come up over the break. Here are some links to WWJ references and demos:
A WWJ demo app is available: simply check out the following project: https://svn.rice.edu/r/comp310/course/FinalProjec/FXX/WWJ_Demo (FXX = F14 for Fall 2014, for example)
Word of advice: Those who accomplish more, earlier, will have first dibs at suggesting and effecting common API changes that would naturally benefit their game the most! Stragglers will be forced to modify their code to accomodate other people's desires.
The supplied map package provides some basic WWJ map capabilities such as
See the documentation for the map package.
© 2016 by Stephen Wong