From 0b3c81fe0bf99e224f38a22ff8edd66972a60594 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 24 May 2018 12:53:34 -0700 Subject: [PATCH] Add example model of Apache Geode/Pivotal GemFire CQ query event handlers and types. --- .../query/cq/event/TemperatureReading.java | 89 +++++++++++++++++++ ...atureReadingsContinuousQueriesHandler.java | 87 ++++++++++++++++++ .../geode/query/cq/event/TemperatureUnit.java | 59 ++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReading.java create mode 100644 geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReadingsContinuousQueriesHandler.java create mode 100644 geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureUnit.java diff --git a/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReading.java b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReading.java new file mode 100644 index 00000000..0ce6fa04 --- /dev/null +++ b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReading.java @@ -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()); + } +} diff --git a/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReadingsContinuousQueriesHandler.java b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReadingsContinuousQueriesHandler.java new file mode 100644 index 00000000..e04a7b1a --- /dev/null +++ b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureReadingsContinuousQueriesHandler.java @@ -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 boilingTemperatureReadings = new CopyOnWriteArrayList<>(); + private final List freezingTemperatureReadings = new CopyOnWriteArrayList<>(); + + public List getBoilingTemperatureReadings() { + return Collections.unmodifiableList(this.boilingTemperatureReadings); + } + + public List getBoilingTemperatures() { + + return getBoilingTemperatureReadings().stream() + .map(TemperatureReading::getTemperature) + .collect(Collectors.toList()); + } + + public List getFreezingTemperatureReadings() { + return Collections.unmodifiableList(this.freezingTemperatureReadings); + } + + public List 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(); + } +} diff --git a/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureUnit.java b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureUnit.java new file mode 100644 index 00000000..1048aa2a --- /dev/null +++ b/geode-spring-boot-starter/src/test/java/example/geode/query/cq/event/TemperatureUnit.java @@ -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(); + } +}