Commit be32d5c2 authored by Matija Obreza's avatar Matija Obreza
Browse files

Filters: added LocalDateFilter

parent a919a9e9
/*
* Copyright 2022 Global Crop Diversity Trust
*
* 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 org.genesys.blocks.model.filters;
import java.io.Serializable;
import java.time.LocalDate;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.TemporalExpression;
/**
* Utility filtering for {@link LocalDate} type.
*/
public class LocalDateFilter implements Serializable {
private static final long serialVersionUID = 7335760829004802798L;
/** Matches records where date is on or after "since" value. */
// >=
public LocalDate ge;
/** The gt. */
// >
public LocalDate gt;
/** Matches records where date is on or before "until" value. */
// <=
public LocalDate le;
/** The lt. */
// <
public LocalDate lt;
/**
* Builds the query.
*
* @param date the date
* @return the predicate
*/
public Predicate buildQuery(final TemporalExpression<LocalDate> date) {
final BooleanBuilder and = new BooleanBuilder();
if (ge != null) {
and.and(date.isNotNull());
and.and(date.goe(ge));
}
if (gt != null) {
and.and(date.isNotNull());
and.and(date.gt(gt));
}
if (le != null) {
and.and(date.isNotNull());
and.and(date.loe(le));
}
if (lt != null) {
and.and(date.isNotNull());
and.and(date.lt(lt));
}
return and;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
final StringBuilder str = new StringBuilder();
if (ge != null) {
str.append(" date >= ").append(ge).append(" ");
}
if (gt != null) {
str.append(" date > ").append(gt).append(" ");
}
if (le != null) {
str.append(" date <= ").append(le).append(" ");
}
if (lt != null) {
str.append(" date < ").append(lt).append(" ");
}
return str.toString().replaceAll("\\s{2,}", " ");
}
/**
* Lt.
*
* @param date the date
* @return the date filter
*/
public LocalDateFilter lt(final LocalDate date) {
lt = date;
return this;
}
/**
* Ge.
*
* @param date the date
* @return the date filter
*/
public LocalDateFilter ge(final LocalDate date) {
ge = date;
return this;
}
}
/*
* Copyright 2020 Global Crop Diversity Trust
*
* 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 org.genesys.blocks.tests.filter;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import java.time.LocalDate;
import java.util.Date;
import org.genesys.blocks.model.filters.DateFilter;
import org.genesys.blocks.model.filters.LocalDateFilter;
import org.junit.Test;
/**
* The Class StringFilterTest.
*/
public class DateFiltersTest {
@Test
public void testLocalDateFilter() {
var filter = new LocalDateFilter();
filter.ge(LocalDate.of(2022, 6, 28));
assertThat(filter.ge, not(nullValue()));
assertThat(filter.ge, equalTo(LocalDate.of(2022, 6, 28)));
filter.lt(LocalDate.of(2022, 6, 28));
assertThat(filter.lt, not(nullValue()));
assertThat(filter.lt, equalTo(LocalDate.of(2022, 6, 28)));
assertThat(filter.gt, is(nullValue()));
assertThat(filter.le, is(nullValue()));
}
@Test
public void testDateFilter() {
var filter = new DateFilter();
filter.ge(new Date());
assertThat(filter.ge, not(nullValue()));
filter.lt(new Date());
assertThat(filter.lt, not(nullValue()));
assertThat(filter.gt, is(nullValue()));
assertThat(filter.le, is(nullValue()));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment