content: package com.expectlost.serialize;
  
  import com.expectlost.config.ObjectWrapper;
  import com.expectlost.serialize.impl.HessianSerializer;
  import com.expectlost.serialize.impl.JdkSerializer;
  import com.expectlost.serialize.impl.JsonSerializer;
  import lombok.extern.slf4j.Slf4j;
  
  import java.util.concurrent.ConcurrentHashMap;
  
  @Slf4j
  public class SerializerFactory {
      private final static ConcurrentHashMap<String, ObjectWrapper<Serializer>> SERIALIZER_CACHE = new ConcurrentHashMap<>(8);
      private final static ConcurrentHashMap<Byte, ObjectWrapper<Serializer>> SERIALIZER_CACHE_CODE = new ConcurrentHashMap<>(8);
  
      static {
          ObjectWrapper<Serializer> jdk = new  ObjectWrapper<>((byte) 1, "jdk", new JdkSerializer());
          ObjectWrapper<Serializer> json = new  ObjectWrapper<>((byte) 2, "json", new JsonSerializer());
          ObjectWrapper<Serializer> hessian = new  ObjectWrapper<>((byte) 3, "hessian", new HessianSerializer());
          SERIALIZER_CACHE.put("jdk",jdk);
          SERIALIZER_CACHE.put("json",json);
          SERIALIZER_CACHE.put("hessian",hessian);
  
          SERIALIZER_CACHE_CODE.put((byte) 1, jdk);
          SERIALIZER_CACHE_CODE.put((byte) 2, json);
          SERIALIZER_CACHE_CODE.put((byte) 3, hessian);
      }
  
      /**
       * 使用工厂方法获取一个SerializerWrapper
       * @param serializeType 序列化的类型
       * @return SerializerWrapper
       */
      public static  ObjectWrapper<Serializer> getSerializer(String serializeType) {
          ObjectWrapper<Serializer> serializerWrapper = SERIALIZER_CACHE.get(serializeType);
          if(serializerWrapper == null){
              log.error("未找到您配置的【{}】序列化工具，默认选用jdk的序列化方式。",serializeType);
              return SERIALIZER_CACHE.get("jdk");
          }
  
          return SERIALIZER_CACHE.get(serializeType);
      }
  
      public static  ObjectWrapper<Serializer> getSerializer(Byte serializeCode) {
  
          System.out.println("正在使用序列化方式為"+serializeCode);
          ObjectWrapper<Serializer> serializerWrapper = SERIALIZER_CACHE_CODE.get(serializeCode);
          if(serializerWrapper == null){
              log.error("未找到您配置的【{}】序列化工具，默认选用jdk的序列化方式。",serializeCode);
              return SERIALIZER_CACHE.get("jdk");
          }
          return SERIALIZER_CACHE_CODE.get(serializeCode);
      }
  
      /**
       * 新增一个新的序列化器
       * @param serializerObjectWrapper 序列化器的包装
       */
      public static void addSerializer(ObjectWrapper<Serializer> serializerObjectWrapper){
          SERIALIZER_CACHE.put(serializerObjectWrapper.getName(),serializerObjectWrapper);
          SERIALIZER_CACHE_CODE.put(serializerObjectWrapper.getCode(),serializerObjectWrapper);
      }
  
  
  }
  