All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.palantir.conjure.java.util.ParameterOrder Maven / Gradle / Ivy

There is a newer version: 8.36.0
Show newest version
/*
 * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.conjure.java.util;

import com.google.common.collect.ImmutableList;
import com.palantir.conjure.java.visitor.DefaultTypeVisitor;
import com.palantir.conjure.spec.ArgumentDefinition;
import com.palantir.conjure.spec.BodyParameterType;
import com.palantir.conjure.spec.HeaderParameterType;
import com.palantir.conjure.spec.ListType;
import com.palantir.conjure.spec.MapType;
import com.palantir.conjure.spec.OptionalType;
import com.palantir.conjure.spec.ParameterType;
import com.palantir.conjure.spec.PathParameterType;
import com.palantir.conjure.spec.QueryParameterType;
import com.palantir.conjure.spec.SetType;
import com.palantir.conjure.spec.Type;
import java.util.Comparator;
import java.util.List;

public final class ParameterOrder {

    /** Produces an ordering for ParamaterType of Header, Path, Query, Body. */
    private static final ParameterType.Visitor PARAM_SORT_ORDER = new ParameterType.Visitor() {
        @Override
        public Integer visitBody(BodyParameterType _value) {
            return 30;
        }

        @Override
        public Integer visitHeader(HeaderParameterType _value) {
            return 0;
        }

        @Override
        public Integer visitPath(PathParameterType _value) {
            return 10;
        }

        @Override
        public Integer visitQuery(QueryParameterType _value) {
            return 20;
        }

        @Override
        public Integer visitUnknown(String _unknownType) {
            return -1;
        }
    };

    /**
     * Produces a type sort ordering for use with {@link #PARAM_SORT_ORDER} such that types with known defaults come
     * after types without known defaults.
     */
    private static final Type.Visitor TYPE_SORT_ORDER = new DefaultTypeVisitor() {
        @Override
        public Integer visitOptional(OptionalType _value) {
            return 1;
        }

        @Override
        public Integer visitList(ListType _value) {
            return 1;
        }

        @Override
        public Integer visitSet(SetType _value) {
            return 1;
        }

        @Override
        public Integer visitMap(MapType _value) {
            return 1;
        }

        @Override
        public Integer visitDefault() {
            return 0;
        }
    };

    private static final Comparator COMPARATOR = Comparator.comparing(
            o -> o.getParamType().accept(PARAM_SORT_ORDER) + o.getType().accept(TYPE_SORT_ORDER));

    public static List sorted(List input) {
        return input.stream().sorted(COMPARATOR).collect(ImmutableList.toImmutableList());
    }

    private ParameterOrder() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy