Example usage for org.apache.commons.lang.mutable MutableInt MutableInt

List of usage examples for org.apache.commons.lang.mutable MutableInt MutableInt

Introduction

In this page you can find the example usage for org.apache.commons.lang.mutable MutableInt MutableInt.

Prototype

public MutableInt() 

Source Link

Document

Constructs a new MutableInt with the default value of zero.

Usage

From source file:com.topekalabs.bigmachine.lib.app.SimpleContinuationTest.java

@Test
public void simpleTest() {
    MutableInt context = new MutableInt();
    Continuation c = Continuation.startWith(new SimpleContinuationRunnable(), context);

    Assert.assertEquals("The value of the context was not set properly", SimpleContinuationRunnable.FIRST_VALUE,
            context.getValue());//  ww w  .  ja va  2s. com

    Continuation.continueWith(c, context);

    Assert.assertEquals("The value of the context was not set properly",
            SimpleContinuationRunnable.SECOND_VALUE, context.getValue());
}

From source file:com.topekalabs.bigmachine.lib.app.ContinuationSerializationTest.java

@Test
public void simpleSerializationTest() throws Exception {
    MutableInt context = new MutableInt();
    Continuation c = Continuation.startWith(new SimpleContinuationRunnable(), context);
    logger.debug("Is serializable: {}", c.isSerializable());

    Assert.assertEquals("The value of the context was not set properly", SimpleContinuationRunnable.FIRST_VALUE,
            context.getValue());/*from  w  w w .jav  a  2  s.c  o  m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(c);
    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    c = (Continuation) ois.readObject();
    ois.close();

    Continuation.continueWith(c, context);

    Assert.assertEquals("The value of the context was not set properly",
            SimpleContinuationRunnable.SECOND_VALUE, context.getValue());
}

From source file:com.novartis.pcs.ontology.service.parser.obo.SynonymTagHandler.java

@Override
void handleTagValue(String tag, String value, String qualifierBlock, String comment) {
    MutableInt fromIndex = new MutableInt();
    String synonym = substr(value, '"', '"', fromIndex);

    if (synonym == null) {
        throw new InvalidFormatException("Invalid OBO synonym tag (missing quotes): " + value);
    }//from  ww w  .  j ava  2  s . c o m

    synonym = context.unescapeTagValue(synonym);
    Synonym.Type type = parseType(value, fromIndex);

    String xrefList = substr(value, '[', ']', fromIndex);
    if (xrefList != null && xrefList.length() > 0) {
        List<String> xrefs = split(xrefList, ',');
        for (String xref : xrefs) {
            int colon = indexOf(xref, ':', 0);
            if (colon == -1) {
                throw new InvalidFormatException("Invalid OBO synonym xref (no datasource): " + xref);
            }
            String datasource = xref.substring(0, colon);
            int quote = indexOf(xref, '"', colon + 1);
            String refId = quote == -1 ? xref.substring(colon + 1) : xref.substring(colon + 1, quote);

            datasource = context.unescapeTagValue(datasource);
            refId = context.unescapeTagValue(refId);

            Synonym synonymObj = null;

            if (UrlParser.isValidProtocol(datasource)) {
                String url = datasource + ":" + refId;
                synonymObj = context.addSynonym(synonym, type, url);
            } else {
                synonymObj = context.addSynonym(synonym, type, datasource, refId);
            }

            if (quote != -1) {
                fromIndex.setValue(quote);
                String description = substr(xref, '"', '"', fromIndex);
                description = context.unescapeTagValue(description);
                synonymObj.setDescription(description);
            }
        }
    } else {
        context.addSynonym(synonym, type);
    }
}

From source file:com.novartis.pcs.ontology.service.parser.obo.DefTagHandler.java

@Override
void handleTagValue(String tag, String value, String qualifierBlock, String comment) {

    MutableInt fromIndex = new MutableInt();
    String definition = substr(value, '"', '"', fromIndex);

    if (definition == null) {
        throw new InvalidFormatException("Invalid OBO def tag (missing quotes): " + value);
    }// w w  w  .  ja  v  a 2  s . c  o  m

    definition = context.unescapeTagValue(definition);

    VersionedEntity entity = context.getCurrentEntity();
    if (entity instanceof Term) {
        Term term = (Term) entity;
        term.setDefinition(definition);
        String xrefList = substr(value, '[', ']', fromIndex);
        if (xrefList != null) {
            List<String> xrefs = split(xrefList, ',');
            for (String xref : xrefs) {
                int colon = indexOf(xref, ':', 0);
                if (colon == -1) {
                    throw new InvalidFormatException("Invalid OBO xref (no datasource): " + xref);
                }
                String datasourceAcronym = xref.substring(0, colon);
                int quote = indexOf(xref, '"', colon + 1);
                String refId = quote == -1 ? xref.substring(colon + 1) : xref.substring(colon + 1, quote);

                datasourceAcronym = context.unescapeTagValue(datasourceAcronym);
                refId = context.unescapeTagValue(refId);
                CrossReference defXref = null;

                if (UrlParser.isValidProtocol(datasourceAcronym)) {
                    String url = datasourceAcronym + ":" + refId;
                    defXref = addCrossReference(url, true);
                } else {
                    defXref = addCrossReference(datasourceAcronym, refId, true);
                }

                if (quote != -1) {
                    fromIndex.setValue(quote);
                    String description = substr(xref, '"', '"', fromIndex);
                    description = context.unescapeTagValue(description);
                    defXref.setDescription(description);
                }
            }
        }
    } else if (entity instanceof RelationshipType) {
        RelationshipType relationshipType = (RelationshipType) entity;
        relationshipType.setDefintion(definition);
    }
}

From source file:com.aionlightning.commons.services.CronServiceTest.java

@Test
public void testCronTriggerExecutionTime() throws Exception {

    MutableInt ref = new MutableInt();
    Runnable test = newIncrementingRunnable(ref);

    // should run on second # 0 and every 2 seconds
    // execute on 0, 2, 4...

    cronService.schedule(test, "0/2 * * * * ?");
    sleep(5);//  w  w  w  .ja va2s  .  co m
    int count = ref.intValue();
    assertEquals(count, 3);
}

From source file:com.jivesoftware.os.tasmo.configuration.events.TenantEventsProvider.java

public void loadModel(TenantId tenantId) {
    ChainedVersion currentVersion = eventsProvider.getCurrentEventsVersion(tenantId);
    if (currentVersion == ChainedVersion.NULL) {
        versionedEventsModels.put(tenantId, new VersionedEventsModel(currentVersion, null));
    } else {/*from w w w . ja  v a2  s .  c om*/
        VersionedEventsModel currentVersionedEventModel = versionedEventsModels.get(tenantId);
        if (currentVersionedEventModel == null
                || !currentVersionedEventModel.getVersion().equals(currentVersion)) {

            final MutableInt errors = new MutableInt();
            final EventsModel newEventsModel = new EventsModel();
            List<ObjectNode> events = eventsProvider
                    .getEvents(new EventsProcessorId(tenantId, "NotBeingUsedYet"));
            for (ObjectNode event : events) {
                try {
                    newEventsModel.addEvent(event);
                } catch (Exception x) {
                    LOG.error("Failed to load event for " + event, x);
                    throw new RuntimeException("Failed to load (" + errors.longValue() + ") event/s. ");
                }
            }
            versionedEventsModels.put(tenantId, new VersionedEventsModel(currentVersion, newEventsModel));

        } else {
            LOG.debug("Didn't reload because event model versions are equal.");
        }
    }
}

From source file:fr.inria.eventcloud.adapters.rdf2go.listeners.Rdf2GoCompoundEventNotificationListenerTest.java

@Test
public void test() {
    Node graph = NodeFactory.createURI("http://example.org/graph");

    Builder<Quadruple> builder = new ImmutableList.Builder<Quadruple>();

    for (int i = 0; i < 10; i++) {
        builder.add(QuadrupleGenerator.random(graph));
    }//from  w w w  .  j  a  va 2s .  c  o m

    final List<Quadruple> quadruples = builder.build();
    CompoundEvent ce = new CompoundEvent(quadruples);

    final MutableInt nbCalls = new MutableInt();

    Rdf2GoCompoundEventNotificationListener listener = new Rdf2GoCompoundEventNotificationListener() {

        private static final long serialVersionUID = 160L;

        @Override
        public void handle(SubscriptionId id, Model solution) {
            ClosableIterator<Statement> it = solution
                    .findStatements(solution.createTriplePattern(Variable.ANY, Variable.ANY, Variable.ANY));

            while (it.hasNext()) {
                Statement stmt = it.next();

                Quadruple q = new Quadruple(TypeConversion.toJenaNode(stmt.getContext()),
                        TypeConversion.toJenaNode(stmt.getSubject()),
                        TypeConversion.toJenaNode(stmt.getPredicate()),
                        TypeConversion.toJenaNode(stmt.getObject()));

                Assert.assertTrue(quadruples.contains(q));
            }

            nbCalls.increment();

            solution.close();
        }
    };

    listener.onNotification(new SubscriptionId(), ce);

    Assert.assertEquals(1, nbCalls.intValue());
}

From source file:com.datatorrent.contrib.hdht.MockFileAccess.java

@Override
public FileReader getReader(final long bucketKey, final String fileName) throws IOException {
    final HashMap<Slice, Pair<byte[], Integer>> data = Maps.newHashMap();
    final ArrayList<Slice> keys = Lists.newArrayList();
    final MutableInt index = new MutableInt();

    DataInputStream is = getInputStream(bucketKey, fileName);
    Input input = new Input(is);
    while (!input.eof()) {
        byte[] keyBytes = kryo.readObject(input, byte[].class);
        byte[] value = kryo.readObject(input, byte[].class);
        Slice key = new Slice(keyBytes, 0, keyBytes.length);
        data.put(key, new Pair<byte[], Integer>(value, keys.size()));
        keys.add(key);/*from   w  w w .  j av  a 2  s.c o m*/
    }
    input.close();
    is.close();

    return new FileReader() {

        @Override
        public void readFully(TreeMap<Slice, Slice> result) throws IOException {
            for (Map.Entry<Slice, Pair<byte[], Integer>> e : data.entrySet()) {
                result.put(e.getKey(), new Slice(e.getValue().first));
            }
        }

        @Override
        public void reset() throws IOException {
            index.setValue(0);
        }

        @Override
        public boolean seek(Slice key) throws IOException {
            Pair<byte[], Integer> v = data.get(key);
            if (v == null) {
                index.setValue(0);
                return false;
            }
            index.setValue(v.second);
            return true;
        }

        @Override
        public boolean next(Slice key, Slice value) throws IOException {

            if (deletedFiles.contains("" + bucketKey + fileName)) {
                throw new IOException("Simulated error for deleted file: " + fileName);
            }

            int pos = index.intValue();
            if (pos < keys.size()) {
                Slice k = keys.get(pos);
                key.buffer = k.buffer;
                key.offset = k.offset;
                key.length = k.length;
                Pair<byte[], Integer> v = data.get(k);
                value.buffer = v.first;
                value.offset = 0;
                value.length = v.first.length;
                index.increment();
                return true;
            }
            return false;
        }

        @Override
        public void close() throws IOException {
        }
    };
}

From source file:com.aionlightning.commons.services.CronServiceTest.java

@Test
public void testCancelRunnableUsingRunnableReference() throws Exception {
    final MutableInt val = new MutableInt();
    Runnable test = new Runnable() {
        @Override//ww  w .  j  av  a2  s.co  m
        public void run() {
            val.increment();
            cronService.cancel(this);
        }
    };
    cronService.schedule(test, "0/2 * * * * ?");
    sleep(5);
    assertEquals(val.intValue(), 1);
}

From source file:mitm.common.hibernate.DatabaseActionExecutorImplTest.java

@Test
public void testRetry0() throws Exception {
    final MutableInt count = new MutableInt();

    final int retries = 0;

    try {//from  w  w w.  ja  v a2s  . c o m
        databaseActionExecutor.executeTransaction(new DatabaseAction<Integer>() {
            @Override
            public Integer doAction(Session session) throws DatabaseException {
                count.increment();

                throw new ConstraintViolationException("Dummy ConstraintViolationException", null, "");
            }
        }, retries);

        fail();
    } catch (ConstraintViolationException e) {
        // ignore
    }

    assertEquals(1, count.intValue());
}