Skip to content

io microsphere util PriorityComparator

github-actions[bot] edited this page Mar 24, 2026 · 4 revisions

PriorityComparator

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/PriorityComparator.java

Overview

A Comparator implementation that sorts objects based on the value of the javax.annotation.Priority annotation.

If an object does not have the javax.annotation.Priority annotation, it is treated as having a default priority value of -1. If both objects lack the annotation, they are considered equal in priority.

Example Usage

`List list = new ArrayList<>();
list.add(new HighPriorityService());
list.add(new LowPriorityService());
Collections.sort(list, PriorityComparator.INSTANCE);
`

Note: The comparison is consistent with equals only if the compared objects are of the same class or both lack the Priority annotation.

Declaration

public class PriorityComparator implements Comparator<Object>

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.2-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

List<Object> list = new ArrayList<>();
list.add(new HighPriorityService());
list.add(new LowPriorityService());
Collections.sort(list, PriorityComparator.INSTANCE);

Method Examples

compare

PriorityComparator comparator = PriorityComparator.INSTANCE;
  int result = comparator.compare(serviceA, serviceB);
  if (result < 0) {
      System.out.println("serviceA has higher priority");
  }

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.PriorityComparator;

API Reference

Public Methods

Method Description
compare Singleton instance of PriorityComparator

Method Details

compare

public int compare(Object o1, Object o2)

Singleton instance of PriorityComparator / public static final PriorityComparator INSTANCE = new PriorityComparator();

/** Compares two objects based on the value of their javax.annotation.Priority annotation.

Example Usage

`PriorityComparator comparator = PriorityComparator.INSTANCE;
  int result = comparator.compare(serviceA, serviceB);
  if (result

*Since: 1.0.0*

## See Also

- `javax.annotation.Priority`

---

*This documentation was auto-generated from the source code of [microsphere-java](https://github.com/microsphere-projects/microsphere-java).*

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally