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

com.crosstreelabs.jaxrs.api.versioned.ValueObjectRegistry Maven / Gradle / Ivy

/*
 * 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.crosstreelabs.jaxrs.api.versioned;

import com.crosstreelabs.jaxrs.api.versioned.annotation.Version;
import com.crosstreelabs.jaxrs.api.versioned.util.VersionUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.MediaType;

/**
 * Keeps track of all known value objects. 
 */
public class ValueObjectRegistry {
    private static final Set> CLASSES = new HashSet<>();
    
    public static Set> getClasses() {
        return Collections.unmodifiableSet(CLASSES);
    }
    public static Class findForMediaType(final MediaType type) {
        int targetVersion = type.getParameters().containsKey("v")
                ? Integer.valueOf(type.getParameters().get("v"))
                : -1;
        Version highest = null;
        Class highestClass = null;
        for (Class cls : CLASSES) {
            if (!cls.isAnnotationPresent(Version.class)) {
                continue;
            }
            Version version = cls.getAnnotation(Version.class);
            if (!VersionUtils.isCompatible(type, version)) {
                continue;
            }
            if (version.version() == targetVersion) {
                return cls;
            }
            if (highest == null || version.version() > highest.version()) {
                highest = version;
                highestClass = cls;
            }
        }
        return highestClass;
    }
    public static void register(final Class cls) {
        CLASSES.add(cls);
    }
    public static void register(final Class...classes) {
        register(Arrays.asList(classes));
    }
    public static void register(final Collection> classes) {
        CLASSES.addAll(classes);
    }
    public static void clear() {
        CLASSES.clear();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy