Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.metadata;
 
 
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public abstract class AbstractTypedJacksonModule<T>
         extends SimpleModule
 {
     private final String typeProperty;
 
     protected AbstractTypedJacksonModule(Class<T> baseClassString typePropertyJsonTypeIdResolver<T> typeIdResolver)
     {
         super(baseClass.getSimpleName() + "Module", Version.unknownVersion());
         this. = typeProperty;
 
         TypeIdResolver typeResolver = new InternalTypeResolver((JsonTypeIdResolver<Object>) typeIdResolver);
 
         addSerializer(baseClassnew InternalTypeSerializer(baseClasstypeResolver));
         addDeserializer(baseClassnew InternalTypeDeserializer(baseClasstypeResolver));
     }
 
     public class InternalTypeDeserializer
             extends StdDeserializer<T>
     {
         private final TypeDeserializer typeDeserializer;
 
         InternalTypeDeserializer(Class<T> baseClassTypeIdResolver typeIdResolver)
         {
             super(baseClass);
             this. = new AsPropertyTypeDeserializer(SimpleType.construct(baseClass), typeIdResolverfalsenull);
         }
 
         @SuppressWarnings("unchecked")
         @Override
         public T deserialize(JsonParser jsonParserDeserializationContext deserializationContext)
                 throws IOException
         {
             return (T) .deserializeTypedFromAny(jsonParserdeserializationContext);
         }
     }
 
     public class InternalTypeSerializer
             extends StdSerializer<T>
     {
         private final TypeSerializer typeSerializer;
         private final Cache<Class<?>, JsonSerializer<Object>> serializerCache = CacheBuilder.newBuilder().build();
 
         InternalTypeSerializer(Class<T> baseClassTypeIdResolver typeIdResolver)
         {
             super(baseClass);
             this. = new AsPropertyTypeSerializer(typeIdResolvernull);
         }
 
         @Override
         public void serialize(final T valueJsonGenerator jsonGeneratorfinal SerializerProvider serializerProvider)
                 throws IOException
         {
            if (value == null) {
                serializerProvider.defaultSerializeNull(jsonGenerator);
            }
            else {
                try {
                    JsonSerializer<Objectserializer = .get(value.getClass(), new Callable<JsonSerializer<Object>>()
                    {
                        @Override
                        public JsonSerializer<Objectcall()
                                throws Exception
                        {
                            return ..createSerializer(serializerProviderserializerProvider.constructType(value.getClass()));
                        }
                    });
                    serializer.serializeWithType(valuejsonGeneratorserializerProvider);
                }
                catch (ExecutionException e) {
                    Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                    Throwables.propagateIfInstanceOf(e.getCause(), JsonGenerationException.class);
                    throw Throwables.propagate(e.getCause());
                }
            }
        }
    }
            implements TypeIdResolver
    {
        private final JsonTypeIdResolver<ObjecttypeIdResolver;
        private final LoadingCache<Class<?>, SimpleTypesimpleTypes;
        InternalTypeResolver(JsonTypeIdResolver<ObjecttypeIdResolver)
        {
            this. = checkNotNull(typeIdResolver"typeIdResolver is null");
             = CacheBuilder.newBuilder().weakKeys().weakValues().build(new CacheLoader<Class<?>, SimpleType>()
            {
                @Override
                public SimpleType load(Class<?> typeClass)
                        throws Exception
                {
                    return SimpleType.construct(typeClass);
                }
            });
        }
        @Override
        public void init(JavaType baseType)
        {
        }
        @Override
        public String idFromValue(Object value)
        {
            checkNotNull(value"value is null");
            return idFromValueAndType(valuevalue.getClass());
        }
        @Override
        public String idFromValueAndType(Object valueClass<?> suggestedType)
        {
            checkNotNull(value"value is null");
            String type = .getId(value);
            checkArgument(type != null"Unknown class %s"suggestedType.getSimpleName());
            return type;
        }
        @Override
        public String idFromBaseType()
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public JavaType typeFromId(String id)
        {
            checkNotNull(id"id is null");
            Class<?> typeClass = .getType(id);
            checkArgument(typeClass != null"Unknown type id %s"id);
            return .getUnchecked(typeClass);
        }
        @Override
        public Id getMechanism()
        {
            return .;
        }
    }
New to GrepCode? Check out our FAQ X