polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [1/7] zest-java git commit: entitystores: replace usage of core/io with streams
Date Fri, 09 Dec 2016 16:28:32 GMT
Repository: zest-java
Updated Branches:
  refs/heads/develop 8854b1306 -> 0b4cca068


http://git-wip-us.apache.org/repos/asf/zest-java/blob/ee1d1abc/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java
b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java
index a52b526..fa66da2 100644
--- a/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java
+++ b/extensions/entitystore-sql/src/main/java/org/apache/zest/entitystore/sql/SQLEntityStoreMixin.java
@@ -34,8 +34,13 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Spliterator;
+import java.util.Spliterators;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 import org.apache.zest.api.common.Optional;
 import org.apache.zest.api.common.QualifiedName;
 import org.apache.zest.api.entity.EntityDescriptor;
@@ -57,11 +62,6 @@ import org.apache.zest.entitystore.sql.internal.DatabaseSQLService;
 import org.apache.zest.entitystore.sql.internal.DatabaseSQLService.EntityValueResult;
 import org.apache.zest.entitystore.sql.internal.SQLEntityState;
 import org.apache.zest.entitystore.sql.internal.SQLEntityState.DefaultSQLEntityState;
-import org.apache.zest.functional.Visitor;
-import org.apache.zest.io.Input;
-import org.apache.zest.io.Output;
-import org.apache.zest.io.Receiver;
-import org.apache.zest.io.Sender;
 import org.apache.zest.library.sql.common.SQLUtil;
 import org.apache.zest.spi.ZestSPI;
 import org.apache.zest.spi.entity.EntityState;
@@ -272,7 +272,8 @@ public class SQLEntityStoreMixin
                                        EntityDescriptor entityDescriptor
     )
     {
-        return new DefaultSQLEntityState( new DefaultEntityState( unitOfWork.currentTime(),
entityRef, entityDescriptor ) );
+        return new DefaultSQLEntityState(
+            new DefaultEntityState( unitOfWork.currentTime(), entityRef, entityDescriptor
) );
     }
 
     @Override
@@ -282,77 +283,56 @@ public class SQLEntityStoreMixin
     }
 
     @Override
-    public Input<EntityState, EntityStoreException> entityStates( final ModuleDescriptor
module )
+    public Stream<EntityState> entityStates( final ModuleDescriptor module )
     {
-        return new Input<EntityState, EntityStoreException>()
+        try
         {
-            @Override
-            public <ReceiverThrowableType extends Throwable> void transferTo( Output<?
super EntityState, ReceiverThrowableType> output )
-                throws EntityStoreException, ReceiverThrowableType
-            {
-                output.receiveFrom( new Sender<EntityState, EntityStoreException>()
+            Connection connection = database.getConnection();
+            PreparedStatement ps = database.prepareGetAllEntitiesStatement( connection );
+            database.populateGetAllEntitiesStatement( ps );
+            ResultSet rs = ps.executeQuery();
+            return StreamSupport.stream(
+                new Spliterators.AbstractSpliterator<EntityState>( Long.MAX_VALUE,
Spliterator.ORDERED )
                 {
                     @Override
-                    public <RecThrowableType extends Throwable> void sendTo( final
Receiver<? super EntityState, RecThrowableType> receiver )
-                        throws RecThrowableType, EntityStoreException
+                    public boolean tryAdvance( final Consumer<? super EntityState>
action )
                     {
-                        queryAllEntities( module, visited -> {
-                            try
-                            {
-                                receiver.receive( visited );
-                            }
-                            catch( Throwable receiverThrowableType )
-                            {
-                                throw new SQLException( receiverThrowableType );
-                            }
+                        try
+                        {
+                            if( !rs.next() ) { return false; }
+                            EntityState entityState = readEntityState( module,
+                                                                       database.getEntityValue(
rs ).getReader() );
+                            action.accept( entityState );
                             return true;
-                        } );
+                        }
+                        catch( SQLException ex )
+                        {
+                            SQLUtil.closeQuietly( rs, ex );
+                            SQLUtil.closeQuietly( ps, ex );
+                            SQLUtil.closeQuietly( connection, ex );
+                            throw new EntityStoreException( ex );
+                        }
                     }
-                } );
-            }
-        };
-    }
-
-    private void queryAllEntities( ModuleDescriptor module, EntityStatesVisitor entityStatesVisitor
)
-    {
-        Connection connection = null;
-        PreparedStatement ps = null;
-        ResultSet rs = null;
-        try
-        {
-            connection = database.getConnection();
-            ps = database.prepareGetAllEntitiesStatement( connection );
-            database.populateGetAllEntitiesStatement( ps );
-            rs = ps.executeQuery();
-            while( rs.next() )
-            {
-                DefaultEntityState entityState = readEntityState( module, database.getEntityValue(
rs ).getReader() );
-                if( !entityStatesVisitor.visit( entityState ) )
+                },
+                false
+            ).onClose(
+                () ->
                 {
-                    return;
+                    SQLUtil.closeQuietly( rs );
+                    SQLUtil.closeQuietly( ps );
+                    SQLUtil.closeQuietly( connection );
                 }
-            }
+            );
         }
         catch( SQLException ex )
         {
             throw new EntityStoreException( ex );
         }
-        finally
-        {
-            SQLUtil.closeQuietly( rs );
-            SQLUtil.closeQuietly( ps );
-            SQLUtil.closeQuietly( connection );
-        }
-    }
-
-    private interface EntityStatesVisitor
-        extends Visitor<EntityState, SQLException>
-    {
     }
 
     protected Identity newUnitOfWorkId()
     {
-        return identityGenerator.generate(EntityStore.class);
+        return identityGenerator.generate( EntityStore.class );
     }
 
     protected DefaultEntityState readEntityState( ModuleDescriptor module, Reader entityState
)
@@ -364,7 +344,7 @@ public class SQLEntityStoreMixin
             final EntityStatus[] status = { EntityStatus.LOADED };
 
             String version = jsonObject.getString( JSONKeys.VERSION );
-            Instant modified = Instant.ofEpochMilli(jsonObject.getLong( JSONKeys.MODIFIED
));
+            Instant modified = Instant.ofEpochMilli( jsonObject.getLong( JSONKeys.MODIFIED
) );
             String identity = jsonObject.getString( JSONKeys.IDENTITY );
 
             // Check if version is correct
@@ -398,102 +378,117 @@ public class SQLEntityStoreMixin
 
             Map<QualifiedName, Object> properties = new HashMap<>();
             JSONObject props = jsonObject.getJSONObject( JSONKeys.PROPERTIES );
-            entityDescriptor.state().properties().forEach( propertyDescriptor -> {
-                Object jsonValue;
-                try
+            entityDescriptor.state().properties().forEach(
+                propertyDescriptor ->
                 {
-                    jsonValue = props.get( propertyDescriptor.qualifiedName().name() );
-                    if( JSONObject.NULL.equals( jsonValue ) )
+                    Object jsonValue;
+                    try
                     {
-                        properties.put( propertyDescriptor.qualifiedName(), null );
+                        jsonValue = props.get(
+                            propertyDescriptor.qualifiedName().name() );
+                        if( JSONObject.NULL.equals( jsonValue ) )
+                        {
+                            properties.put( propertyDescriptor.qualifiedName(), null );
+                        }
+                        else
+                        {
+                            Object value = valueSerialization.deserialize( module,
+                                                                           propertyDescriptor.valueType(),
+                                                                           jsonValue.toString()
);
+                            properties.put( propertyDescriptor.qualifiedName(), value );
+                        }
                     }
-                    else
+                    catch( JSONException e )
                     {
-                        Object value = valueSerialization.deserialize( module, propertyDescriptor.valueType(),
jsonValue
-                            .toString() );
-                        properties.put( propertyDescriptor.qualifiedName(), value );
+                        // Value not found, default it
+                        Object initialValue = propertyDescriptor.initialValue( module );
+                        properties.put( propertyDescriptor.qualifiedName(), initialValue
);
+                        status[ 0 ] = EntityStatus.UPDATED;
                     }
                 }
-                catch( JSONException e )
-                {
-                    // Value not found, default it
-                    Object initialValue = propertyDescriptor.initialValue( module );
-                    properties.put( propertyDescriptor.qualifiedName(), initialValue );
-                    status[ 0 ] = EntityStatus.UPDATED;
-                }
-            } );
+            );
 
             Map<QualifiedName, EntityReference> associations = new HashMap<>();
             JSONObject assocs = jsonObject.getJSONObject( JSONKeys.ASSOCIATIONS );
-            entityDescriptor.state().associations().forEach( associationType -> {
-                try
+            entityDescriptor.state().associations().forEach(
+                associationType ->
                 {
-                    Object jsonValue = assocs.get( associationType.qualifiedName().name()
);
-                    EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference(
-                        (String) jsonValue );
-                    associations.put( associationType.qualifiedName(), value );
-                }
-                catch( JSONException e )
-                {
-                    // Association not found, default it to null
-                    associations.put( associationType.qualifiedName(), null );
-                    status[ 0 ] = EntityStatus.UPDATED;
+                    try
+                    {
+                        Object jsonValue = assocs.get( associationType.qualifiedName().name()
);
+                        EntityReference value = jsonValue == JSONObject.NULL
+                                                ? null
+                                                : EntityReference.parseEntityReference( (String)
jsonValue );
+                        associations.put( associationType.qualifiedName(), value );
+                    }
+                    catch( JSONException e )
+                    {
+                        // Association not found, default it to null
+                        associations.put( associationType.qualifiedName(), null );
+                        status[ 0 ] = EntityStatus.UPDATED;
+                    }
                 }
-            } );
+            );
 
             JSONObject manyAssocs = jsonObject.getJSONObject( JSONKeys.MANY_ASSOCIATIONS
);
             Map<QualifiedName, List<EntityReference>> manyAssociations = new
HashMap<>();
-            entityDescriptor.state().manyAssociations().forEach( manyAssociationType ->
{
-                List<EntityReference> references = new ArrayList<>();
-                try
+            entityDescriptor.state().manyAssociations().forEach(
+                manyAssociationType ->
                 {
-                    JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name()
);
-                    for( int i = 0; i < jsonValues.length(); i++ )
+                    List<EntityReference> references = new ArrayList<>();
+                    try
+                    {
+                        JSONArray jsonValues = manyAssocs.getJSONArray( manyAssociationType.qualifiedName().name()
);
+                        for( int i = 0; i < jsonValues.length(); i++ )
+                        {
+                            Object jsonValue = jsonValues.getString( i );
+                            EntityReference value = jsonValue == JSONObject.NULL
+                                                    ? null
+                                                    : EntityReference.parseEntityReference(
(String) jsonValue );
+                            references.add( value );
+                        }
+                        manyAssociations.put( manyAssociationType.qualifiedName(), references
);
+                    }
+                    catch( JSONException e )
                     {
-                        Object jsonValue = jsonValues.getString( i );
-                        EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference(
-                            (String) jsonValue );
-                        references.add( value );
+                        // ManyAssociation not found, default to empty one
+                        manyAssociations.put( manyAssociationType.qualifiedName(), references
);
                     }
-                    manyAssociations.put( manyAssociationType.qualifiedName(), references
);
-                }
-                catch( JSONException e )
-                {
-                    // ManyAssociation not found, default to empty one
-                    manyAssociations.put( manyAssociationType.qualifiedName(), references
);
-                }
-            } );
+                } );
 
             JSONObject namedAssocs = jsonObject.has( JSONKeys.NAMED_ASSOCIATIONS )
                                      ? jsonObject.getJSONObject( JSONKeys.NAMED_ASSOCIATIONS
)
                                      : new JSONObject();
             Map<QualifiedName, Map<String, EntityReference>> namedAssociations
= new HashMap<>();
-            entityDescriptor.state().namedAssociations().forEach( namedAssociationType ->
{
-                Map<String, EntityReference> references = new LinkedHashMap<>();
-                try
+            entityDescriptor.state().namedAssociations().forEach(
+                namedAssociationType ->
                 {
-                    JSONObject jsonValues = namedAssocs.getJSONObject( namedAssociationType.qualifiedName().name()
);
-                    JSONArray names = jsonValues.names();
-                    if( names != null )
+                    Map<String, EntityReference> references = new LinkedHashMap<>();
+                    try
                     {
-                        for( int idx = 0; idx < names.length(); idx++ )
+                        JSONObject jsonValues = namedAssocs.getJSONObject( namedAssociationType.qualifiedName().name()
);
+                        JSONArray names = jsonValues.names();
+                        if( names != null )
                         {
-                            String name = names.getString( idx );
-                            String jsonValue = jsonValues.getString( name );
-                            references.put( name, EntityReference.parseEntityReference( jsonValue
) );
+                            for( int idx = 0; idx < names.length(); idx++ )
+                            {
+                                String name = names.getString( idx );
+                                String jsonValue = jsonValues.getString( name );
+                                references.put( name, EntityReference.parseEntityReference(
jsonValue ) );
+                            }
                         }
+                        namedAssociations.put( namedAssociationType.qualifiedName(), references
);
                     }
-                    namedAssociations.put( namedAssociationType.qualifiedName(), references
);
-                }
-                catch( JSONException e )
-                {
-                    // NamedAssociation not found, default to empty one
-                    namedAssociations.put( namedAssociationType.qualifiedName(), references
);
-                }
-            } );
+                    catch( JSONException e )
+                    {
+                        // NamedAssociation not found, default to empty one
+                        namedAssociations.put( namedAssociationType.qualifiedName(), references
);
+                    }
+                } );
 
             return new DefaultEntityState( version, modified,
-                                           EntityReference.parseEntityReference( identity
), status[ 0 ], entityDescriptor,
+                                           EntityReference.parseEntityReference( identity
), status[ 0 ],
+                                           entityDescriptor,
                                            properties, associations, manyAssociations, namedAssociations
);
         }
         catch( JSONException e )
@@ -507,7 +502,7 @@ public class SQLEntityStoreMixin
         throws IOException
     {
         JSONObject jsonObject;
-        try (Reader reader = getValue( EntityReference.parseEntityReference( id ) ).getReader())
+        try( Reader reader = getValue( EntityReference.parseEntityReference( id ) ).getReader()
)
         {
             jsonObject = new JSONObject( new JSONTokener( reader ) );
         }
@@ -553,58 +548,65 @@ public class SQLEntityStoreMixin
         try
         {
             JSONWriter json = new JSONWriter( writer );
-            JSONWriter properties = json.object().
-                key( JSONKeys.IDENTITY ).value( state.entityReference().identity().toString()
).
-                key( JSONKeys.APPLICATION_VERSION ).value( application.version() ).
-                key( JSONKeys.TYPE ).value( state.entityDescriptor().types().findFirst().get().getName()
).
-                key( JSONKeys.VERSION ).value( version ).
-                key( JSONKeys.MODIFIED ).value( state.lastModified().toEpochMilli() ).
-                key( JSONKeys.PROPERTIES ).object();
-
-            state.entityDescriptor().state().properties().forEach( persistentProperty ->
{
-                try
+            JSONWriter properties = json.object()
+                                        .key( JSONKeys.IDENTITY )
+                                        .value( state.entityReference().identity().toString()
)
+                                        .key( JSONKeys.APPLICATION_VERSION )
+                                        .value( application.version() )
+                                        .key( JSONKeys.TYPE )
+                                        .value( state.entityDescriptor().types().findFirst().get().getName()
)
+                                        .key( JSONKeys.VERSION )
+                                        .value( version )
+                                        .key( JSONKeys.MODIFIED )
+                                        .value( state.lastModified().toEpochMilli() )
+                                        .key( JSONKeys.PROPERTIES )
+                                        .object();
+
+            state.entityDescriptor().state().properties().forEach(
+                persistentProperty ->
                 {
-                    Object value = state.properties().get( persistentProperty.qualifiedName()
);
-                    json.key( persistentProperty.qualifiedName().name() );
-                    if( value == null || ValueType.isPrimitiveValue( value ) )
+                    try
                     {
-                        json.value( value );
-                    }
-                    else
-                    {
-                        String serialized = valueSerialization.serialize( value );
-                        if( serialized.startsWith( "{" ) )
+                        Object value = state.properties().get( persistentProperty.qualifiedName()
);
+                        json.key( persistentProperty.qualifiedName().name() );
+                        if( value == null || ValueType.isPrimitiveValue( value ) )
                         {
-                            json.value( new JSONObject( serialized ) );
-                        }
-                        else if( serialized.startsWith( "[" ) )
-                        {
-                            json.value( new JSONArray( serialized ) );
+                            json.value( value );
                         }
                         else
                         {
-                            json.value( serialized );
+                            String serialized = valueSerialization.serialize( value );
+                            if( serialized.startsWith( "{" ) )
+                            {
+                                json.value( new JSONObject( serialized ) );
+                            }
+                            else if( serialized.startsWith( "[" ) )
+                            {
+                                json.value( new JSONArray( serialized ) );
+                            }
+                            else
+                            {
+                                json.value( serialized );
+                            }
                         }
                     }
-                }
-                catch( JSONException e )
-                {
-                    throw new EntityStoreException( "Could not store EntityState", e );
-                }
-            } );
+                    catch( JSONException e )
+                    {
+                        throw new EntityStoreException(
+                            "Could not store EntityState", e );
+                    }
+                } );
 
             JSONWriter associations = properties.endObject().key( JSONKeys.ASSOCIATIONS ).object();
-            for( Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry
: state.associations()
-                .entrySet() )
+            for( Map.Entry<QualifiedName, EntityReference> stateNameEntityRefEntry
: state.associations().entrySet() )
             {
-                EntityReference value = stateNameEntityReferenceEntry.getValue();
-                associations.key( stateNameEntityReferenceEntry.getKey().name() ).
-                    value( value != null ? value.identity().toString() : null );
+                EntityReference value = stateNameEntityRefEntry.getValue();
+                associations.key( stateNameEntityRefEntry.getKey().name() )
+                            .value( value != null ? value.identity().toString() : null );
             }
 
             JSONWriter manyAssociations = associations.endObject().key( JSONKeys.MANY_ASSOCIATIONS
).object();
-            for( Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry
: state.manyAssociations()
-                .entrySet() )
+            for( Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry
: state.manyAssociations().entrySet() )
             {
                 JSONWriter assocs = manyAssociations.key( stateNameListEntry.getKey().name()
).array();
                 for( EntityReference entityReference : stateNameListEntry.getValue() )
@@ -615,8 +617,7 @@ public class SQLEntityStoreMixin
             }
 
             JSONWriter namedAssociations = manyAssociations.endObject().key( JSONKeys.NAMED_ASSOCIATIONS
).object();
-            for( Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry
: state.namedAssociations()
-                .entrySet() )
+            for( Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry
: state.namedAssociations().entrySet() )
             {
                 JSONWriter assocs = namedAssociations.key( stateNameMapEntry.getKey().name()
).object();
                 for( Map.Entry<String, EntityReference> entry : stateNameMapEntry.getValue().entrySet()
)

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ee1d1abc/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java
----------------------------------------------------------------------
diff --git a/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java
b/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java
index 1da1f7b..7f8cb54 100644
--- a/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java
+++ b/extensions/migration/src/test/java/org/apache/zest/migration/MigrationTest.java
@@ -19,26 +19,18 @@
  */
 package org.apache.zest.migration;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.StringReader;
-import org.apache.zest.api.identity.Identity;
-import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler;
-import org.hamcrest.CoreMatchers;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.Test;
+import java.util.List;
+import java.util.stream.Stream;
 import org.apache.zest.api.activation.ActivationException;
+import org.apache.zest.api.identity.Identity;
 import org.apache.zest.api.service.importer.NewObjectImporter;
 import org.apache.zest.api.unitofwork.UnitOfWork;
 import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
 import org.apache.zest.bootstrap.SingletonAssembler;
-import org.apache.zest.io.Input;
-import org.apache.zest.io.Output;
-import org.apache.zest.io.Receiver;
-import org.apache.zest.io.Sender;
+import org.apache.zest.bootstrap.unitofwork.DefaultUnitOfWorkAssembler;
 import org.apache.zest.migration.assembly.EntityMigrationOperation;
 import org.apache.zest.migration.assembly.MigrationBuilder;
 import org.apache.zest.migration.assembly.MigrationOperation;
@@ -47,7 +39,12 @@ import org.apache.zest.spi.entitystore.helpers.JSONKeys;
 import org.apache.zest.spi.entitystore.helpers.StateStore;
 import org.apache.zest.test.AbstractZestTest;
 import org.apache.zest.test.EntityTestAssembler;
+import org.hamcrest.CoreMatchers;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Test;
 
+import static java.util.stream.Collectors.toList;
 import static org.junit.Assert.assertThat;
 
 /**
@@ -106,7 +103,7 @@ public class MigrationTest
     {
         Identity id;
         // Set up version 1
-        StringInputOutput data_v1 = new StringInputOutput();
+        List<String> data_v1;
         {
             SingletonAssembler v1 = new SingletonAssembler()
             {
@@ -130,11 +127,14 @@ public class MigrationTest
             BackupRestore backupRestore = v1.module()
                 .findService( BackupRestore.class )
                 .get();
-            backupRestore.backup().transferTo( data_v1 );
+            try( Stream<String> backup = backupRestore.backup() )
+            {
+                data_v1 = backup.collect( toList() );
+            }
         }
 
         // Set up version 1.1
-        StringInputOutput data_v1_1 = new StringInputOutput();
+        List<String> data_v1_1;
         {
             SingletonAssembler v1_1 = new SingletonAssembler()
             {
@@ -148,7 +148,7 @@ public class MigrationTest
             };
 
             BackupRestore testData = v1_1.module().findService( BackupRestore.class ).get();
-            data_v1.transferTo( testData.restore() );
+            testData.restore( data_v1.stream() );
 
             UnitOfWork uow = v1_1.module().unitOfWorkFactory().newUnitOfWork();
             TestEntity1_1 entity = uow.get( TestEntity1_1.class, id );
@@ -157,7 +157,10 @@ public class MigrationTest
             assertThat( "Association has been renamed", entity.newFooAssoc().get(), CoreMatchers.equalTo(
entity ) );
             uow.complete();
 
-            testData.backup().transferTo( data_v1_1 );
+            try( Stream<String> backup = testData.backup() )
+            {
+                data_v1_1 = backup.collect( toList() );
+            }
         }
 
         // Set up version 2.0
@@ -177,7 +180,7 @@ public class MigrationTest
 
             // Test migration from 1.0 -> 2.0
             {
-                data_v1.transferTo( testData.restore() );
+                testData.restore( data_v1.stream() );
                 UnitOfWork uow = v2_0.module().unitOfWorkFactory().newUnitOfWork();
                 TestEntity2_0 entity = uow.get( TestEntity2_0.class, id );
                 assertThat( "Property has been created", entity.bar().get(), CoreMatchers.equalTo(
"Some value" ) );
@@ -202,11 +205,11 @@ public class MigrationTest
             };
 
             BackupRestore testData = v3_0.module().findService( BackupRestore.class ).get();
-            data_v1_1.transferTo( testData.restore() );
+            testData.restore( data_v1_1.stream() );
 
             // Test migration from 1.0 -> 3.0
             {
-                data_v1.transferTo( testData.restore() );
+                testData.restore( data_v1.stream() );
                 UnitOfWork uow = v3_0.module().unitOfWorkFactory().newUnitOfWork();
                 org.apache.zest.migration.moved.TestEntity2_0 entity = uow.get( org.apache.zest.migration.moved.TestEntity2_0.class,
id );
                 uow.complete();
@@ -258,51 +261,4 @@ public class MigrationTest
             System.out.println( msg );
         }
     }
-
-    private static class StringInputOutput
-        implements Output<String, IOException>, Input<String, IOException>
-    {
-        final StringBuilder builder = new StringBuilder();
-
-        @Override
-        public <SenderThrowableType extends Throwable> void receiveFrom( Sender<?
extends String, SenderThrowableType> sender )
-            throws IOException, SenderThrowableType
-        {
-            sender.sendTo((Receiver<String, IOException>) item -> builder.append(
item ).append( "\n" ));
-        }
-
-        @Override
-        public <ReceiverThrowableType extends Throwable> void transferTo( Output<?
super String, ReceiverThrowableType> output )
-            throws IOException, ReceiverThrowableType
-        {
-            output.receiveFrom( new Sender<String, IOException>()
-            {
-                @Override
-                public <ReceiverThrowableType extends Throwable> void sendTo( Receiver<?
super String, ReceiverThrowableType> receiver )
-                    throws ReceiverThrowableType, IOException
-                {
-                    BufferedReader reader = new BufferedReader( new StringReader( builder.toString()
) );
-                    String line;
-                    try
-                    {
-                        while( ( line = reader.readLine() ) != null )
-                        {
-                            receiver.receive( line );
-                        }
-                    }
-                    finally
-                    {
-                        reader.close();
-                    }
-                }
-            } );
-        }
-
-        @Override
-        public String toString()
-        {
-            return builder.toString();
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ee1d1abc/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java
----------------------------------------------------------------------
diff --git a/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java
b/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java
index c3a3af4..f11dc77 100644
--- a/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java
+++ b/extensions/reindexer/src/main/java/org/apache/zest/index/reindexer/internal/ReindexerMixin.java
@@ -21,7 +21,7 @@
 package org.apache.zest.index.reindexer.internal;
 
 import java.util.ArrayList;
-import org.apache.zest.api.common.QualifiedName;
+import java.util.stream.Stream;
 import org.apache.zest.api.configuration.Configuration;
 import org.apache.zest.api.identity.HasIdentity;
 import org.apache.zest.api.injection.scope.Service;
@@ -31,9 +31,6 @@ import org.apache.zest.api.service.ServiceReference;
 import org.apache.zest.api.structure.ModuleDescriptor;
 import org.apache.zest.index.reindexer.Reindexer;
 import org.apache.zest.index.reindexer.ReindexerConfiguration;
-import org.apache.zest.io.Output;
-import org.apache.zest.io.Receiver;
-import org.apache.zest.io.Sender;
 import org.apache.zest.spi.entity.EntityState;
 import org.apache.zest.spi.entitystore.EntityStore;
 import org.apache.zest.spi.entitystore.StateChangeListener;
@@ -67,52 +64,43 @@ public class ReindexerMixin
         {
             loadValue = 50;
         }
-        new ReindexerOutput( loadValue ).reindex( store );
+        ReindexerHelper helper = new ReindexerHelper( loadValue );
+        helper.reindex( store );
     }
 
-    private class ReindexerOutput
-        implements Output<EntityState, RuntimeException>, Receiver<EntityState,
RuntimeException>
+    private class ReindexerHelper
     {
         private int count;
         private int loadValue;
         private ArrayList<EntityState> states;
 
-        public ReindexerOutput( Integer loadValue )
+        private ReindexerHelper( int loadValue )
         {
             this.loadValue = loadValue;
             states = new ArrayList<>();
         }
 
-        public void reindex( EntityStore store )
+        private void reindex( EntityStore store )
         {
-
-            store.entityStates( module ).transferTo( this );
-            reindexState();
-        }
-
-        @Override
-        public <SenderThrowableType extends Throwable> void receiveFrom( Sender<?
extends EntityState, SenderThrowableType> sender )
-            throws RuntimeException, SenderThrowableType
-        {
-            sender.sendTo( this );
-            reindexState();
-        }
-
-        @Override
-        public void receive( EntityState item )
-            throws RuntimeException
-        {
-            count++;
-            item.setPropertyValue( HasIdentity.IDENTITY_STATE_NAME, item.entityReference().identity()
);
-            states.add( item );
-
-            if( states.size() >= loadValue )
+            try( Stream<EntityState> entityStates = store.entityStates( module ) )
             {
-                reindexState();
+                entityStates
+                    .forEach( entityState ->
+                              {
+                                  count++;
+                                  entityState.setPropertyValue( HasIdentity.IDENTITY_STATE_NAME,
+                                                                entityState.entityReference().identity()
);
+                                  states.add( entityState );
+                                  if( states.size() >= loadValue )
+                                  {
+                                      reindexState();
+                                  }
+                              } );
             }
+            reindexState();
         }
 
-        public void reindexState()
+        private void reindexState()
         {
             for( ServiceReference<StateChangeListener> listener : listeners )
             {

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ee1d1abc/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java
----------------------------------------------------------------------
diff --git a/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java
b/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java
index ae75fb4..1fccdde 100644
--- a/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java
+++ b/libraries/logging/src/test/java/org/apache/zest/library/logging/DebuggingTest.java
@@ -20,9 +20,8 @@
 
 package org.apache.zest.library.logging;
 
-import java.util.function.Function;
+import java.util.stream.Stream;
 import org.apache.zest.api.identity.Identity;
-import org.junit.Test;
 import org.apache.zest.api.injection.scope.This;
 import org.apache.zest.api.mixin.Mixins;
 import org.apache.zest.api.service.ServiceComposite;
@@ -31,8 +30,6 @@ import org.apache.zest.api.unitofwork.UnitOfWork;
 import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException;
 import org.apache.zest.bootstrap.AssemblyException;
 import org.apache.zest.bootstrap.ModuleAssembly;
-import org.apache.zest.io.Outputs;
-import org.apache.zest.io.Transforms;
 import org.apache.zest.library.logging.debug.Debug;
 import org.apache.zest.library.logging.debug.DebugConcern;
 import org.apache.zest.library.logging.debug.records.ServiceDebugRecordEntity;
@@ -42,6 +39,7 @@ import org.apache.zest.spi.entity.EntityState;
 import org.apache.zest.spi.entitystore.EntityStore;
 import org.apache.zest.test.AbstractZestTest;
 import org.apache.zest.test.EntityTestAssembler;
+import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
@@ -76,19 +74,18 @@ public class DebuggingTest
             assertEquals( message, "Hello!" );
             EntityStore es = serviceFinder.findService( EntityStore.class ).get();
             final Identity[] result = new Identity[1];
-            es.entityStates( module ).transferTo( Transforms.map( new Function<EntityState,
EntityState>()
-                    {
-                        public EntityState apply( EntityState entityState )
-                        {
-                            if( ServiceDebugRecordEntity.class.getName()
-                                    .equals( entityState.entityDescriptor().types().findFirst().get().getName()
) )
-                            {
-                                result[0] = entityState.entityReference().identity();
-                            }
-
-                            return entityState;
-                        }
-                    }, Outputs.<EntityState>noop() ));
+            try( Stream<EntityState> entityStates = es.entityStates( module ) )
+            {
+                entityStates
+                    .forEach( entityState ->
+                              {
+                                  if( ServiceDebugRecordEntity.class.getName().equals(
+                                      entityState.entityDescriptor().types().findFirst().get().getName()
) )
+                                  {
+                                      result[ 0 ] = entityState.entityReference().identity();
+                                  }
+                              } );
+            }
 
             ServiceDebugRecordEntity debugEntry = uow.get( ServiceDebugRecordEntity.class,
result[ 0 ] );
             String mess = debugEntry.message().get();

http://git-wip-us.apache.org/repos/asf/zest-java/blob/ee1d1abc/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java
----------------------------------------------------------------------
diff --git a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java
index 12a2bd4..e87c9c6 100644
--- a/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java
+++ b/libraries/sql/src/main/java/org/apache/zest/library/sql/common/SQLUtil.java
@@ -26,43 +26,94 @@ import java.sql.Statement;
 
 public class SQLUtil
 {
-
     public static void closeQuietly( ResultSet resultSet )
     {
-        if ( resultSet != null ) {
-            try {
+        closeQuietly( resultSet, null );
+    }
+
+    public static void closeQuietly( ResultSet resultSet, Throwable originalException )
+    {
+        if( resultSet != null )
+        {
+            try
+            {
                 resultSet.close();
-            } catch ( SQLException ignored ) {
+            }
+            catch( SQLException ignored )
+            {
+                if( originalException != null )
+                {
+                    originalException.addSuppressed( ignored );
+                }
             }
         }
     }
 
     public static void closeQuietly( Statement select )
     {
-        if ( select != null ) {
-            try {
+        closeQuietly( select, null );
+    }
+
+    public static void closeQuietly( Statement select, Throwable originalException )
+    {
+        if( select != null )
+        {
+            try
+            {
                 select.close();
-            } catch ( SQLException ignored ) {
+            }
+            catch( SQLException ignored )
+            {
+                if( originalException != null )
+                {
+                    originalException.addSuppressed( ignored );
+                }
             }
         }
     }
 
     public static void closeQuietly( Connection connection )
     {
-        if ( connection != null ) {
-            try {
+        closeQuietly( connection, null );
+    }
+
+    public static void closeQuietly( Connection connection, Throwable originalException )
+    {
+        if( connection != null )
+        {
+            try
+            {
                 connection.close();
-            } catch ( SQLException ignored ) {
+            }
+            catch( SQLException ignored )
+            {
+                if( originalException != null )
+                {
+                    originalException.addSuppressed( ignored );
+                }
             }
         }
     }
 
     public static void rollbackQuietly( Connection connection )
     {
-        if ( connection != null ) {
-            try {
+        rollbackQuietly( connection, null );
+    }
+
+    public static void rollbackQuietly( Connection connection, Throwable originalException
)
+    {
+        if( connection != null )
+        {
+            try
+            {
                 connection.rollback();
-            } catch ( SQLException ignored ) {
+            }
+            catch( SQLException ignored )
+            {
+                if( originalException != null )
+                {
+                    originalException.addSuppressed( ignored );
+                }
             }
         }
     }
@@ -70,5 +121,4 @@ public class SQLUtil
     private SQLUtil()
     {
     }
-
 }


Mime
View raw message