Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBX-2780: Create Interface for TableFilterWrapper #4729

Merged
merged 35 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f097b07
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
d0d2afe
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
c796445
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
82650de
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
891129b
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
ea580fb
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
c42decf
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
318221f
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
49238e5
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
41d57bc
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
c8fbb9f
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
3b9a17a
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
57609f5
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
fa50264
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
a5e3b7a
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
e8563ed
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
5aebd99
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 25, 2024
25c762a
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
2ad43eb
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
7be0387
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
5192935
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
1b7b601
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
db4f066
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
388b33e
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
f366e36
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
a4add85
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 26, 2024
138a764
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
fee0965
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
7013807
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
faf434e
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
5b2446c
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
e798a35
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
6421c13
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
4c77f97
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 29, 2024
b84154f
HBX-2780: Create Interface for TableFilterWrapper
koentsje Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.hibernate.tool.orm.jbt.api;

import java.util.Map;

import org.hibernate.tool.orm.jbt.internal.util.TypeRegistry;

public interface TypeFactoryWrapper {

default TypeWrapper getBooleanType() { return TypeRegistry.getType("boolean"); }
default TypeWrapper getByteType() { return TypeRegistry.getType("byte"); }
default TypeWrapper getBigIntegerType() { return TypeRegistry.getType("big_integer"); }
default TypeWrapper getShortType() { return TypeRegistry.getType("short"); }
default TypeWrapper getCalendarType() { return TypeRegistry.getType("calendar"); }
default TypeWrapper getCalendarDateType() { return TypeRegistry.getType("calendar_date"); }
default TypeWrapper getIntegerType() { return TypeRegistry.getType("integer"); }
default TypeWrapper getBigDecimalType() { return TypeRegistry.getType("big_decimal"); }
default TypeWrapper getCharacterType() { return TypeRegistry.getType("character"); }
default TypeWrapper getClassType() { return TypeRegistry.getType("class"); }
default TypeWrapper getCurrencyType() { return TypeRegistry.getType("currency"); }
default TypeWrapper getDateType() { return TypeRegistry.getType("date"); }
default TypeWrapper getDoubleType() { return TypeRegistry.getType("double"); }
default TypeWrapper getFloatType() { return TypeRegistry.getType("float"); }
default TypeWrapper getLocaleType() { return TypeRegistry.getType("locale"); }
default TypeWrapper getLongType() { return TypeRegistry.getType("long"); }
default TypeWrapper getStringType() { return TypeRegistry.getType("string"); }
default TypeWrapper getTextType() { return TypeRegistry.getType("text"); }
default TypeWrapper getTimeType() { return TypeRegistry.getType("time"); }
default TypeWrapper getTimestampType() { return TypeRegistry.getType("timestamp"); }
default TypeWrapper getTimezoneType() { return TypeRegistry.getType("timezone"); }
default TypeWrapper getTrueFalseType() { return TypeRegistry.getType("true_false"); }
default TypeWrapper getYesNoType() { return TypeRegistry.getType("true_false"); }
default TypeWrapper getNamedType(String name) { return TypeRegistry.getType(name); }
default TypeWrapper getBasicType(String name) { return getNamedType(name); }
default Map<TypeWrapper, String> getTypeFormats() { return TypeRegistry.getTypeFormats(); }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hibernate.tool.orm.jbt.internal.factory;

import org.hibernate.tool.orm.jbt.api.TypeFactoryWrapper;

public class TypeFactoryWrapperFactory {

private static TypeFactoryWrapper INSTANCE = new TypeFactoryWrapper() {};

public static TypeFactoryWrapper createTypeFactoryWrapper() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.hibernate.tool.orm.jbt.internal.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Currency;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.hibernate.tool.orm.jbt.api.TypeWrapper;
import org.hibernate.tool.orm.jbt.internal.factory.TypeWrapperFactory;
import org.hibernate.type.BasicTypeRegistry;
import org.hibernate.type.Type;
import org.hibernate.type.spi.TypeConfiguration;

public class TypeRegistry {

static Map<String, TypeWrapper> TYPE_REGISTRY = new HashMap<String, TypeWrapper>();
static Map<TypeWrapper, String> TYPE_FORMATS = null;
private static final BasicTypeRegistry BASIC_TYPE_REGISTRY = new TypeConfiguration().getBasicTypeRegistry();

public static TypeWrapper getType(String name) {
if (!TYPE_REGISTRY.containsKey(name)) {
Type basicType = BASIC_TYPE_REGISTRY.getRegisteredType(name);
if (basicType != null) {
TYPE_REGISTRY.put(name, TypeWrapperFactory.createTypeWrapper(basicType));
} else {
TYPE_REGISTRY.put(name, null);
}
}
return TYPE_REGISTRY.get(name);
}

public static Map<TypeWrapper, String> getTypeFormats() {
if (TYPE_FORMATS == null) {
initializeTypeFormats();
}
return TYPE_FORMATS;
}

private static void initializeTypeFormats() {
TYPE_FORMATS = new HashMap<TypeWrapper, String>();
addTypeFormat(getType("boolean"), Boolean.TRUE);
addTypeFormat(getType("byte"), Byte.valueOf((byte) 42));
addTypeFormat(getType("big_integer"), BigInteger.valueOf(42));
addTypeFormat(getType("short"), Short.valueOf((short) 42));
addTypeFormat(getType("calendar"), new GregorianCalendar());
addTypeFormat(getType("calendar_date"), new GregorianCalendar());
addTypeFormat(getType("integer"), Integer.valueOf(42));
addTypeFormat(getType("big_decimal"), new BigDecimal(42.0));
addTypeFormat(getType("character"), Character.valueOf('h'));
addTypeFormat(getType("class"), Class.class);
addTypeFormat(getType("currency"), Currency.getInstance(Locale.getDefault()));
addTypeFormat(getType("date"), new Date());
addTypeFormat(getType("double"), Double.valueOf(42.42));
addTypeFormat(getType("float"), Float.valueOf((float)42.42));
addTypeFormat(getType("locale"), Locale.getDefault());
addTypeFormat(getType("long"), Long.valueOf(42));
addTypeFormat(getType("string"), "a string"); //$NON-NLS-1$
addTypeFormat(getType("text"), "a text"); //$NON-NLS-1$
addTypeFormat(getType("time"), new Date());
addTypeFormat(getType("timestamp"), new Date());
addTypeFormat(getType("timezone"), TimeZone.getDefault());
addTypeFormat(getType("true_false"), Boolean.TRUE);
addTypeFormat(getType("yes_no"), Boolean.TRUE);
}

private static void addTypeFormat(TypeWrapper type, Object value) {
TYPE_FORMATS.put(type, type.toString(value));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package org.hibernate.tool.orm.jbt.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.hibernate.tool.orm.jbt.internal.factory.TypeFactoryWrapperFactory;
import org.hibernate.tool.orm.jbt.internal.util.TypeRegistry;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TypeFactoryWrapperTest {

private TypeFactoryWrapper typeFactoryWrapper = null;

@BeforeAll
public static void beforeAll() {
Locale.setDefault(new Locale("nl", "BE"));
}

@BeforeEach
public void beforeEach() {
typeFactoryWrapper = TypeFactoryWrapperFactory.createTypeFactoryWrapper();
}

@Test
public void testConstruction() {
assertNotNull(typeFactoryWrapper);
}

@Test
public void testGetBooleanType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getBooleanType();
assertEquals("boolean", typeWrapper.getName());
}

@Test
public void testGetByteType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getByteType();
assertEquals("byte", typeWrapper.getName());
}

@Test
public void testGetBigIntegerType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getBigIntegerType();
assertEquals("big_integer", typeWrapper.getName());
}

@Test
public void testGetShortType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getShortType();
assertEquals("short", typeWrapper.getName());
}

@Test
public void testGetCalendarType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getCalendarType();
assertEquals("calendar", typeWrapper.getName());
}

@Test
public void testGetCalendarDateType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getCalendarDateType();
assertEquals("calendar_date", typeWrapper.getName());
}

@Test
public void testGetIntegerType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getIntegerType();
assertEquals("integer", typeWrapper.getName());
}

@Test
public void testGetBigDecimalType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getBigDecimalType();
assertEquals("big_decimal", typeWrapper.getName());
}

@Test
public void testGetCharacterType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getCharacterType();
assertEquals("character", typeWrapper.getName());
}

@Test
public void testGetClassType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getClassType();
assertEquals("class", typeWrapper.getName());
}

@Test
public void testGetCurrencyType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getCurrencyType();
assertEquals("currency", typeWrapper.getName());
}

@Test
public void testGetDateType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getDateType();
assertEquals("date", typeWrapper.getName());
}

@Test
public void testGetDoubleType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getDoubleType();
assertEquals("double", typeWrapper.getName());
}

@Test
public void testGetFloatType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getFloatType();
assertEquals("float", typeWrapper.getName());
}

@Test
public void testGetLocaleType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getLocaleType();
assertEquals("locale", typeWrapper.getName());
}

@Test
public void testGetLongType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getLongType();
assertEquals("long", typeWrapper.getName());
}

@Test
public void testGetStringType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getStringType();
assertEquals("string", typeWrapper.getName());
}

@Test
public void testGetTextType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getTextType();
assertEquals("text", typeWrapper.getName());
}

@Test
public void testGetTimeType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getTimeType();
assertEquals("time", typeWrapper.getName());
}

@Test
public void testGetTimestampType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getTimestampType();
assertEquals("timestamp", typeWrapper.getName());
}

@Test
public void testGetTimezoneType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getTimezoneType();
assertEquals("timezone", typeWrapper.getName());
}

@Test
public void testGetTrueFalseType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getTrueFalseType();
assertEquals("true_false", typeWrapper.getName());
}

@Test
public void testGetYesNoType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getYesNoType();
assertEquals("true_false", typeWrapper.getName());
}

@Test
public void testGetNamedType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getNamedType(String.class.getName());
assertEquals("string", typeWrapper.getName());
}

@Test
public void testGetBasicType() {
TypeWrapper typeWrapper = typeFactoryWrapper.getBasicType(String.class.getName());
assertEquals("string", typeWrapper.getName());
}

@Test
public void testGetTypeFormats() {
Map<TypeWrapper, String> typeFormats = typeFactoryWrapper.getTypeFormats();
assertEquals(23, typeFormats.size());
assertEquals("true", typeFormats.get(TypeRegistry.getType("boolean")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("byte")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("big_integer")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("short")));
assertEquals(
new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
typeFormats.get(TypeRegistry.getType("calendar")));
assertEquals(
new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
typeFormats.get(TypeRegistry.getType("calendar_date")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("integer")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("big_decimal")));
assertEquals("h", typeFormats.get(TypeRegistry.getType("character")));
assertEquals(
Class.class.getName(),
typeFormats.get(TypeRegistry.getType("class")));
assertEquals(
Currency.getInstance(Locale.getDefault()).toString(),
typeFormats.get(TypeRegistry.getType("currency")));
assertEquals(
new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
typeFormats.get(TypeRegistry.getType("date")));
assertEquals("42.42", typeFormats.get(TypeRegistry.getType("double")));
assertEquals("42.42", typeFormats.get(TypeRegistry.getType("float")));
assertEquals(
Locale.getDefault().toString(),
typeFormats.get(TypeRegistry.getType("locale")));
assertEquals("42", typeFormats.get(TypeRegistry.getType("long")));
assertEquals("a string", typeFormats.get(TypeRegistry.getType("string")));
assertEquals("a text", typeFormats.get(TypeRegistry.getType("text")));
assertEquals(':', typeFormats.get(TypeRegistry.getType("time")).charAt(2));
//JdbcTimestampJavaType uses timezone UTC+0 for the string format vs the system default tz for JdbcDateJavaType.
SimpleDateFormat utcDateFormat = new SimpleDateFormat("yyyy-MM-dd");
utcDateFormat.setTimeZone(TimeZone.getTimeZone(ZoneId.from( ZoneOffset.UTC )));
assertEquals(utcDateFormat.format(new Date()).substring(0, 10),
typeFormats.get(TypeRegistry.getType("timestamp")).substring(0, 10));
assertEquals(
TimeZone.getDefault().getID(),
typeFormats.get(TypeRegistry.getType("timezone")));
assertEquals("true", typeFormats.get(TypeRegistry.getType("true_false")));
assertEquals("true", typeFormats.get(TypeRegistry.getType("yes_no")));
}
}