Skip to content

Commit 3205248

Browse files
committed
reimplement Order using record classes
1 parent a551068 commit 3205248

File tree

6 files changed

+363
-209
lines changed

6 files changed

+363
-209
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query;
6+
7+
import jakarta.persistence.criteria.Nulls;
8+
import jakarta.persistence.metamodel.SingularAttribute;
9+
10+
/**
11+
* An {@link Order} based on an attribute of an entity.
12+
*
13+
* @param <X> The entity type
14+
*
15+
* @author Gavin King
16+
*/
17+
record AttributeOrder<X>
18+
(SortDirection direction, Nulls nullPrecedence, SingularAttribute<X, ?> attribute, boolean caseSensitive)
19+
implements Order<X> {
20+
21+
AttributeOrder(SortDirection order, Nulls nullPrecedence, SingularAttribute<X, ?> attribute) {
22+
this( order, nullPrecedence, attribute, true );
23+
}
24+
25+
private AttributeOrder(AttributeOrder<X> that, Nulls nullPrecedence) {
26+
this( that.direction, nullPrecedence, that.attribute, that.caseSensitive );
27+
}
28+
29+
private AttributeOrder(AttributeOrder<X> that, SortDirection direction) {
30+
this( direction, that.nullPrecedence, that.attribute, that.caseSensitive );
31+
}
32+
33+
private AttributeOrder(AttributeOrder<X> that, boolean caseSensitive) {
34+
this( that.direction, that.nullPrecedence, that.attribute, caseSensitive );
35+
}
36+
37+
@Override
38+
public Class<X> entityClass() {
39+
return attribute.getDeclaringType().getJavaType();
40+
}
41+
42+
@Override
43+
public int element() {
44+
return 1;
45+
}
46+
47+
@Override
48+
public String attributeName() {
49+
return attribute.getName();
50+
}
51+
52+
@Override
53+
public Order<X> ignoringCase() {
54+
return new AttributeOrder<>( this, true );
55+
}
56+
57+
@Override
58+
public Order<X> reverse() {
59+
return new AttributeOrder<>( this, direction.reverse() );
60+
}
61+
62+
@Override
63+
public Order<X> withNullsFirst() {
64+
return new AttributeOrder<>( this, Nulls.FIRST );
65+
}
66+
67+
@Override
68+
public Order<X> withNullsLast() {
69+
return new AttributeOrder<>( this, Nulls.LAST );
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return attribute.getName() + " " + direction;
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query;
6+
7+
import jakarta.persistence.criteria.Nulls;
8+
import jakarta.persistence.metamodel.SingularAttribute;
9+
10+
/**
11+
* An {@link Order} based on an indexed element of a select clause.
12+
*
13+
* @param <X> The query result type
14+
*
15+
* @author Gavin King
16+
*/
17+
record ElementOrder<X>
18+
(SortDirection direction, Nulls nullPrecedence, int element, boolean caseSensitive)
19+
implements Order<X> {
20+
21+
ElementOrder(SortDirection order, Nulls nullPrecedence, int element) {
22+
this( order, nullPrecedence, element, true );
23+
}
24+
25+
@Override
26+
public Class<X> entityClass() {
27+
return null;
28+
}
29+
30+
@Override
31+
public String attributeName() {
32+
return null;
33+
}
34+
35+
@Override
36+
public SingularAttribute<X, ?> attribute() {
37+
return null;
38+
}
39+
40+
@Override
41+
public Order<X> ignoringCase() {
42+
return new ElementOrder<>( direction, nullPrecedence, element, false );
43+
}
44+
45+
@Override
46+
public Order<X> reverse() {
47+
return new ElementOrder<>( direction.reverse(), nullPrecedence, element, caseSensitive );
48+
}
49+
50+
@Override
51+
public Order<X> withNullsFirst() {
52+
return new ElementOrder<>( direction, Nulls.FIRST, element );
53+
}
54+
55+
@Override
56+
public Order<X> withNullsLast() {
57+
return new ElementOrder<>( direction, Nulls.LAST, element );
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return element + " " + direction;
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query;
6+
7+
import jakarta.persistence.criteria.Nulls;
8+
import jakarta.persistence.metamodel.SingularAttribute;
9+
10+
/**
11+
* An {@link Order} based on the name of an attribute of an entity.
12+
*
13+
* @param <X> The entity type
14+
*
15+
* @author Gavin King
16+
*/
17+
record NamedAttributeOrder<X>
18+
(SortDirection direction, Nulls nullPrecedence, Class<X> entityClass, String attributeName, boolean caseSensitive)
19+
implements Order<X> {
20+
21+
NamedAttributeOrder(SortDirection order, Nulls nullPrecedence, Class<X> entityClass, String attributeName) {
22+
this( order, nullPrecedence, entityClass, attributeName, true );
23+
}
24+
25+
private NamedAttributeOrder(NamedAttributeOrder<X> that, Nulls nullPrecedence) {
26+
this( that.direction, nullPrecedence, that.entityClass, that.attributeName, that.caseSensitive );
27+
}
28+
29+
private NamedAttributeOrder(NamedAttributeOrder<X> that, SortDirection direction) {
30+
this( direction, that.nullPrecedence, that.entityClass, that.attributeName, that.caseSensitive );
31+
}
32+
33+
private NamedAttributeOrder(NamedAttributeOrder<X> that, boolean caseSensitive) {
34+
this( that.direction, that.nullPrecedence, that.entityClass, that.attributeName, caseSensitive );
35+
}
36+
37+
@Override
38+
public int element() {
39+
return 1;
40+
}
41+
42+
@Override
43+
public SingularAttribute<X, ?> attribute() {
44+
return null;
45+
}
46+
47+
@Override
48+
public Order<X> ignoringCase() {
49+
return new NamedAttributeOrder<>( this, false );
50+
}
51+
52+
@Override
53+
public Order<X> reverse() {
54+
return new NamedAttributeOrder<>( this, direction.reverse() );
55+
}
56+
57+
@Override
58+
public Order<X> withNullsFirst() {
59+
return new NamedAttributeOrder<>( this, Nulls.FIRST );
60+
}
61+
62+
@Override
63+
public Order<X> withNullsLast() {
64+
return new NamedAttributeOrder<>( this, Nulls.LAST );
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return attributeName + " " + direction;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)