All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.configuration.attributes.CollectionAttributeCopier Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commons.configuration.attributes;

import java.util.HashMap;
import java.util.HashSet;

import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;

/**
 * CollectionAttributeCopier. This {@link AttributeCopier} can handle a handful of "known"
 * collection types ( {@link HashSet}, {@link HashMap} )
 *
 * @author Tristan Tarrant
 * @since 7.2
 */
public class CollectionAttributeCopier implements AttributeCopier {
   private static final Log log = LogFactory.getLog(CollectionAttributeCopier.class);
   public static final AttributeCopier INSTANCE = new CollectionAttributeCopier<>();

   @SuppressWarnings("unchecked")
   @Override
   public T copyAttribute(T attribute) {
      if (attribute == null) {
         return null;
      }
      Class klass = attribute.getClass();
      if (HashSet.class.equals(klass)) {
         return (T) new HashSet<>((HashSet)attribute);
      } else if (HashMap.class.equals(klass)) {
         return (T) new HashMap<>((HashMap)attribute);
      } else {
         throw log.noAttributeCopierForType(klass);
      }
   }

}