Collections API update

I recently made a number of updates to my collections API which include some minor changes to the existing code as well as a few additional classes and interfaces which have been added.

The most significant updates involve three additional operations which have been added to the IMap interface which are as follows:

  • putAll(table:Dictionary) : void;
    Allows an Object or Dictionary instance of key / value pairs to be added to an IMap implementation.
  • putEntry(entry:IHashMapEntry) : void;
    Serves as a pseudo-overloaded implementation of the put(key:*, value:*); method in order to allow Strongly typed key / value implementations to be added to a map.
  • getEntries() : IList;
    Returns an IList of HashMapEntry objects based on all key/value pairs
  • I have also added two new additional IMap implementations to the collections API; LocalPersistenceMap, which can be utilized to provide an IMap implementation into a local SharedObject and ResourceMap which allows developers to work with a ResourceBundles via an IMap implementation. All IMap implementations; HashMap, ResourceMap and LocalPersistenceMap have been updated to implement the new operations.

    The IIterator interface has been renamed to Iterator. In addition the remove() operation has been omitted in order to enforce that a concrete implementations can not modify an aggregate. Additionally, ICollectionViewSortHelper has been renamed to CollectionSortUtil.

    Some design decisions worth mentioning involve the inclusion of multiple IMap implementations; HashMap, ResouceMap and LocalPersistanceMap. Initially I identified an AbstractMap from which these classes would extend, however I realize some developers may want to minimize dependencies as much as possible therefore I decided to simply have each concrete map implement the IMap interface rather than extend an abstract map implementation.

    The source, binary and ASDocs for the new Collections API can be downloaded here.

    The Collections API is published under the MIT license.

    Cairngen Project moved to Google Code!

    Since the initial release of Cairngen 1.0 there has been an amazing amount of interest in the project and your feedback has been very encouraging. In addition I have received a number of extremely valuable customizations from community members, many of which have made it into subsequent releases.

    In order to provide a solid foundation to help facilitate a collaborative Open Source initiative for the Cairngen Project I have decided to move the project to Google Code.

    Moving Cairngen to Google Code allows for a number of significant development benefits for the Cairngorm Community. This includes regular development updates, access to all releases, availability of all source code revisions, better documentation, defect lists, feature requests and much more.

    I am currently accepting requests for new contributors so if you feel you have added new functionality which would provide benefit to the project visit the Become a Contributor page. All comments on the project are welcome and encouraged. In addition I strongly recommend the if you have any issues with Cairngen, especially environmental issues you should log them to the Issues Page.

    Along with the new Cairngen Google Code Project I have also released Cairngen 2.1.1 which aligns the source project with the new SVN repository.

    So go check out all of the new information on Cairngen at the new Cairngen Project Home.

    Using multiline values in properties files

    When building localized Flex applications with ResourceBundle there are a few tricks you may not be aware of which can come in handy should you need them.

    I have had quite a few people ask how multiline values can be specified in a properties file. This is a pretty common question and luckily the answer is very simple: backslash (\).

    For example, suppose you have a properties file which contains a string resource with a really long value, like a paragraph. Typically the property value will just be one really long String on one line. However you can use the backslash (\) character to continue the value on multiple lines in order to make the properties file more legible, such as in the following example:

    So if you have properties with very long values remember to use backslash (\) to help clean up your resources.

    example

    Accessing private class members in AS3

    Reflective programming is a very common, powerful, yet expensive software development practice. In most modern programming languages reflection can not be utilized to modify private members of an object, which is to be expected. However in certain circumstances, such as when developing very high level frameworks and automation APIs it may be beneficial at times to be have the ability to access private members of an object from outside of the public API of the class.

    For instance, consider the following class definition:

    The above class defines two private members: _username and _password. The public API of this class exposes these properties via the read-only accessors get username and get password. Per design assignments to these properties can only be made via the instance constructor.

    So how can these values be modified once the object has been instantiated? More importantly, should this be allowed as the properties have not been publicly exposed? Theoretically they can not, philosophically they shouldn’t. All of this is debatable; however as a dynamic programming language ActionScript can easily accomplish accessing and modifying private members of an object which have not been exposed.

    To do so is simple: define a method which provides an interface into the private members of the class instance, as in the following example:

    The above example is pretty straightforward. It is simply a static method which requires 3 arguments. The first being the instance of the class, the second is the property in which to assign a new value, and the third is the value to assign to the property. This static method can be defined in any class and will work fine – you only need to change the type of the instance argument and error message to match the type of the class in which the method is defined. All methods in a class definition have access to any member of the class therefore assignments can be made to an instance of the class via a static method. This is possible in most object oriented languages such as C# and Java, however ActionScript takes this approach into the dynamic realm thus making it much more robust as the actual properties which are to be modified can be specified at runtime.

    This approach can just as easily be accomplished on the instance level as well via an instance method, as in the following:

    A client implementation for each would be as follows:

    This approach is best suited for assigning values to simple types, or for pointing reference types to other object instances. This does not allow nested properties of a member to be accessed or modified (although it could easily be refactored to do so). Most importantly, (as you may be thinking) this breaks encapsulation and violates information hiding; therefore I do not recommend utilizing this approach for general use, however it is a convenient and efficient way to modify objects when developing complex APIs which use reflection and also for implementing Unit Tests directly against private members.

    Initially I was introduced to the static implementation of this technique by a .Net developer on my team, and admittedly I was somewhat skeptical at first, however given the dynamic nature of ActionScript I realized this could open up some interesting possibilities under unique circumstances, similar to how mixins open up many possibilities – when used with discretion in a good design they both provide powerful advantages over other languages.

    Food for thought. Enjoy.

    LocalPersistenceMap

    When developing browser based Flex applications, SharedObject provides just about everything one needs to facilitate local persistence of application data. While being somewhat restricted by certain rules governed by the Flash Player Security Model, in general, it provides a rather simple solution for most needs.

    The data property of a SharedObject provides read/write access to the underlying data which is persisted to the SharedObject. Personally, I prefer to have a consistent API available when working with dynamic objects, thus I developed the IMap interface.

    LocalPersistenceMap provides an IMap implementation into the data property of a SharedObject instance. It allows clients to work with the underlying data of a SharedObject just as one would with a HashMap, ResourceMap, etc.

    Below is a basic example which demonstrates how LocalPersistenceMap can be utilized to provide an IMap implementation into a SharedObject:

    The LocalPersistenceMap constructor creates a reference to a local SharedObject based on the specified identifier and optional local path. A reference to the underlying SharedObject can also be retrieved via the sharedObjectInstance accessor.

    In addition to the new LocalPersistenceMap, I have also packaged the Collections API which can be downloaded here. Complete documentation and code examples for the Collections API are also available here.

    Embedding assets with application/octet-stream

    Awhile back one of the guys on my team discovered an interesting way to embed files in ActionScript using the “application/octet-stream” mimeType with the Embed metadata tag.

    When using the Embed tag it is not common to explicitly assign a specific mimeType to an asset. Because of this Flex uses heuristics to determine the appropriate mimeType based on the extension of the embedded asset (i.e. file).

    The Adobe Flex documentation states:

    “You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.”

    However when specifying application/octet-stream (which is essentially an arbitrary byte stream ) as the assets mimeType it is also possible to embed a file of any type. If the file type is not supported natively, such as XML, developers can write custom parsers which can read the file utilizing the ByteArray (Flex 2) or ByteArrayAsset (Flex 3).

    Below I have provided a basic example which demonstrates how the application/octet-stream mimeType can be utilized to embed a file, in this case an external xml document which serves as a config file.

    In the above example an XML document is embedded as a Class object from which the contents of the file are read as a string via ByteArrayAsset and converted to an XML object.

    Based on this example it would also be possible to create arbitrary custom types in conjunction with custom parsers to allow additional support for numerous other file types in Flex as well.