Example usage for java.util ArrayDeque offer

List of usage examples for java.util ArrayDeque offer

Introduction

In this page you can find the example usage for java.util ArrayDeque offer.

Prototype

public boolean offer(E e) 

Source Link

Document

Inserts the specified element at the end of this deque.

Usage

From source file:bachelorthesis.captchabuilder.builder.CaptchaBuildSequenceParser.java

/**
 * Creates new elementCreators from the list CaptchaElementCreatorBuilders
 * in the CaptchaBuilder.//from   w  w  w .  j a  va 2s . c  o m
 *
 * @param builder
 *                <p/>
 * @throws ParseException
 */
public static void shortParse(CaptchaBuilder builder) throws ParseException {
    ArrayDeque<CaptchaElementCreatorBuilder> elementBuilders = builder.getBuilders().clone();
    ArrayDeque<BuildSequenceOptions> sequence = new ArrayDeque<>();
    for (String lvl1Arg : builder.getBuildSequence().split(CaptchaConstants.buildSequencelvl1Delim)) {
        if (!lvl1Arg.isEmpty()) {
            try {
                String[] optionArgs = lvl1Arg.split(CaptchaConstants.buildSequencelvl2Delim);
                sequence.offer(BuildSequenceOptions.valueOf(optionArgs[0]));
            } catch (IllegalArgumentException e) {
                throw new ParseException(e.getMessage());
            }
        }
    }

    for (BuildSequenceOptions buildSequence : sequence) {
        switch (buildSequence) {
        case BACKGROUND: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof BackgroundProducerBuilder) {
                builder.addBackground((BackgroundProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + BackgroundProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case BORDER: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof BorderProducerBuilder) {
                builder.addBorder((BorderProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + BorderProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case GIMP: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof GimpyRendererBuilder) {
                builder.gimp((GimpyRenderer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + GimpyRendererBuilder.class.getSimpleName());
            }
        }
            break;
        case NOISE: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof NoiseProducerBuilder) {
                builder.addNoise((NoiseProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + NoiseProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case TEXT: {
            CaptchaElementCreatorBuilder elementBuilder1 = elementBuilders.poll();
            CaptchaElementCreatorBuilder elementBuilder2 = elementBuilders.poll();
            if (elementBuilder1 instanceof TextProducerBuilder
                    && elementBuilder2 instanceof WordRendererBuilder) {
                builder.addText((TextProducer) elementBuilder1.create(),
                        (WordRenderer) elementBuilder2.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder1.getClass().getSimpleName() + " and expected "
                        + TextProducerBuilder.class.getSimpleName() + "\n" + "Class Mismatch: Got "
                        + elementBuilder2.getClass().getSimpleName() + " and expected "
                        + WordRendererBuilder.class.getSimpleName());
            }
        }
        }
    }
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testOffer() {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertTrue(deque.offer(o1));
    checkDequeSizeAndContent(deque, o1);
    assertTrue(deque.offer(o2));/*from  w  w  w . jav a  2s  .  co  m*/
    checkDequeSizeAndContent(deque, o1, o2);
    assertTrue(deque.offer(o3));
    checkDequeSizeAndContent(deque, o1, o2, o3);

    try {
        deque.offer(null);
        fail();
    } catch (NullPointerException expected) {
    }
}