Add example model of Apache Geode/Pivotal GemFire CQ query event handlers and types.

This commit is contained in:
John Blum
2018-05-24 12:53:34 -07:00
parent d3aa26a3ee
commit 0b3c81fe0b
3 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package example.geode.query.cq.event;
import java.io.Serializable;
import java.time.LocalDateTime;
import org.springframework.util.ObjectUtils;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* The {@link TemperatureReading} class is an Abstract Data Type (ADT) modeling a temperature event,
* tracking the recorded temperature, unit and timestamp of the event.
*
* @author John Blum
* @see example.geode.query.cq.event.TemperatureUnit
* @since 1.0.0
*/
@Data
@RequiredArgsConstructor(staticName = "of")
public class TemperatureReading {
@NonNull
private Integer temperature;
private LocalDateTime timestamp = LocalDateTime.now();
private TemperatureUnit temperatureUnit = TemperatureUnit.defaultTemperatureUnit();
public TemperatureReading at(LocalDateTime timestamp) {
setTimestamp(timestamp);
return this;
}
public TemperatureReading in(TemperatureUnit temperatureUnit) {
setTemperatureUnit(temperatureUnit);
return this;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TemperatureReading)) {
return false;
}
TemperatureReading that = (TemperatureReading) obj;
return ObjectUtils.nullSafeEquals(this.getTemperature(), that.getTemperature())
&& ObjectUtils.nullSafeEquals(this.getTemperatureUnit(), that.getTemperatureUnit());
}
@Override
public int hashCode() {
int hashValue = 17;
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getTemperature());
hashValue = 37 * hashValue + ObjectUtils.nullSafeHashCode(getTemperatureUnit());
return hashValue;
}
@Override
public String toString() {
return String.format("%d %s", getTemperature(), getTemperatureUnit());
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package example.geode.query.cq.event;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.geode.cache.query.CqEvent;
import org.springframework.data.gemfire.listener.annotation.ContinuousQuery;
/**
* The TemperatureReadingsContinuousQueriesHandler class...
*
* @author John Blum
* @since 1.0.0
*/
@SuppressWarnings("unused")
public final class TemperatureReadingsContinuousQueriesHandler {
private final AtomicInteger temperatureReadingsCounter = new AtomicInteger(0);
private final List<TemperatureReading> boilingTemperatureReadings = new CopyOnWriteArrayList<>();
private final List<TemperatureReading> freezingTemperatureReadings = new CopyOnWriteArrayList<>();
public List<TemperatureReading> getBoilingTemperatureReadings() {
return Collections.unmodifiableList(this.boilingTemperatureReadings);
}
public List<Integer> getBoilingTemperatures() {
return getBoilingTemperatureReadings().stream()
.map(TemperatureReading::getTemperature)
.collect(Collectors.toList());
}
public List<TemperatureReading> getFreezingTemperatureReadings() {
return Collections.unmodifiableList(this.freezingTemperatureReadings);
}
public List<Integer> getFreezingTemperatures() {
return getFreezingTemperatureReadings().stream()
.map(TemperatureReading::getTemperature)
.collect(Collectors.toList());
}
public int getTemperatureReadingCount() {
return this.temperatureReadingsCounter.get();
}
@ContinuousQuery(name = "BoilingTemperatures",
query = "SELECT * FROM /TemperatureReadings r WHERE r.temperature >= 212")
public void boilingTemperatures(CqEvent event) {
TemperatureReading temperatureReading = (TemperatureReading) event.getNewValue();
this.boilingTemperatureReadings.add(temperatureReading);
this.temperatureReadingsCounter.incrementAndGet();
}
@ContinuousQuery(name = "FreezingTemperatures",
query = "SELECT * FROM /TemperatureReadings r WHERE r.temperature <= 32")
public void freezingTemperatures(CqEvent event) {
TemperatureReading temperatureReading = (TemperatureReading) event.getNewValue();
this.freezingTemperatureReadings.add(temperatureReading);
this.temperatureReadingsCounter.incrementAndGet();
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package example.geode.query.cq.event;
import java.util.Locale;
import java.util.Optional;
/**
* The {@link TemperatureUnit} enum is an enumeration of different temperature units
* as defined by International System of Units (SI).
*
* @author John Blum
* @since 1.0.0
*/
@SuppressWarnings("unused")
public enum TemperatureUnit {
CELSIUS("°C"),
FAHRENHEIT("°F"),
KELVIN("K");
public static TemperatureUnit defaultTemperatureUnit() {
return Optional.of(Locale.getDefault())
.map(Locale::getISO3Country)
.filter(Locale.US.getISO3Country()::equalsIgnoreCase)
.map(it -> TemperatureUnit.FAHRENHEIT)
.orElse(TemperatureUnit.CELSIUS);
}
private final String symbol;
TemperatureUnit(String symbol) {
this.symbol = symbol;
}
public String getSymbol() {
return this.symbol;
}
@Override
public String toString() {
return getSymbol();
}
}