private Comparator<String> orderComparator(final Class<?> clazz) { final Comparator<String> keyComparator; final JsonbPropertyOrder orderAnnotation = Meta.getAnnotation(clazz, JsonbPropertyOrder.class); if (orderAnnotation != null) { final List<String> indexed = new ArrayList<>(asList(orderAnnotation.value())); if (naming != null) { // JsonbPropertyOrder applies on java names for (int i = 0; i < indexed.size(); i++) { indexed.set(i, naming.translateName(indexed.get(i))); } } keyComparator = (o1, o2) -> { final int i1 = indexed.indexOf(o1); final int i2 = indexed.indexOf(o2); if (i1 < 0) { if (i2 < 0) { if (order != null) { switch (order) { case PropertyOrderStrategy.LEXICOGRAPHICAL: return o1.compareTo(o2); case PropertyOrderStrategy.REVERSE: return o2.compareTo(o1); case PropertyOrderStrategy.ANY: default: return 1; } } } return 1; } return i1 - i2; }; } else if (order != null) { switch (order) { case PropertyOrderStrategy.ANY: keyComparator = null; break; case PropertyOrderStrategy.LEXICOGRAPHICAL: keyComparator = String::compareTo; break; case PropertyOrderStrategy.REVERSE: keyComparator = Comparator.reverseOrder(); break; default: keyComparator = null; } } else { keyComparator = null; } return keyComparator; }
/** * Checks for {@link JsonbPropertyOrder} annotation. * * @param clazzElement class to search on * @return ordered properties names or null if not found */ public String[] getPropertyOrder(JsonbAnnotatedElement<Class<?>> clazzElement) { final JsonbPropertyOrder jsonbPropertyOrder = clazzElement.getElement().getAnnotation(JsonbPropertyOrder.class); return jsonbPropertyOrder != null ? jsonbPropertyOrder.value() : null; }