@Test public void updateSingleTest() throws Exception { for (int i = 0; i < _test_ids.size(); i++) { Counter c = _counter_factory.getCounter(_test_ids.get(i)); Client client = new Client(c, _test_numOfIncrements.get(i)); client.start(); client.join(); assertEquals((int) _test_numOfIncrements.get(i), client .getCollectedValues().size()); assertEquals((int) _test_numOfIncrements.get(i), c.current()); } }
/** * Returns a counter associated with a given identifier. * * @param id the identifier. * @return a counter associated with a given identifier. */ public synchronized Counter getCounter(final String id) { Counter counter = _id2counter.get(id); if (counter == null) { counter = new Counter(id); _id2counter.put(id, counter); } return counter; }
/** * Builds a new counter with a given identifier. * * @param id the counter identifier. */ public Counter(final String id) { _id = id; try { sync(); if (_data == -1L) { _data = 0; _dao.set(_id, 0L); LOGGER.info(MessageCatalog._00080_INIT_COUNTER, id, _data); } } catch (final DataAccessLayerException exception) { LOGGER.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); throw new IllegalStateException(exception); } }
/** * Builds a new counter with a given identifier. * * @param id the counter identifier. */ public Counter(final String id) { _id = id; try { sync(); if (_data == -1L) { _data = 0; _dao.set(_id, 0L); LOGGER.info(MessageCatalog._00080_INIT_COUNTER, id, _data); } } catch (final DataAccessLayerException exception) { LOGGER.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); throw new IllegalStateException(exception); } }
/** * Increases the counter value. * * @param increment the increment. * @return the new value of the counter. * @throws DataAccessLayerException if the counter can not be incremented. */ public synchronized long increment(final long increment) throws DataAccessLayerException { _dao.increment(_id, increment); sync(); if (_data < 0) { LOGGER.error(MessageCatalog._00081_COUNTER_OVERFLOW, _id, _data); } return _data; }
/** * Increases the counter value. * * @param increment the increment. * @return the new value of the counter. * @throws DataAccessLayerException if the counter can not be incremented. */ public synchronized long increment(final long increment) throws DataAccessLayerException { _dao.increment(_id, increment); sync(); if (_data < 0) { LOGGER.error(MessageCatalog._00081_COUNTER_OVERFLOW, _id, _data); } return _data; }
@Override public void run() { for (int i = 0; i < _numOfincrements && !isInterrupted(); i++) { try { _collected.add(_counter.next()); } catch (Exception exception) { throw new RuntimeException(exception); } try { Thread.sleep((long) (Math.random() * 100)); } catch (InterruptedException e) { interrupt(); } } }
@Override public void update(final EventObject event) { try { if (event instanceof AddTripleEvent) { _triple_counter.increment(((AddTripleEvent)event).numOfChanges()); } else if (event instanceof RemoveTriplesEvent && _triple_counter.current() > 0) { _triple_counter.decrement(((RemoveTriplesEvent)event).numOfChanges()); } } catch (final DataAccessLayerException exception) { _log.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); } catch (final Exception exception) { _log.error(MessageCatalog._00026_NWS_SYSTEM_INTERNAL_FAILURE, exception); } } }
@Override public void update(final EventObject event) { try { if (event instanceof AddTripleEvent) { _triple_counter.increment(((AddTripleEvent)event).numOfChanges()); } else if (event instanceof RemoveTriplesEvent && _triple_counter.current() > 0) { _triple_counter.decrement(((RemoveTriplesEvent)event).numOfChanges()); } } catch (final DataAccessLayerException exception) { _log.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); } catch (final Exception exception) { _log.error(MessageCatalog._00026_NWS_SYSTEM_INTERNAL_FAILURE, exception); } } }
/** * Decreases the counter value. * * @param decrement the decrement. * @return the new value of the counter. * @throws DataAccessLayerException if the counter can not be decremented. */ public synchronized long decrement(final long decrement) throws DataAccessLayerException { _dao.decrement(_id, decrement); sync(); return _data; }
/** * Sets the counter to the given value. * * @param val the target counter value. * @throws DataAccessLayerException if the counter cannot be set. */ public synchronized void set(final long val) throws DataAccessLayerException { _dao.set(_id, val); sync(); }
/** * Decreases the counter value. * * @param decrement the decrement. * @return the new value of the counter. * @throws DataAccessLayerException if the counter can not be decremented. */ public synchronized long decrement(final long decrement) throws DataAccessLayerException { _dao.decrement(_id, decrement); sync(); return _data; }
@Test public void createCounterTest() throws Exception { for (String id : _test_ids) { final Counter c = _counter_factory.getCounter(id); assertNotNull(c); assertEquals(0l, c.current()); } }
/** * Increments the counter. * * @return the new value of the counter * @throws DataAccessLayerException if the counter can not be incremented. */ public synchronized long next() throws DataAccessLayerException { _dao.increment(_id, 1L); sync(); return _data; }
/** * Returns the current value of the counter. * @return the current value of the counter. * * @throws DataAccessLayerException if the counter value can not be read. */ public long current() throws DataAccessLayerException { sync(); return _data; }
@Override public long triplesCount() { try { return _triple_counter.current(); } catch (final DataAccessLayerException exception) { _log.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); } catch (final Exception exception) { _log.error(MessageCatalog._00026_NWS_SYSTEM_INTERNAL_FAILURE, exception); } return -1; }
/** * Returns a counter associated with a given identifier. * * @param id the identifier. * @return a counter associated with a given identifier. */ public synchronized Counter getCounter(final String id) { Counter counter = _id2counter.get(id); if (counter == null) { counter = new Counter(id); _id2counter.put(id, counter); } return counter; }
@Override public long triplesCount() { try { return _triple_counter.current(); } catch (final DataAccessLayerException exception) { _log.error(MessageCatalog._00093_DATA_ACCESS_LAYER_FAILURE, exception); } catch (final Exception exception) { _log.error(MessageCatalog._00026_NWS_SYSTEM_INTERNAL_FAILURE, exception); } return -1; }
/** * Increments the counter. * * @return the new value of the counter * @throws DataAccessLayerException if the counter can not be incremented. */ public synchronized long next() throws DataAccessLayerException { _dao.increment(_id, 1L); sync(); return _data; }
/** * Sets the counter to the given value. * * @param val the target counter value. * @throws DataAccessLayerException if the counter cannot be set. */ public synchronized void set(final long val) throws DataAccessLayerException { _dao.set(_id, val); sync(); }