1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.proxy2.provider;
19
20 import java.io.Serializable;
21
22 import org.apache.commons.lang3.ObjectUtils;
23 import org.apache.commons.lang3.Validate;
24 import org.apache.commons.lang3.exception.CloneFailedException;
25 import org.apache.commons.lang3.reflect.MethodUtils;
26 import org.apache.commons.proxy2.ObjectProvider;
27 import org.apache.commons.proxy2.exception.ObjectProviderException;
28
29 /**
30 * Merely calls <code>clone()</code> (reflectively) on the given {@link Cloneable} object.
31 *
32 * @since 1.0
33 */
34 public class CloningProvider<T extends Cloneable> implements ObjectProvider<T>, Serializable
35 {
36 /**
37 * Serialization version
38 */
39 private static final long serialVersionUID = 1L;
40
41 //******************************************************************************************************************
42 // Fields
43 //******************************************************************************************************************
44
45 private final T cloneable;
46
47 //******************************************************************************************************************
48 // Constructors
49 //******************************************************************************************************************
50
51 /**
52 * Constructs a provider which returns clone copies of the specified {@link Cloneable} object.
53 *
54 * @param cloneable
55 * the object to clone
56 */
57 public CloningProvider(T cloneable)
58 {
59 Validate.notNull(cloneable, "Cloneable object cannot be null.");
60 Validate.isTrue(MethodUtils.getAccessibleMethod(cloneable.getClass(), "clone") != null,
61 String.format("Class %s does not override clone() method as public.", cloneable.getClass().getName()));
62 this.cloneable = cloneable;
63 }
64
65 //******************************************************************************************************************
66 // ObjectProvider Implementation
67 //******************************************************************************************************************
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public T getObject()
74 {
75 try
76 {
77 return ObjectUtils.clone(cloneable);
78 }
79 catch (CloneFailedException e) {
80 throw new ObjectProviderException(e.getMessage(), e.getCause());
81 }
82 }
83
84 }