001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.manager.service;
017
018import java.util.Comparator;
019import java.util.List;
020
021import lombok.AllArgsConstructor;
022
023@AllArgsConstructor
024public class MediaTypeComparator implements Comparator<String> {
025
026    private final List<String> keys;
027
028    @Override
029    public int compare(final String mediaType1, final String mediaType2) {
030        // -- same types
031        if (mediaType1.equals(mediaType2)) {
032            return 0;
033        }
034
035        // -- only one of type contains wildcard
036        if (mediaType1.contains("*") && !mediaType2.contains("*")) {
037            return 1;
038        } else if (!mediaType1.contains("*") && mediaType2.contains("*")) {
039            return -1;
040        }
041
042        // -- both types d'ont have wildcard -> check subtypes
043        if (!mediaType1.contains("*") && !mediaType2.contains("*")) {
044            final String[] types1 = mediaType1.split("/");
045            final String[] types2 = mediaType2.split("/");
046            String subType1 = types1[1].contains("+") ? types1[1].split("\\+")[0] : null;
047            String subType2 = types2[1].contains("+") ? types2[1].split("\\+")[0] : null;
048            if (subType1 != null && subType2 == null) {
049                return 1;
050            } else if (subType1 == null && subType2 != null) {
051                return -1;
052            }
053            return keys.indexOf(mediaType1) - keys.indexOf(mediaType2);
054        }
055
056        // -- both types have wildcards
057        final String[] types1 = mediaType1.split("/");
058        final String[] types2 = mediaType2.split("/");
059        if (types1[0].contains("*") && !types2[0].contains("*")) {
060            return 1;
061        } else if (!types1[0].contains("*") && types2[0].contains("*")) {
062            return -1;
063        } else {// compare sub types
064            String subType1 = types1[1].contains("+") ? types1[1].split("\\+")[0] : null;
065            String subType2 = types2[1].contains("+") ? types2[1].split("\\+")[0] : null;
066            if ("*".equals(subType1) && !"*".equals(subType2)) {
067                return 1;
068            } else if (!"*".equals(subType1) && "*".equals(subType2)) {
069                return -1;
070            } else if (subType1 != null && subType2 != null) {
071                return keys.indexOf(mediaType1) - keys.indexOf(mediaType2);
072            } else {
073                // no subtypes or both are *
074                if ("*".equals(types1[1]) && !"*".equals(types2[1])) {
075                    return 1;
076                } else if (!"*".equals(types1[1]) && "*".equals(types2[1])) {
077                    return -1;
078                }
079
080                return keys.indexOf(mediaType1) - keys.indexOf(mediaType2);
081            }
082        }
083
084    }
085}