Example usage for java.lang Iterable iterator

List of usage examples for java.lang Iterable iterator

Introduction

In this page you can find the example usage for java.lang Iterable iterator.

Prototype

Iterator<T> iterator();

Source Link

Document

Returns an iterator over elements of type T .

Usage

From source file:org.openbaton.nfvo.core.test.NetworkServiceRecordManagementClassSuiteTest.java

@Test
public void nsrManagementQueryTest() {
    when(nsrRepository.findAll()).thenReturn(new ArrayList<NetworkServiceRecord>());
    Iterable<NetworkServiceRecord> nsds = nsrManagement.query();
    Assert.assertEquals(nsds.iterator().hasNext(), false);
    final NetworkServiceRecord nsd_exp = createNetworkServiceRecord();
    when(nsrRepository.findAll()).thenReturn(new ArrayList<NetworkServiceRecord>() {
        {//  w  ww .  j av  a 2 s.c  om
            add(nsd_exp);
        }
    });
    nsds = nsrManagement.query();
    Assert.assertEquals(nsds.iterator().hasNext(), true);

    when(nsrRepository.findOne(nsd_exp.getId())).thenReturn(nsd_exp);
    assertEqualsNSR(nsd_exp);
}

From source file:com.ebay.erl.mobius.core.collection.BigTupleList.java

/**
 * Add all the tuples in <code>collection</code> into
 * this list.//from ww w.  ja  v a2 s. co  m
 * 
 * @throws UnsupportedOperationException if this list is immutable.
 */
public void addAll(Iterable<Tuple> collection) {
    Iterator<Tuple> it = collection.iterator();
    while (it.hasNext()) {
        this.add(it.next());
    }

    if (it instanceof Closeable) {
        try {
            ((Closeable) it).close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:thingynet.workflow.WorkflowServiceTest.java

@Test
public void processShouldRemoveWorkflowAndLogErrorWhenNodeIsNull() {
    Workflow workflow = workflowService.createWorkflow(TEST_WORKFLOW, null, start, STRING_VALUE, PROCESSING);

    workflowService.process(workflow);/*  w ww . j a  v a  2  s .c  o m*/

    Workflow gone = workflowCollection.findOne(workflow.getId()).as(Workflow.class);
    assertThat(gone, nullValue());

    Iterable<WorkflowError> errors = workflowLogCollection.find().as(WorkflowError.class);
    assertThat(errors, notNullValue());
    WorkflowError error = errors.iterator().next();
    assertThat(error, notNullValue());

    assertThat(error.getName(), is(TEST_WORKFLOW));
    assertThat(error.getCommand(), nullValue());
    assertThat(error.getCreated(), greaterThanOrEqualTo(error.getStart()));
    assertThat(error.getStart(), greaterThanOrEqualTo(start));
    assertThat(error.getUpdated(), is(workflow.getUpdated()));
    assertThat(error.getClassification(), is(ERR_NODE));
    assertThat(error.getMessage(), is(WORKFLOW_NODE_MISSING));
}

From source file:thingynet.workflow.WorkflowServiceTest.java

@Test
public void processShouldRemoveWorkflowAndLogErrorWhenCurrentIsNull() {
    Workflow workflow = workflowService.createWorkflow(TEST_WORKFLOW, null, start, STRING_VALUE, PROCESSING);

    workflowService.process(workflow);/*from   w  w w  . j  a v  a 2 s .  c  om*/

    Workflow gone = workflowCollection.findOne(workflow.getId()).as(Workflow.class);
    assertThat(gone, nullValue());

    Iterable<WorkflowError> errors = workflowLogCollection.find().as(WorkflowError.class);
    assertThat(errors, notNullValue());
    WorkflowError error = errors.iterator().next();
    assertThat(error, notNullValue());

    assertThat(error.getName(), is(TEST_WORKFLOW));
    assertThat(error.getCommand(), nullValue());
    assertThat(error.getCreated(), greaterThanOrEqualTo(error.getStart()));
    assertThat(error.getStart(), is(workflow.getStart()));
    assertThat(error.getUpdated(), is(workflow.getUpdated()));
    assertThat(error.getClassification(), is(ERR_NODE));
    assertThat(error.getMessage(), is(WORKFLOW_NODE_MISSING));
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@Test
public void annotatedElementsMembers2Classes() throws SecurityException, NoSuchMethodException {
    ClassCriteria classCriteria = new ClassCriteria();
    classCriteria.setSelection(ClassType.CLASSES);
    memberCriteria.membersOfType(Method.class);
    memberCriteria.named("size");
    Iterable<Class<?>> classIterable = classCriteria.getIterable(ArrayList.class);
    Iterable<? extends AnnotatedElement> annotatedElementIterable = memberCriteria
            .getAnnotatedElementIterable(classIterable, IterateStrategy.MEMBERS_CLASS);
    Iterator<? extends AnnotatedElement> iterator = annotatedElementIterable.iterator();
    assertTrue(iterator.hasNext());/*ww w  .  j a va  2s  .c  om*/
    AnnotatedElement next = iterator.next();
    assertEquals(ArrayList.class.getDeclaredMethod("size"), next);
    next = iterator.next();
    assertEquals(ArrayList.class, next);
    next = iterator.next();
    assertEquals(AbstractList.class, next);
    next = iterator.next();
    assertEquals(AbstractCollection.class.getDeclaredMethod("size"), next);
    next = iterator.next();
    assertEquals(AbstractCollection.class, next);
    next = iterator.next();
    assertEquals(Object.class, next);
    assertFalse(iterator.hasNext());
}

From source file:thingynet.workflow.WorkflowServiceTest.java

@Test
public void processShouldRemoveWorkflowAndLogErrorWhenCommandIsNull() {
    WorkflowNode noCommandWorkflowNode = new WorkflowNode(NO_COMMAND, null);
    workflowNodeCollection.save(noCommandWorkflowNode);
    Workflow workflow = workflowService.createWorkflow(TEST_WORKFLOW, NO_COMMAND, start, STRING_VALUE,
            PROCESSING);//from   w  w w  .  ja v  a  2  s .  c  o  m

    workflowService.process(workflow);

    Workflow gone = workflowCollection.findOne(workflow.getId()).as(Workflow.class);
    assertThat(gone, nullValue());

    Iterable<WorkflowError> errors = workflowLogCollection.find().as(WorkflowError.class);
    assertThat(errors, notNullValue());
    WorkflowError error = errors.iterator().next();
    assertThat(error, notNullValue());
    assertThat(error.getName(), is(TEST_WORKFLOW));
    assertThat(error.getCommand(), is(NO_COMMAND));
    assertThat(error.getCreated(), greaterThanOrEqualTo(error.getStart()));
    assertThat(error.getStart(), is(workflow.getStart()));
    assertThat(error.getUpdated(), is(workflow.getUpdated()));
    assertThat(error.getClassification(), is(ERR_COMMAND));
    assertThat(error.getMessage(), is(WORKFLOW_COMMAND_FACTORY_RETURNED_NULL));
}

From source file:thingynet.workflow.WorkflowServiceTest.java

@Test
public void processShouldRemoveWorkflowAndLogErrorWhenCommandThrowsCommandException() {
    WorkflowNode exceptionWorkflowNode = new WorkflowNode(EXCEPTION_WORKFLOW_COMMAND, null);
    workflowNodeCollection.save(exceptionWorkflowNode);
    Workflow workflow = workflowService.createWorkflow(TEST_WORKFLOW, EXCEPTION_WORKFLOW_COMMAND, start,
            STRING_VALUE, PROCESSING);/* w  w w .j av  a 2 s.c o  m*/

    workflowService.process(workflow);

    Workflow gone = workflowCollection.findOne(workflow.getId()).as(Workflow.class);
    assertThat(gone, nullValue());

    Iterable<WorkflowError> errors = workflowLogCollection.find().as(WorkflowError.class);
    assertThat(errors, notNullValue());
    WorkflowError error = errors.iterator().next();
    assertThat(error, notNullValue());

    assertThat(error.getName(), is(TEST_WORKFLOW));
    assertThat(error.getCommand(), is(EXCEPTION_WORKFLOW_COMMAND));
    assertThat(error.getCreated(), greaterThanOrEqualTo(error.getStart()));
    assertThat(error.getStart(), is(workflow.getStart()));
    assertThat(error.getUpdated(), is(workflow.getUpdated()));
    assertThat(error.getClassification(), is(ERR_TEST));
    assertThat(error.getMessage(), is(WORKFLOW_COMMAND_THREW_EXCEPTION));
}

From source file:com.transwarp.hbase.bulkload.withindex.TextWithIndexSortReducer.java

@Override
protected void reduce(ImmutableBytesWritable rowKey, java.lang.Iterable<Text> lines,
        Reducer<ImmutableBytesWritable, Text, ImmutableBytesWritable, KeyValue>.Context context)
        throws java.io.IOException, InterruptedException {
    // although reduce() is called per-row, handle pathological case
    long threshold = context.getConfiguration().getLong("reducer.row.threshold", 1L * (1 << 30));
    Iterator<Text> iter = lines.iterator();
    boolean qualifier = context.getConfiguration().getBoolean("indexqualifier", false);
    while (iter.hasNext()) {
        // Get the prefix to judge whethre primary table(Prefix == 0) or index table (prefix  > 0)
        int rowkeyPrefix = Bytes.toInt(rowKey.get(), 0, 4);
        byte[] rowKeyWithoutPrefix = Bytes.tail(rowKey.get(), rowKey.get().length - 4);
        Set<KeyValue> map = new TreeSet<KeyValue>(KeyValue.COMPARATOR);
        long curSize = 0;
        // stop at the end or the RAM threshold
        while (iter.hasNext() && curSize < threshold) {
            Text line = iter.next();
            String lineStr = line.toString();
            try {
                Put p = null;/*from   w w w.  j a va 2s . c o  m*/
                if (rowkeyPrefix == 0) {
                    ArrayList<String> parsedLine = ParsedLine.parse(converter.getRecordSpec(), lineStr);

                    p = converter.convert(parsedLine, rowKeyWithoutPrefix);
                } else {
                    p = new Put(rowKeyWithoutPrefix);
                    if (qualifier) {
                        p.add(family, line.getBytes(), emptyByte);
                    } else {
                        p.add(family, this.qualifier, line.getBytes());
                    }
                }

                if (p != null) {
                    for (List<KeyValue> kvs : p.getFamilyMap().values()) {
                        for (KeyValue kv : kvs) {
                            map.add(kv);
                            curSize += kv.getLength();
                        }
                    }
                }
            } catch (FormatException badLine) {
                if (skipBadLines) {
                    System.err.println("Bad line." + badLine.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(badLine);
            } catch (IllegalArgumentException e) {
                if (skipBadLines) {
                    System.err.println("Bad line." + e.getMessage());
                    incrementBadLineCount(1);
                    return;
                }
                throw new IOException(e);
            }
        }
        context.setStatus("Read " + map.size() + " entries of " + map.getClass() + "("
                + StringUtils.humanReadableInt(curSize) + ")");
        int index = 0;
        for (KeyValue kv : map) {
            context.write(rowKey, kv);
            if (++index > 0 && index % 100 == 0)
                context.setStatus("Wrote " + index + " key values.");
        }

        // if we have more entries to process
        if (iter.hasNext()) {
            // force flush because we cannot guarantee intra-row sorted order
            context.write(null, null);
        }
    }
}

From source file:com.allogy.mime.MimeGeneratingInputStream.java

public MimeGeneratingInputStream(Iterable<Header> headers, InputStream bodyInputStream) {
    Iterable<InputStream> inputStreamHeaders = Iterables.transform(headers,
            new Function<Header, InputStream>() {
                public InputStream apply(@Nullable Header s) {
                    InputStream headerStream = new ByteArrayInputStream(s.toString().getBytes());
                    return new SequenceInputStream(headerStream,
                            new ByteArrayInputStream(MimeUtilities.CRLFEnding.getBytes()));
                }//  w ww  .  j a v a2 s  . c o  m
            });

    InputStream givenHeadersInputStream = new SequenceInputStream(
            new IteratorEnumeration(inputStreamHeaders.iterator()));

    InputStream headerInputStream = new SequenceInputStream(givenHeadersInputStream,
            new ByteArrayInputStream(MimeUtilities.CRLFEnding.getBytes()));

    innerInputStream = new SequenceInputStream(headerInputStream, bodyInputStream);
}

From source file:me.cybermaxke.merchants.v17r4.SMerchant.java

protected void sendUpdateWithSerializer(PacketDataSerializer serializer, Iterable<EntityPlayer> players) {
    // Write the recipe list
    this.offers.a(serializer);

    // Send a packet to all the players
    Iterator<EntityPlayer> it = players.iterator();
    while (it.hasNext()) {
        EntityPlayer player0 = it.next();

        // Every player has a different window id
        PacketDataSerializer content1 = new PacketDataSerializer(Unpooled.buffer());
        content1.writeInt(player0.activeContainer.windowId);
        content1.writeBytes(serializer);

        player0.playerConnection.sendPacket(new PacketPlayOutCustomPayload("MC|TrList", content1));
    }/*  w  w  w  .  j  a  v  a 2  s .c om*/
}