|
| 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 | +} |
0 commit comments