001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.proxy2.provider; 019 020import java.io.Serializable; 021 022import org.apache.commons.lang3.ObjectUtils; 023import org.apache.commons.lang3.Validate; 024import org.apache.commons.lang3.exception.CloneFailedException; 025import org.apache.commons.lang3.reflect.MethodUtils; 026import org.apache.commons.proxy2.ObjectProvider; 027import org.apache.commons.proxy2.exception.ObjectProviderException; 028 029/** 030 * Merely calls <code>clone()</code> (reflectively) on the given {@link Cloneable} object. 031 * 032 * @since 1.0 033 */ 034public class CloningProvider<T extends Cloneable> implements ObjectProvider<T>, Serializable 035{ 036 /** 037 * Serialization version 038 */ 039 private static final long serialVersionUID = 1L; 040 041 //****************************************************************************************************************** 042 // Fields 043 //****************************************************************************************************************** 044 045 private final T cloneable; 046 047 //****************************************************************************************************************** 048 // Constructors 049 //****************************************************************************************************************** 050 051 /** 052 * Constructs a provider which returns clone copies of the specified {@link Cloneable} object. 053 * 054 * @param cloneable 055 * the object to clone 056 */ 057 public CloningProvider(T cloneable) 058 { 059 Validate.notNull(cloneable, "Cloneable object cannot be null."); 060 Validate.isTrue(MethodUtils.getAccessibleMethod(cloneable.getClass(), "clone") != null, 061 String.format("Class %s does not override clone() method as public.", cloneable.getClass().getName())); 062 this.cloneable = cloneable; 063 } 064 065 //****************************************************************************************************************** 066 // ObjectProvider Implementation 067 //****************************************************************************************************************** 068 069 /** 070 * {@inheritDoc} 071 */ 072 @Override 073 public T getObject() 074 { 075 try 076 { 077 return ObjectUtils.clone(cloneable); 078 } 079 catch (CloneFailedException e) { 080 throw new ObjectProviderException(e.getMessage(), e.getCause()); 081 } 082 } 083 084}