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

org.ehcache.impl.copy.SerializingCopier Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * Copyright Terracotta, Inc.
 *
 * 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 org.ehcache.impl.copy;

import org.ehcache.spi.copy.Copier;
import org.ehcache.spi.serialization.SerializerException;
import org.ehcache.spi.serialization.Serializer;

/**
 * A helper copier implementation that performs the "copying" using {@link Serializer serialization}.
 */
public final class SerializingCopier extends ReadWriteCopier {

  private final Serializer serializer;

  /**
   * Convenience method allowing to represent this copier's class as the expected type in configuration.
   *
   * @param  The type to work on
   * @return the proper type
   */
  @SuppressWarnings("unchecked")
  public static  Class> asCopierClass() {
    return (Class) SerializingCopier.class;
  }

  /**
   * Creates a new copier that will using the provided {@link Serializer}.
   *
   * @param serializer the serializer to use
   */
  public SerializingCopier(Serializer serializer) {
    if (serializer == null) {
      throw new NullPointerException("A " + SerializingCopier.class.getName() + " instance requires a "
                                     + Serializer.class.getName() + " instance to copy!");
    }
    this.serializer = serializer;
  }

  /**
   * Returns a copy of the passed in instance by serializing and deserializing it.
   */
  @Override
  public T copy(final T obj) {
    try {
      return serializer.read(serializer.serialize(obj));
    } catch (ClassNotFoundException e) {
      throw new SerializerException("Copying failed.", e);
    }
  }

  /**
   * Returns the {@link Serializer} used by this copier.
   *
   * @return the serializer used
   */
  public Serializer getSerializer() {
    return serializer;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy