001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2018–2019 microBean™. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 014 * implied. See the License for the specific language governing 015 * permissions and limitations under the License. 016 */ 017package org.microbean.microprofile.config; 018 019import java.util.Comparator; 020 021import org.eclipse.microprofile.config.spi.Converter; 022 023/** 024 * A {@link Comparator} that considers only the {@linkplain Converter 025 * priority, if any, available from the <code>Converter</code> 026 * instances} being compared, in accordance with the minimal 027 * requirements of the MicroProfile Config specification as spelled 028 * out (only) in the {@linkplain Converter <code>Converter</code> API 029 * documentation}. 030 * 031 * <p><strong>This {@link Comparator} implementation is, and must be, 032 * inconsistent with {@link Object#equals(Object) equals()}</strong>, 033 * as implied by the same specification.</p> 034 * 035 * @author <a href="https://about.me/lairdnelson" 036 * target="_parent">Laird Nelson</a> 037 * 038 * @see #compare(Converter, Converter) 039 * 040 * @see Converter 041 */ 042public final class ConverterComparator implements Comparator<Converter<?>> { 043 044 045 /* 046 * Static fields. 047 */ 048 049 050 /** 051 * The sole instance of this class. 052 */ 053 public static final ConverterComparator INSTANCE = new ConverterComparator(); 054 055 056 /* 057 * Constructors. 058 */ 059 060 061 /** 062 * Creates a new {@link ConverterComparator}. 063 */ 064 private ConverterComparator() { 065 super(); 066 } 067 068 069 /* 070 * Instance methods. 071 */ 072 073 074 /** 075 * Compares two {@link Converter}s, returning {@code -1} if the 076 * first has a {@linkplain Converter priority} greater than that of 077 * the second and {@code 1} if the second has a {@linkplain 078 * Converter priority} greater than that of the first, and {@code 0} 079 * in all other cases. 080 * 081 * <p><strong>This method may return {@code 0} when both {@link 082 * Converter}s are not otherwise semantically equal.</strong></p> 083 * 084 * @param firstConverter the first of two {@link Converter}s; may be 085 * {@code null} 086 * 087 * @param secondConverter the second of two {@link Converter}s; may 088 * be {@code null} 089 * 090 * @return {@code -1} if the first has a {@linkplain Converter 091 * priority} greater than that of the second and {@code 1} if the 092 * second has a {@linkplain Converter priority} greater than that of 093 * the first, and {@code 0} in all other cases 094 * 095 * @see Converter 096 */ 097 @Override 098 public final int compare(final Converter<?> firstConverter, final Converter<?> secondConverter) { 099 final int returnValue; 100 if (firstConverter == secondConverter) { 101 returnValue = 0; 102 } else if (firstConverter == null) { 103 returnValue = 1; 104 } else if (secondConverter == null) { 105 returnValue = -1; 106 } else { 107 final int firstPriority = Converters.getPriority(firstConverter); 108 final int secondPriority = Converters.getPriority(secondConverter); 109 if (firstPriority > secondPriority) { 110 returnValue = -1; 111 } else if (firstPriority < secondPriority) { 112 returnValue = 1; 113 } else { 114 returnValue = 0; 115 } 116 } 117 return returnValue; 118 } 119 120}