@Override public void increment(Delta<?> delta) { final String metricName = delta.getName(); final int amount = delta.getValue().intValue(); final Date timestamp = delta.getTimestamp(); this.metrics.update(metricName, new Callback<Metric<?>>() { @Override public Metric<?> modify(Metric<?> current) { if (current != null) { Metric<? extends Number> metric = current; return new Metric<Long>(metricName, metric.increment(amount).getValue(), timestamp); } else { return new Metric<Long>(metricName, Long.valueOf(amount), timestamp); } } }); }
@Override public void set(Metric<?> metric) { final String name = metric.getName(); final double value = metric.getValue().doubleValue(); this.repository.update(name, new Callback<RichGauge>() { @Override public RichGauge modify(RichGauge current) { if (current == null) { current = new RichGauge(name, value); } else { current.set(value); } return current; } }); }
@Override public void increment(Delta<?> delta) { final String metricName = delta.getName(); final int amount = delta.getValue().intValue(); final Date timestamp = delta.getTimestamp(); this.metrics.update(metricName, new Callback<Metric<?>>() { @Override public Metric<?> modify(Metric<?> current) { if (current != null) { Metric<? extends Number> metric = current; return new Metric<Long>(metricName, metric.increment(amount).getValue(), timestamp); } else { return new Metric<Long>(metricName, new Long(amount), timestamp); } } }); }
@Test public void updateExisting() { this.repository.set("foo", "spam"); this.repository.update("foo", new Callback<String>() { @Override public String modify(String current) { return "bar"; } }); assertThat(this.repository.findOne("foo")).isEqualTo("bar"); }
@Test public void updateNonexistent() { this.repository.update("foo", new Callback<String>() { @Override public String modify(String current) { return "bar"; } }); assertThat(this.repository.findOne("foo")).isEqualTo("bar"); }
@Override public Boolean call() throws Exception { this.repository.update("foo", new Callback<Integer>() { @Override public Integer modify(Integer current) { if (current == null) { return RepositoryUpdate.this.delta; } return current + RepositoryUpdate.this.delta; } }); return true; }
@Test public void updateExisting() { this.repository.set("foo", "spam"); this.repository.update("foo", new Callback<String>() { @Override public String modify(String current) { return "bar"; } }); assertEquals("bar", this.repository.findOne("foo")); }
@Test public void updateNonexistent() { this.repository.update("foo", new Callback<String>() { @Override public String modify(String current) { return "bar"; } }); assertEquals("bar", this.repository.findOne("foo")); }