-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
YearGroupBySupplier
/ YearMonthGroupBySupplier
/ `Y…
…earMonthDayGroupBySupplier`.
- Loading branch information
Showing
11 changed files
with
441 additions
and
138 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/AbstractDateGroupBySupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosid.segment.grouped.date; | ||
|
||
import me.ahoo.cosid.segment.grouped.GroupBySupplier; | ||
import me.ahoo.cosid.segment.grouped.GroupedKey; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.TemporalAccessor; | ||
|
||
public abstract class AbstractDateGroupBySupplier<D extends TemporalAccessor> implements GroupBySupplier { | ||
protected final DateTimeFormatter formatter; | ||
|
||
public AbstractDateGroupBySupplier(DateTimeFormatter formatter) { | ||
this.formatter = formatter; | ||
} | ||
|
||
abstract D now(); | ||
|
||
abstract LocalDateTime lastTimestamp(D date); | ||
|
||
@Override | ||
public GroupedKey get() { | ||
D nowDate = now(); | ||
String key = formatter.format(nowDate); | ||
|
||
LocalDateTime lastTs = lastTimestamp(nowDate); | ||
ZoneId currentZone = ZoneId.systemDefault(); | ||
long ttlAt = lastTs.atZone(currentZone).toInstant().toEpochMilli() / 1000; | ||
return new GroupedKey(key, ttlAt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosid.segment.grouped.date; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class YearMonthDayGroupBySupplier extends AbstractDateGroupBySupplier<LocalDate> { | ||
|
||
public YearMonthDayGroupBySupplier(DateTimeFormatter formatter) { | ||
super(formatter); | ||
} | ||
|
||
public YearMonthDayGroupBySupplier(String pattern) { | ||
this(DateTimeFormatter.ofPattern(pattern)); | ||
} | ||
|
||
@Override | ||
LocalDate now() { | ||
return LocalDate.now(); | ||
} | ||
|
||
@Override | ||
LocalDateTime lastTimestamp(LocalDate date) { | ||
return LocalDateTime.of(date, LocalTime.MAX); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
cosid-core/src/main/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosid.segment.grouped.date; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.YearMonth; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
|
||
public class YearMonthGroupBySupplier extends AbstractDateGroupBySupplier<YearMonth> { | ||
public YearMonthGroupBySupplier(DateTimeFormatter formatter) { | ||
super(formatter); | ||
} | ||
|
||
public YearMonthGroupBySupplier(String pattern) { | ||
this(DateTimeFormatter.ofPattern(pattern)); | ||
} | ||
|
||
@Override | ||
YearMonth now() { | ||
return YearMonth.now(); | ||
} | ||
|
||
@Override | ||
LocalDateTime lastTimestamp(YearMonth date) { | ||
LocalDate lastDate = LocalDate.MAX.withYear(date.getYear()).withMonth(date.getMonthValue()); | ||
return LocalDateTime.of(lastDate, LocalTime.MAX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ore/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthDayGroupBySupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosid.segment.grouped.date; | ||
|
||
import me.ahoo.cosid.segment.grouped.GroupedKey; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.YearMonth; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class YearMonthDayGroupBySupplierTest { | ||
|
||
@Test | ||
void get() { | ||
String pattern = "yyyyMMdd"; | ||
YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern); | ||
GroupedKey groupedKey = supplier.get(); | ||
assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern))); | ||
} | ||
|
||
@Test | ||
void getYyMm() { | ||
String pattern = "yyMMDD"; | ||
YearMonthDayGroupBySupplier supplier = new YearMonthDayGroupBySupplier(pattern); | ||
GroupedKey groupedKey = supplier.get(); | ||
assertEquals(groupedKey.getKey(), LocalDate.now().format(DateTimeFormatter.ofPattern(pattern))); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...d-core/src/test/java/me/ahoo/cosid/segment/grouped/date/YearMonthGroupBySupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosid.segment.grouped.date; | ||
|
||
import me.ahoo.cosid.segment.grouped.GroupedKey; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.YearMonth; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class YearMonthGroupBySupplierTest { | ||
|
||
@Test | ||
void get() { | ||
String pattern = "yyyy-MM"; | ||
YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern); | ||
GroupedKey groupedKey = supplier.get(); | ||
assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern))); | ||
} | ||
|
||
@Test | ||
void getYyMm() { | ||
String pattern = "yyMM"; | ||
YearMonthGroupBySupplier supplier = new YearMonthGroupBySupplier(pattern); | ||
GroupedKey groupedKey = supplier.get(); | ||
assertEquals(groupedKey.getKey(), YearMonth.now().format(DateTimeFormatter.ofPattern(pattern))); | ||
} | ||
} |
Oops, something went wrong.