org.conqat.engine.commons.util.canonical.CanonicalJson Maven / Gradle / Ivy
Show all versions of teamscale-commons Show documentation
package org.conqat.engine.commons.util.canonical;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Comparator;
/**
* Provides additional support, when exporting the member as
* {@link org.conqat.engine.commons.util.JsonUtils#serializeToJSONCanonical(Object)
* canonical JSON}.
*/
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface CanonicalJson {
/**
* Provides information to the serializer about how values of a
* {@link java.util.Collection} should be ordered in the output JSON.
*/
OrderBy ordering();
/**
* Provides information to the serializer about how values of a
* {@link java.util.Collection} should be ordered in the output JSON.
*
* Either {@link #properties()} or {@link #comparator()} must be provided.
*/
@Retention(RetentionPolicy.RUNTIME)
// Explicitly remove any target,
// so that it can only be used as argument for ordering()
@Target({})
@interface OrderBy {
/**
* The properties by which the JSON array should be ordered.
*
* Values are the actual properties in the JSON (usually determined by
* {@link com.fasterxml.jackson.annotation.JsonProperty @JsonProperty}), not the
* field/method name.
*
* Each property must implement the {@link Comparable} interface.
*/
String[] properties() default {};
/**
* Comparator class that should be used for the ordering.
*
* The respective class must have a non-private no-args constructor.
*/
Class extends Comparator> comparator() default NoComparator.class;
/**
* Default implementation for {@link #comparator()}, indicating that nothing was
* set.
*/
abstract class NoComparator implements Comparator {
private NoComparator() {
throw new AssertionError("Should never be instantiated");
}
}
/**
* Comparator that should be used, when the type is a {@link Comparable}.
*/
class NaturalOrdering> implements Comparator {
@Override
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}
}
}