Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.type;
  
  import java.util.*;
  
Type that represents Map-like types; things that consist of key/value pairs but that do not necessarily implement java.util.Map, but that do not have enough introspection functionality to allow for some level of generic handling. This specifically allows framework to check for configuration and annotation settings used for Map types, and pass these to custom handlers that may be more familiar with actual type.
 
 public class MapLikeType extends TypeBase
 {
     private static final long serialVersionUID = -4720638149668688793L;

    
Type of keys of Map.
 
     protected final JavaType _keyType;

    
Type of values of Map.
 
     protected final JavaType _valueType;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     protected MapLikeType(Class<?> mapTypeJavaType keyTJavaType valueT,
             Object valueHandlerObject typeHandler)
     {
         super(mapTypekeyT.hashCode() ^ valueT.hashCode(), valueHandlertypeHandler);
          = keyT;
          = valueT;
     }
     
     public static MapLikeType construct(Class<?> rawTypeJavaType keyTJavaType valueT)
     {
         // nominally component types will be just Object.class
         return new MapLikeType(rawTypekeyTvalueTnullnull);
     }
 
     @Override
     protected JavaType _narrow(Class<?> subclass)
     {
         return new MapLikeType(subclass);
     }
 
     @Override
     public JavaType narrowContentsBy(Class<?> contentClass)
     {
         // Can do a quick check first:
         if (contentClass == .getRawClass()) {
             return this;
         }
         return new MapLikeType(.narrowBy(contentClass),
                );
     }
 
     @Override
     public JavaType widenContentsBy(Class<?> contentClass)
     {
         if (contentClass == .getRawClass()) {
             return this;
         }
         return new MapLikeType(.widenBy(contentClass),
                 );
     }
     
     public JavaType narrowKey(Class<?> keySubclass)
     {
         // Can do a quick check first:
         if (keySubclass == .getRawClass()) {
             return this;
         }
         return new MapLikeType(.narrowBy(keySubclass), ,
                 );
     }
 
     public JavaType widenKey(Class<?> keySubclass)
     {
         // Can do a quick check first:
         if (keySubclass == .getRawClass()) {
             return this;
         }
         return new MapLikeType(.widenBy(keySubclass), ,
                 );
     }
     
     @Override
     public MapLikeType withTypeHandler(Object h)
     {
         return new MapLikeType(h);
    }
    @Override
    {
        return new MapLikeType(.withTypeHandler(h),
                );
    }
    @Override
    public MapLikeType withValueHandler(Object h) {
        return new MapLikeType(h);
    }
    @Override
        return new MapLikeType(.withValueHandler(h),
                );
    }
    
    @Override
    protected String buildCanonicalName() {
        StringBuilder sb = new StringBuilder();
        sb.append(.getName());
        if ( != null) {
            sb.append('<');
            sb.append(.toCanonical());
            sb.append(',');
            sb.append(.toCanonical());
            sb.append('>');
        }
        return sb.toString();
    }
 
    /*
    /**********************************************************
    /* Public API
    /**********************************************************
     */
    @Override
    public boolean isContainerType() { return true; }
    @Override
    public boolean isMapLikeType() { return true; }
    
    @Override
    public JavaType getKeyType() { return ; }
    @Override
    public JavaType getContentType() { return ; }
    @Override
    public int containedTypeCount() { return 2; }
    
    @Override
    public JavaType containedType(int index) {
        if (index == 0) return ;
        if (index == 1) return ;
        return null;
    }

    
Not sure if we should count on this, but type names for core interfaces are "K" and "V" respectively. For now let's assume this should work.
    @Override
    public String containedTypeName(int index) {
        if (index == 0) return "K";
        if (index == 1) return "V";
        return null;
    }
    @Override
        return _classSignature(sbtrue);
    }
    
    @Override
    {
        _classSignature(sbfalse);
        sb.append('<');
        .getGenericSignature(sb);
        .getGenericSignature(sb);
        sb.append(">;");
        return sb;
    }
 
    /*
    /**********************************************************
    /* Extended API
    /**********************************************************
     */
    {
        return new MapLikeType(.withTypeHandler(h), ,
                );
    }
    public MapLikeType withKeyValueHandler(Object h) {
        return new MapLikeType(.withValueHandler(h), ,
                );
    }
    
    
Method that can be used for checking whether this type is a "real" Collection type; meaning whether it represents a parameterized subtype of java.util.Collection or just something that acts like one.
    public boolean isTrueMapType() {
        return Map.class.isAssignableFrom();
    }
    /*
    /**********************************************************
    /* Standard methods
    /**********************************************************
     */
    @Override
    public String toString()
    {
        return "[map-like type; class "+.getName()+", "++" -> "++"]";
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) return false;
        MapLikeType other = (MapLikeTypeo;
        return ( == other._class)
            && .equals(other._keyType)
            && .equals(other._valueType);
    }
    
New to GrepCode? Check out our FAQ X