Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-874

Typesafe AttributeKeys

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.0.0-DEAD
    • 3.0.0-DEAD
    • Core
    • None

    Description

      In Mina 2 we can add attributes to a session and get them as well. Because IoSession#getAttribute(Object key) returns Object we always have to remember what attribute type is associated with the key and cast the result to the expected type.

      Mina 2 example:

      final static Object KEY = new AttributeKey(SourceClass.class,"myKey");
      ...
      session.set(KEY,"myAttribute");
      String attributeValue= (String)session.get(KEY);

      Instead of using plain Object-keys, the key type should contain information about its attributes. The aim is to get type-safe access to attributes.

      Assume we have the following new AttributeKey-Class:
      /**

      • @parmeter T Type of the referenced Attribute
        */
        class AttributeKey<T> {
        public TypesafeAttributeKey(Class<T> attributeType, String attributeKey) { ... }

        }

      The IoSession should have Attribute related accessors like these:

      void setAttribute(AttributeKey<T> key, T value);
      T getAttribute(AttributeKey<T> key);

      So in Mina 3 the example could look like this:

      final static AttributeKey<String> KEY = new AttributeKey<String>(String.class,"myKey");
      ...
      session.set(KEY,"myAttribute");
      String attributeValue=session.get(KEY);

      These 2 cases won't compile:

      session.set(KEY,new Date());
      Integer attributeValue=session.get(KEY);

      This pattern would simplify the use of Attributes, because the programmer don't have to care about the types and can concentrate on more improtant things. In my humble opinion the Objekt keys should be removed at all, i don't see the purpose for such unspecific keys.

      Attachments

        1. mina3.zip
          22 kB
          Christian Schwarz
        2. TypeSafeAttributes_v1_2011_12_16.patch
          74 kB
          Christian Schwarz

        Activity

          People

            Unassigned Unassigned
            christian_schwarz Christian Schwarz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: