Skip to content

Commit e244bcb

Browse files
committed
Added refreshed jar
1 parent c226102 commit e244bcb

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.rits.cloning;
2+
3+
/**
4+
* @author kostantinos.kougios
5+
*
6+
* 17 Jul 2012
7+
*/
8+
public interface IInstantiationStrategy
9+
{
10+
<T> T newInstance(final Class<T> c);
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.rits.cloning;
2+
3+
import org.objenesis.Objenesis;
4+
import org.objenesis.ObjenesisStd;
5+
6+
/**
7+
* @author kostantinos.kougios
8+
*
9+
* 17 Jul 2012
10+
*/
11+
public class ObjenesisInstantiationStrategy implements IInstantiationStrategy
12+
{
13+
private final Objenesis objenesis = new ObjenesisStd();
14+
15+
@SuppressWarnings("unchecked")
16+
public <T> T newInstance(Class<T> c)
17+
{
18+
return (T) objenesis.newInstance(c);
19+
}
20+
21+
private static ObjenesisInstantiationStrategy instance = new ObjenesisInstantiationStrategy();
22+
23+
public static ObjenesisInstantiationStrategy getInstance()
24+
{
25+
return instance;
26+
}
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.rits.perspectives;
2+
3+
import java.util.Collection;
4+
5+
import com.rits.cloning.Cloner;
6+
7+
/**
8+
* Perspectives: an object instance of a class behaving differently according to the "view angle".
9+
*
10+
* @author kostantinos.kougios
11+
*
12+
* 30 Nov 2009
13+
*/
14+
public class Perspectives
15+
{
16+
private final Cloner cloner;
17+
18+
public Perspectives(final Cloner cloner)
19+
{
20+
this.cloner = cloner;
21+
}
22+
23+
/**
24+
* Sample: if o is an instance of Product and c is OrderedProduct.class then this returns
25+
* and instance of OrderedProduct.class which has equal field values to those of the instance of Product.
26+
* In other words, the returned instance of OrderedProduct.class is the Product instance from the perspective
27+
* of an OrderedProduct
28+
*
29+
* View an object o from the perspective of class c. (view o as an instance of c). c must be instanceof o.getClass()
30+
*
31+
* @param <T> the object
32+
* @param <E> this will be the returned type and it must be instanceof T. All properties of o will be copied to this instance.
33+
* @param c the class of E. This is used to generate new instances of c
34+
* @param o the object that must be viewed from a different perspective
35+
* @return the E perspective of o
36+
*/
37+
public <T, E extends T> E viewAs(final Class<E> c, final T o)
38+
{
39+
if (o == null) return null;
40+
if (o instanceof Collection<?>) throw new IllegalArgumentException("for collections please use viewCollectionAs() method. Invalid object " + o);
41+
final E newInstance = cloner.fastCloneOrNewInstance(c);
42+
cloner.copyPropertiesOfInheritedClass(o, newInstance);
43+
return newInstance;
44+
}
45+
46+
/**
47+
* Sample: if o is a [ Products extends LinkedList<Product> ] then the returned instance
48+
* is a [ OrderedProducts extends LinkedList<OrderedProduct> ].
49+
*
50+
* View a collection o from the perspective of collection E.
51+
*
52+
* NOTE: order of the items might not be preserved, depending on the collection type
53+
*
54+
* @param <T> the type of the collection o
55+
* @param <I> the type of the elements of the collection o
56+
* @param <E> the type of the perspective collection
57+
* @param <NI> the type of the perspective's elements
58+
* @param newCollection the collection to which the adapted instances should be added
59+
* @param currentCollection the collection with the instances to be adapted
60+
* @param perspectiveCollectionItemClass the class of the NI
61+
* @return E, the collection from a different perspective or null if currentCollection is null
62+
*/
63+
public <I, NI extends I, T extends Collection<I>, E extends Collection<NI>> E viewCollectionAs(final E newCollection, final Class<NI> perspectiveCollectionItemClass, final T currentCollection)
64+
{
65+
if (currentCollection == null) return null;
66+
for (final I item : currentCollection)
67+
{
68+
final NI newItem = viewAs(perspectiveCollectionItemClass, item);
69+
newCollection.add(newItem);
70+
}
71+
return newCollection;
72+
}
73+
}

chroniclerj-0.4.1.jar

4.96 KB
Binary file not shown.

0 commit comments

Comments
 (0)