1
package com.eucalyptus.util;
3
import java.util.Arrays;
5
import java.util.concurrent.ConcurrentMap;
6
import org.apache.log4j.Logger;
7
import com.eucalyptus.system.Ats;
8
import com.google.common.base.Function;
9
import com.google.common.collect.Lists;
10
import com.google.common.collect.Maps;
12
public class Composites {
13
private static Logger LOG = Logger.getLogger( Composites.class );
14
private static ConcurrentMap<Class, CompositeHelper> subTypeCache = Maps.newConcurrentHashMap( );
16
private static <T> CompositeHelper<T> build( Class<T> destType ) {
17
List<Class> sourceTypes = Lists.newArrayList( );
18
if ( !Ats.from( destType ).has( Composite.class ) ) {
19
sourceTypes.add( destType );
21
sourceTypes = Arrays.asList( Ats.from( destType ).get( Composite.class ).value( ) );
23
CompositeHelper<T> helper = null;
24
if ( !subTypeCache.containsKey( destType ) ) {
25
helper = new CompositeHelper<T>( destType, sourceTypes );
26
subTypeCache.putIfAbsent( destType, helper );
28
helper = subTypeCache.get( destType );
33
public static <A,B> B updateNulls( A source, B dest ) {
34
return ( B ) CompositeHelper.updateNulls( source, dest );
37
public static <A,B> B update( A source, B dest ) {
38
return ( B ) CompositeHelper.update( source, dest );
41
public static <T> T composeNew( Class<T> destType, Object... sources ) {
44
dest = destType.newInstance( );
45
return compose( dest, sources );
46
} catch ( Exception e ) {
48
throw new RuntimeException( "Composition for " + destType.getCanonicalName( ) + " failed because of " + e.getMessage( ), e );
52
public static <T> T compose( T dest, Object... sources ) {
53
CompositeHelper<T> helper = Composites.build( (Class<T>)dest.getClass( ) );
54
return helper.compose( dest, sources );
57
public static <A,B> B updateNew( A source, Class<B> destType ) {
59
return ( B ) CompositeHelper.update( source, destType.newInstance( ) );
60
} catch ( Exception e ) {
62
throw new RuntimeException( "Failed to update composable object because of: " + e.getMessage( ), e );
65
public static <T> List<Object> projectNew( T source, Class... destTypes ) {
66
List<Object> dests = Lists.transform( Arrays.asList( destTypes ), new Function<Class,Object>( ) {
68
public Object apply( Class arg0 ) {
70
return arg0.newInstance( );
71
} catch ( Exception e ) {
72
throw new RuntimeException( e );
75
return project( source, dests );
78
public static <T> List<Object> projectNew( T source ) {
80
List<Object> dests = Lists.transform( subTypeCache.get( source ).getSourceTypes( ), new Function<Class,Object>() {
82
public Object apply( Class arg0 ) {
84
return arg0.newInstance( );
85
} catch ( Exception e ) {
86
throw new RuntimeException( e );
89
return project( source, dests );
90
} catch ( Exception e ) {
92
throw new RuntimeException( "Projection for " + source.getClass( ).getCanonicalName( ) + " failed because of " + e.getMessage( ), e );
96
public static <T> List<Object> project( T source, Object... dests ) {
97
CompositeHelper<T> helper = ( CompositeHelper<T> ) Composites.build( source.getClass( ) );
98
return helper.project( source, dests );