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

org.elasticsearch.common.io.stream.NamedWriteableRegistry Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.io.stream;

import java.util.HashMap;
import java.util.Map;

/**
 * Registry for {@link NamedWriteable} objects. Allows to register and retrieve prototype instances of writeable objects
 * given their name.
 */
public class NamedWriteableRegistry {

    private final Map, InnerRegistry> registry = new HashMap<>();

    /**
     * Registers a {@link NamedWriteable} prototype given its category
     */
    public synchronized  void registerPrototype(Class categoryClass, NamedWriteable namedWriteable) {
        @SuppressWarnings("unchecked")
        InnerRegistry innerRegistry = (InnerRegistry)registry.get(categoryClass);
        if (innerRegistry == null) {
            innerRegistry = new InnerRegistry<>(categoryClass);
            registry.put(categoryClass, innerRegistry);
        }
        innerRegistry.registerPrototype(namedWriteable);
    }

    /**
     * Returns a prototype of the {@link NamedWriteable} object identified by the name provided as argument and its category
     */
    public synchronized  NamedWriteable getPrototype(Class categoryClass, String name) {
        @SuppressWarnings("unchecked")
        InnerRegistry innerRegistry = (InnerRegistry)registry.get(categoryClass);
        if (innerRegistry == null) {
            throw new IllegalArgumentException("unknown named writeable category [" + categoryClass.getName() + "]");
        }
        return innerRegistry.getPrototype(name);
    }

    private static class InnerRegistry {

        private final Map> registry = new HashMap<>();
        private final Class categoryClass;

        private InnerRegistry(Class categoryClass) {
            this.categoryClass = categoryClass;
        }

        private void registerPrototype(NamedWriteable namedWriteable) {
            NamedWriteable existingNamedWriteable = registry.get(namedWriteable.getWriteableName());
            if (existingNamedWriteable != null) {
                throw new IllegalArgumentException("named writeable of type [" + namedWriteable.getClass().getName() + "] with name [" + namedWriteable.getWriteableName() + "] " +
                        "is already registered by type [" + existingNamedWriteable.getClass().getName() + "] within category [" + categoryClass.getName() + "]");
            }
            registry.put(namedWriteable.getWriteableName(), namedWriteable);
        }

        private NamedWriteable getPrototype(String name) {
            NamedWriteable namedWriteable = registry.get(name);
            if (namedWriteable == null) {
                throw new IllegalArgumentException("unknown named writeable with name [" + name + "] within category [" + categoryClass.getName() + "]");
            }
            return namedWriteable;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy