-
Notifications
You must be signed in to change notification settings - Fork 94
/
PropertyOrdering.java
69 lines (62 loc) · 2.89 KB
/
PropertyOrdering.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Roman Grigoriadi
* Gyúróczki Gergő
******************************************************************************/
package org.eclipse.yasson.internal.model.customization;
import org.eclipse.yasson.internal.model.ClassModel;
import org.eclipse.yasson.internal.model.PropertyModel;
import java.util.*;
import java.util.function.*;
import javax.json.bind.JsonbConfig;
import javax.json.bind.config.PropertyOrderStrategy;
/**
* Order properties in bean object. {@link javax.json.bind.annotation.JsonbPropertyOrder} have always precedence.
* If configured with {@link JsonbConfig} provided property order strategy will be used.
*
* @author Roman Grigoriadi
*/
public class PropertyOrdering {
private final Function<Collection<PropertyModel>, List<PropertyModel>> propertyOrderStrategy;
/**
* Creates a new instance.
*
* @param propertyOrderStrategy Property order strategy. Must be not null.
*/
public PropertyOrdering(Function<Collection<PropertyModel>, List<PropertyModel>> propertyOrderStrategy) {
this.propertyOrderStrategy = Objects.requireNonNull(propertyOrderStrategy);
}
/**
* Sorts class properties either, by class {@link javax.json.bind.annotation.JsonbPropertyOrder} annotation,
* or by {@link PropertyOrderStrategy} if set in {@link JsonbConfig}.
*
* @param properties Properties to sort.
* @param classModel Class model.
* @return Sorted list of properties.
*/
public List<PropertyModel> orderProperties(List<PropertyModel> properties, ClassModel classModel) {
Map<String, PropertyModel> byReadName = new HashMap<>();
properties.stream().forEach(propertyModel -> byReadName.put(propertyModel.getPropertyName(), propertyModel));
String[] order = classModel.getClassCustomization().getPropertyOrder();
List<PropertyModel> sortedProperties = new ArrayList<>();
if (order != null) {
//if @JsonbPropertyOrder annotation is defined on a class
for (String propName : order) {
final PropertyModel remove = byReadName.remove(propName);
if (remove != null) {
sortedProperties.add(remove);
}
}
}
sortedProperties.addAll(propertyOrderStrategy.apply(byReadName.values()));
return sortedProperties;
}
}