@Test public void testJoinedIterables() { List<Iterable<Integer>> listOfIterables = new ArrayList<Iterable<Integer>>( ); List<Integer> twoElementList = Arrays.asList( 0, 1 ); listOfIterables.add( twoElementList ); List<Integer> emptyList = new ArrayList<Integer>( ); listOfIterables.add( emptyList ); List<Integer> oneElementList = Arrays.asList( 2 ); listOfIterables.add( oneElementList ); List<Integer> threeElementList = Arrays.asList( 3, 4, 5 ); listOfIterables.add( threeElementList ); JoinedIterable<Integer> joinedIterable = new JoinedIterable<Integer>( listOfIterables ); int i = 0; for ( Integer val : joinedIterable ) { assertEquals( Integer.valueOf( i ), val ); i++; } } }
@Test public void testNullIterables() { try { new JoinedIterable<String>( null ); fail(); } catch (NullPointerException ex) { // expected } }
@Test public void testSingleEmptyIterable() { Set<String> emptyList = new HashSet<String>(); List<Iterable<String>> iterableSets = new ArrayList<Iterable<String>>( ); iterableSets.add( emptyList ); Iterable<String> iterable = new JoinedIterable<String>( iterableSets ); assertFalse( iterable.iterator().hasNext() ); try { iterable.iterator().next(); fail( "Should have thrown NoSuchElementException because the underlying collection is empty."); } catch ( NoSuchElementException ex ) { // expected } try { iterable.iterator().remove(); fail( "Should have thrown IllegalStateException because the underlying collection is empty." ); } catch ( IllegalStateException ex ) { // expected } for ( String s : iterable ) { fail( "Should not have entered loop because underlying collection is empty"); } }
@Test public void testSingleIterableOfSingletonCollection() { final String str = "a string"; Set<String> singleTonSet = new HashSet<String>( 1 ); singleTonSet.add( str ); List<Iterable<String>> iterableSets = new ArrayList<Iterable<String>>( ); iterableSets.add( singleTonSet ); Iterable<String> iterable = new JoinedIterable<String>( iterableSets ); assertTrue( iterable.iterator().hasNext() ); assertSame( str, iterable.iterator().next() ); assertFalse( iterable.iterator().hasNext() ); try { iterable.iterator().next(); fail( "Should have thrown NoSuchElementException because the underlying collection is empty."); } catch ( NoSuchElementException ex ) { // expected } for ( String s : iterable ) { fail( "should not have entered loop because underlying iterator should have been exhausted." ); } assertEquals( 1, singleTonSet.size() ); iterable = new JoinedIterable<String>( iterableSets ); for ( String s : iterable ) { assertSame( str, s ); iterable.iterator().remove(); } assertTrue( singleTonSet.isEmpty() ); }
private Iterable<EntityBinding> getPreOrderSubEntityBindingClosure(boolean includeThis) { List<Iterable<EntityBinding>> iterables = new ArrayList<Iterable<EntityBinding>>(); if ( includeThis ) { iterables.add( java.util.Collections.singletonList( this ) ); } for ( EntityBinding subEntityBinding : subEntityBindings ) { Iterable<EntityBinding> subSubEntityBindingClosure = subEntityBinding.getPreOrderSubEntityBindingClosure( true ); if ( subSubEntityBindingClosure.iterator().hasNext() ) { iterables.add( subSubEntityBindingClosure ); } } return new JoinedIterable<EntityBinding>( iterables ); }
private Iterable<EntityBinding> getPreOrderSubEntityBindingClosure(boolean includeThis) { List<Iterable<EntityBinding>> iterables = new ArrayList<Iterable<EntityBinding>>(); if ( includeThis ) { iterables.add( java.util.Collections.singletonList( this ) ); } for ( EntityBinding subEntityBinding : subEntityBindings ) { Iterable<EntityBinding> subSubEntityBindingClosure = subEntityBinding.getPreOrderSubEntityBindingClosure( true ); if ( subSubEntityBindingClosure.iterator().hasNext() ) { iterables.add( subSubEntityBindingClosure ); } } return new JoinedIterable<EntityBinding>( iterables ); }
/** * Returns sub-EntityBinding objects in a special 'order', most derived subclasses * first. Specifically, the sub-entity bindings follow a depth-first, * post-order traversal * * Note that the returned value excludes this entity binding. * * @return sub-entity bindings ordered by those entity bindings that are most derived. */ public Iterable<EntityBinding> getPostOrderSubEntityBindingClosure() { // TODO: why this order? List<Iterable<EntityBinding>> subclassIterables = new ArrayList<Iterable<EntityBinding>>( subEntityBindings.size() + 1 ); for ( EntityBinding subEntityBinding : subEntityBindings ) { Iterable<EntityBinding> subSubEntityBindings = subEntityBinding.getPostOrderSubEntityBindingClosure(); if ( subSubEntityBindings.iterator().hasNext() ) { subclassIterables.add( subSubEntityBindings ); } } if ( ! subEntityBindings.isEmpty() ) { subclassIterables.add( subEntityBindings ); } return new JoinedIterable<EntityBinding>( subclassIterables ); }
/** * Returns sub-EntityBinding objects in a special 'order', most derived subclasses * first. Specifically, the sub-entity bindings follow a depth-first, * post-order traversal * * Note that the returned value excludes this entity binding. * * @return sub-entity bindings ordered by those entity bindings that are most derived. */ public Iterable<EntityBinding> getPostOrderSubEntityBindingClosure() { // TODO: why this order? List<Iterable<EntityBinding>> subclassIterables = new ArrayList<Iterable<EntityBinding>>( subEntityBindings.size() + 1 ); for ( EntityBinding subEntityBinding : subEntityBindings ) { Iterable<EntityBinding> subSubEntityBindings = subEntityBinding.getPostOrderSubEntityBindingClosure(); if ( subSubEntityBindings.iterator().hasNext() ) { subclassIterables.add( subSubEntityBindings ); } } if ( ! subEntityBindings.isEmpty() ) { subclassIterables.add( subEntityBindings ); } return new JoinedIterable<EntityBinding>( subclassIterables ); }
/** * Gets the attribute bindings for this EntityBinding and all of its * sub-EntityBinding, starting from the root of the hierarchy; includes * the identifier and attribute bindings defined as part of a join. * @return */ public Iterable<AttributeBinding> getSubEntityAttributeBindingClosure() { List<Iterable<AttributeBinding>> iterables = new ArrayList<Iterable<AttributeBinding>>(); iterables.add( getAttributeBindingClosure() ); for ( EntityBinding subEntityBinding : getPreOrderSubEntityBindingClosure() ) { // only add attribute bindings declared for the subEntityBinding iterables.add( subEntityBinding.attributeBindings() ); // TODO: if EntityBinding.attributeBindings() excludes joined attributes, then they need to be added here } return new JoinedIterable<AttributeBinding>( iterables ); }
/** * Gets the attribute bindings for this EntityBinding and all of its * sub-EntityBinding, starting from the root of the hierarchy; includes * the identifier and attribute bindings defined as part of a join. * @return */ public Iterable<AttributeBinding> getSubEntityAttributeBindingClosure() { List<Iterable<AttributeBinding>> iterables = new ArrayList<Iterable<AttributeBinding>>(); iterables.add( getAttributeBindingClosure() ); for ( EntityBinding subEntityBinding : getPreOrderSubEntityBindingClosure() ) { // only add attribute bindings declared for the subEntityBinding iterables.add( subEntityBinding.attributeBindings() ); // TODO: if EntityBinding.attributeBindings() excludes joined attributes, then they need to be added here } return new JoinedIterable<AttributeBinding>( iterables ); }
/** * Gets the attribute bindings defined on this class, including the * identifier attribute binding and attribute bindings defined * as part of a join. * * @return The attribute bindings. */ public Iterable<AttributeBinding> getAttributeBindingClosure() { // TODO: update size to account for joins Iterable<AttributeBinding> iterable; if ( superEntityBinding != null ) { List<Iterable<AttributeBinding>> iterables = new ArrayList<Iterable<AttributeBinding>>( 2 ); iterables.add( superEntityBinding.getAttributeBindingClosure() ); iterables.add( attributeBindings() ); iterable = new JoinedIterable<AttributeBinding>( iterables ); } else { iterable = attributeBindings(); } return iterable; }
/** * Gets the attribute bindings defined on this class, including the * identifier attribute binding and attribute bindings defined * as part of a join. * * @return The attribute bindings. */ public Iterable<AttributeBinding> getAttributeBindingClosure() { // TODO: update size to account for joins Iterable<AttributeBinding> iterable; if ( superEntityBinding != null ) { List<Iterable<AttributeBinding>> iterables = new ArrayList<Iterable<AttributeBinding>>( 2 ); iterables.add( superEntityBinding.getAttributeBindingClosure() ); iterables.add( attributeBindings() ); iterable = new JoinedIterable<AttributeBinding>( iterables ); } else { iterable = attributeBindings(); } return iterable; }