下面列出了怎么用com.google.common.util.concurrent.AtomicDouble的API类实例代码及写法,或者点击链接到github查看源代码。
public CrossTable computeCrossTable() {
int totalCoOccurrences = 0;
Map<Term, AtomicDouble> aPlusB = Maps.newConcurrentMap() ;
Map<Term, AtomicDouble> aPlusC = Maps.newConcurrentMap();
Term term;
for (Iterator<TermService> it1 = terminology.getTerms().iterator();
it1.hasNext() ;) {
term = it1.next().getTerm();
// this.totalFrequency++;
if(term.getContext() == null)
continue;
ContextVector context = term.getContext();
for (ContextVector.Entry entry : context.getEntries()) {
totalCoOccurrences += entry.getNbCooccs();
getScoreFromMap(aPlusB, term).addAndGet(entry.getNbCooccs());
getScoreFromMap(aPlusC, entry.getCoTerm()).addAndGet(entry.getNbCooccs());
}
}
return new CrossTable(aPlusB, aPlusC, totalCoOccurrences);
}
/**
* {@inheritDoc}
*/
@Override
public void runLearning() {
efBayesianNetwork = new EF_BayesianNetwork(dag);
dataInstanceCount = new AtomicDouble(0);
sumSS = efBayesianNetwork.createZeroSufficientStatistics();
for (DataOnMemory<DataInstance> batch : dataStream.iterableOverBatches(windowsSize)){
SufficientStatistics batchSS = batch.stream()
.map(efBayesianNetwork::getSufficientStatistics)
.reduce(SufficientStatistics::sumVectorNonStateless).get();
sumSS.multiplyBy(fadingFactor);
sumSS.sum(batchSS);
dataInstanceCount.set(dataInstanceCount.get()*fadingFactor + windowsSize);
}
}
public ClutchController(StageScalingPolicy.ScalingReason metric, StageSchedulingInfo stageSchedulingInfo, ClutchPIDConfig config, AtomicDouble gainFactor, long initialSize, long min, long max) {
this.metric = metric;
this.config = config;
this.gainFactor = gainFactor;
this.initialSize = initialSize;
this.stageSchedulingInfo = stageSchedulingInfo;
this.min = min;
this.max = max;
this.integrator = new Integrator(this.initialSize, this.min - 1, this.max + 1);
}
public AreaShareChart(@Nonnull ModularFeatureListRow row, AtomicDouble progress) {
Float sum = row.streamFeatures().map(ModularFeature::getArea).filter(p -> p.getValue() != null)
.map(Property<Float>::getValue).reduce(0f, Float::sum);
List<Rectangle> all = new ArrayList<>();
int i = 0;
int size = row.getFeatures().size();
for (Entry<RawDataFile, ModularFeature> entry : row.getFeatures().entrySet()) {
Property<Float> areaProperty = entry.getValue().get(AreaType.class);
if (areaProperty.getValue() != null) {
// color from sample
Color color = entry.getValue().get(RawColorType.class).getValue();
if (color == null)
color = Color.DARKORANGE;
float ratio = areaProperty.getValue() / sum;
Rectangle rect = new Rectangle();
rect.setFill(color);
// bind width
rect.widthProperty().bind(this.widthProperty().multiply(ratio));
rect.setHeight(i % 2 == 0 ? 20 : 25);
all.add(rect);
i++;
if (progress != null)
progress.addAndGet(1.0 / size);
}
}
HBox box = new HBox(0, all.toArray(Rectangle[]::new));
box.setPrefWidth(100);
box.setAlignment(Pos.CENTER_LEFT);
this.getChildren().add(box);
}
public AreaBarChart(@Nonnull ModularFeatureListRow row, AtomicDouble progress) {
XYChart.Series data = new XYChart.Series();
int i = 1;
int size = row.getFeatures().size();
for (Entry<RawDataFile, ModularFeature> entry : row.getFeatures().entrySet()) {
Property<Float> areaProperty = entry.getValue().get(AreaType.class);
data.getData().add(
new XYChart.Data("" + i, areaProperty.getValue() == null ? 0f : areaProperty.getValue()));
i++;
if (progress != null)
progress.addAndGet(1.0 / size);
}
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
bc.setLegendVisible(false);
bc.setMinHeight(100);
bc.setPrefHeight(100);
bc.setMaxHeight(100);
bc.setBarGap(3);
bc.setCategoryGap(3);
bc.setPrefWidth(150);
bc.getData().addAll(data);
this.getChildren().add(bc);
}
private void drawHistory() {
AtomicDouble offset = new AtomicDouble();
messageHistory
.stream()
.limit(100)
.forEach(
str -> {
MC.fontRenderer.drawString(
str, 5, (this.height - 50 - offset.intValue()), Colors.WHITE.toBuffer());
offset.addAndGet(10);
});
}
public static void walk( final Context context, S2LatLng end) { // PokeStop Walking
context.getWalking().set(true);
S2LatLng start = S2LatLng.fromDegrees(context.getLat().get(), context.getLng().get());
S2LatLng diff = end.sub(start);
double distance = start.getEarthDistance(end);
long timeout = 200L;
double timeRequired = distance / Config.getSpeed();
final AtomicDouble stepsRequired = new AtomicDouble(timeRequired / (timeout / 1000D));
double deltaLat = diff.latDegrees() / stepsRequired.get();
double deltaLng = diff.lngDegrees() / stepsRequired.get();
logger.debug("starting to walk");
//Schedule a timer to walk every 200 ms
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
context.getApi().setLocation(context.getLat().addAndGet(deltaLat), context.getLng().addAndGet(deltaLng), new Random().nextInt(10));
stepsRequired.getAndAdd(-1);
if (stepsRequired.get() <= 0) {
logger.debug("Setting a new destination");
context.getWalking().set(false);
context.addTask(new TagPokestop(context));
context.addTask(new Navigate(context, new LatLng(context.getLat().get() - VARIANCE, context.getLng().get() - VARIANCE),
new LatLng(context.getLat().get() + VARIANCE, context.getLng().get() + VARIANCE)));
cancel();
}
}
}, 0, timeout);
}
private static IExpr convert(Number n) {
if (n instanceof Integer) {
return F.ZZ(((Integer) n).longValue());
}
if (n instanceof Double) {
return F.num(((Double) n).doubleValue());
}
if (n instanceof Long) {
return F.ZZ(((Long) n).longValue());
}
if (n instanceof BigInteger) {
return F.integer((BigInteger) n);
}
if (n instanceof BigDecimal) {
return F.num(new Apfloat(((BigDecimal) n).doubleValue()));
}
if (n instanceof Float) {
return F.num(((Float) n).doubleValue());
}
if (n instanceof AtomicDouble) {
return F.num(((AtomicDouble) n).doubleValue());
}
if (n instanceof AtomicInteger) {
return F.ZZ(((AtomicInteger) n).longValue());
}
if (n instanceof AtomicLong) {
return F.ZZ(((AtomicLong) n).longValue());
}
return F.num(((Number) n).doubleValue());
}
public double getProjectionScore(TerminologyService referenceTerminology) {
AtomicDouble sum = new AtomicDouble(0);
AtomicDouble total = new AtomicDouble(0);
List<Term> top100 = topN(100).collect(toList());
for(Term docTerm :top100) {
total.addAndGet(docTerm.getSpecificity());
int baseRank = getBaseRankInRefTermino(referenceTerminology, docTerm);
if(baseRank > 0 && baseRank < 500)
sum.addAndGet(docTerm.getSpecificity());
}
return sum.doubleValue() / total.doubleValue();
}
private void flush() {
Collection<Metric> metricsToFlush = new ArrayList<>();
while(accumulator.size() > 0 && (accumulator.firstKey() < DateTime.now(DateTimeZone.UTC).getMillis() / 1000 - distheneConfiguration.getCarbon().getAggregatorDelay())) {
ConcurrentMap<MetricKey, AtomicDouble> timestampMap = accumulator.pollFirstEntry().getValue();
for(Map.Entry<MetricKey, AtomicDouble> entry : timestampMap.entrySet()) {
metricsToFlush.add(new Metric(entry.getKey(), entry.getValue().get()));
}
}
if (metricsToFlush.size() > 0) {
doFlush(metricsToFlush, getFlushRateLimiter(metricsToFlush.size()));
}
}
/**
* Constructor.
*
* @param registry the metrics registry
* @param dataServices The {@link DataServices} instance to use
* @param userMetricsProperties the properties that configure this task
*/
public UserMetricsTask(
final MeterRegistry registry,
final DataServices dataServices,
final UserMetricsProperties userMetricsProperties
) {
this.registry = registry;
this.persistenceService = dataServices.getPersistenceService();
this.userMetricsProperties = userMetricsProperties;
this.activeUsersCount = new AtomicDouble(Double.NaN);
// Register gauge for count of distinct users with active jobs.
Gauge.builder(USER_ACTIVE_USERS_METRIC_NAME, this::getUsersCount)
.register(registry);
}
/**
* {@inheritDoc}
*/
@Override
public double updateModel(DataStream<DynamicDataInstance> dataStream) {
Stream<DataOnMemory<DynamicDataInstance>> stream = null;
if (parallelMode){
stream = dataStream.parallelStreamOfBatches(windowsSize);
}else{
stream = dataStream.streamOfBatches(windowsSize);
}
dataInstanceCount = new AtomicDouble(0); //Initial count
this.sumSS = stream
.peek(batch -> {
dataInstanceCount.getAndAdd(batch.getNumberOfDataInstances());
if (debug) System.out.println("Parallel ML procesando "+(int)dataInstanceCount.get() +" instances");
})
.map(batch -> {
return batch.stream()
.map(dataInstance -> computeCountSufficientStatistics(this.efBayesianNetwork, dataInstance))
.reduce(DynamicPartialSufficientSatistics::sumNonStateless).get();
})
.reduce(DynamicPartialSufficientSatistics::sumNonStateless).get();
if (laplace) {
DynamicPartialSufficientSatistics initSS = DynamicPartialSufficientSatistics.createInitPartialSufficientStatistics(efBayesianNetwork);
sumSS.sum(initSS);
}
return Double.NaN;
}
@Test
public void operator_equals_short_Number() {
assertFalse(operator_equals(left, new AtomicDouble(right)));
assertFalse(operator_equals(left2, new AtomicDouble(right)));
assertTrue(operator_equals(left, new AtomicDouble(left)));
}
@Test
public void operator_greaterThan_Byte_Number() {
assertTrue(operator_greaterThan(left, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left2, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left, new AtomicDouble(left)));
}
@Override
public void metric(String label, double value, String topic) {
Metrics.gauge(label, Collections.singletonList(
Tag.of("topic", topic)), new AtomicDouble(0)).set(value);
}
public static PIDController of(Double kp, Double ki, Double kd, Double deltaT, AtomicDouble dampener) {
return new PIDController(kp, ki, kd, deltaT, dampener);
}
@Test
public void operator_notEquals_Byte_Number() {
assertTrue(operator_notEquals(left, new AtomicDouble(right)));
assertTrue(operator_notEquals(left2, new AtomicDouble(right)));
assertFalse(operator_notEquals(left, new AtomicDouble(left)));
}
@Test
public void operator_greaterThan_short_Number() {
assertTrue(operator_greaterThan(left, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left2, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left, new AtomicDouble(left)));
}
@Test
public void operator_greaterEqualsThan_Byte_Number() {
assertTrue(operator_greaterEqualsThan(left, new AtomicDouble(right)));
assertFalse(operator_greaterEqualsThan(left2, new AtomicDouble(right)));
assertTrue(operator_greaterEqualsThan(left, new AtomicDouble(left)));
}
public Map<String, AtomicDouble> getGaugeCollector() {
return gaugeCollector;
}
@Test
public void operator_lessEqualsThan_Byte_Number() {
assertFalse(operator_lessEqualsThan(left, new AtomicDouble(right)));
assertTrue(operator_lessEqualsThan(left2, new AtomicDouble(right)));
assertTrue(operator_lessEqualsThan(left, new AtomicDouble(left)));
}
protected String buildGaugeLog(String key, Map<String, AtomicDouble> gaugeCollector) {
return key + " = " + gaugeCollector.get(key);
}
@Test
public void operator_greaterThan_Byte_Number() {
assertTrue(operator_greaterThan(left, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left2, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left, new AtomicDouble(left)));
}
@Test
public void operator_equals_Byte_Number() {
assertFalse(operator_equals(left, new AtomicDouble(right)));
assertFalse(operator_equals(left2, new AtomicDouble(right)));
assertTrue(operator_equals(left, new AtomicDouble(left)));
}
@Test
public void operator_notEquals_short_Number() {
assertTrue(operator_notEquals(left, new AtomicDouble(right)));
assertTrue(operator_notEquals(left2, new AtomicDouble(right)));
assertFalse(operator_notEquals(left, new AtomicDouble(left)));
}
@Test
public void operator_lessThan_Byte_Number() {
assertFalse(operator_lessThan(left, new AtomicDouble(right)));
assertTrue(operator_lessThan(left2, new AtomicDouble(right)));
assertFalse(operator_lessThan(left, new AtomicDouble(left)));
}
protected ConcurrentHashMap<String, AtomicDouble> getMetricsMap() {
return new ConcurrentHashMap<>();
}
@Override
protected ConcurrentHashMap<String, AtomicDouble> getMetricsMap() {
return metricsMap;
}
@Test
public void operator_greaterThan_Byte_Number() {
assertTrue(operator_greaterThan(left, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left2, new AtomicDouble(right)));
assertFalse(operator_greaterThan(left, new AtomicDouble(left)));
}
@Test
public void operator_lessEqualsThan_short_Number() {
assertFalse(operator_lessEqualsThan(left, new AtomicDouble(right)));
assertTrue(operator_lessEqualsThan(left2, new AtomicDouble(right)));
assertTrue(operator_lessEqualsThan(left, new AtomicDouble(left)));
}